ZTree.com  | ZEN  | About...  

 Index   Back

[OT] Batch file to check if file is open   [OT]

By: Ron Metzger       
Date: Feb 14,2014 at 01:32
In Response to: [OT] Batch file to check if file is open (Neil Clough)

> I want a batch file to check if a file is open.
>
>
> Works generally except for the actual file type I want to check, which
> is Excel !!
>
> Works generally here too except if the file is set as ReadOnly in
> Excel.
ReadOnly is a file attribute unrelated to 'openness'.


>
> The batch file returns that the file is free !!

Free means not open by another process (read? write? read-only? read-write?)

I am guessing you are simply trying to determine whether a file can be written to, regardless of the locking mechanism.

In a batch file, the attributes of a file can be discovered using the %~a1
It returns a string in the format of drahs----

I use a routines like this:
:IsDir
:: call :IsDir _RetIsDir "filespec"
set "_Result=%~a1"
if defined _Result (
if /i NOT "!_Result:~0,1!"=="d" set "_Result="
)
set "%1=%_Result%"
set "_Result="
goto :eof

:IsRO
:: call :IsRO _RetIsRO "filespec"
set "_Result=%~a1"
if defined _Result (
if /i NOT "!_Result:~1,1!"=="r" set "_Result="
)
set "%1=%_Result%"
set "_Result="
goto :eof

From the main program:

setlocal EnableExtensions EnableDelayedExpansion
call :IsRO IsReadOnly "yourFileSpec"
if defined IsReadOnly echo "yourFileSpec" is Read-Only

(Sorry for the lack of indentation and lack of readability; site issue.)

Changing the offset of "!_Result:~3,1!"=="h" or "!_Result:~4,1!"=="s" can also find hidden or system files.

So, test for RO, Hidden, or System, then test the copy routine used in your original post.

Hope this helps.

Ron Metzger

1,250 views      
Thread locked
 

Messages in this Thread

 
96,637 Postings in 12,231 Threads, 350 registered users, 128 users online (0 registered, 128 guests)
Index | Admin contact |   Forum Time: Mar 29, 2024 - 2:54 pm UTC  |  Hits:62,409,784  (24,570 Today )
RSS Feed