> 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