Page 1 of 1

Problem with Check if File Open

Posted: 11 Feb 2014 18:16
by NeilClough
Hi,

The batch file to check if a file is open does not seem to work properly if the file is an Excel file and is set as "Read-Only"

i.e. the batch file reports that the file is available to open !!

Thanks,

Neil

Edited 13/02
Should have included Code.

Code: Select all

    
echo.N|copy /-y NUL "%filename%">NUL&&(

    echo.%filename% is free!

    rem leave this rem here at the block end!!!

) || (

    echo.%filename% is in use!
)

Re: Problem with Check if File Open

Posted: 24 Feb 2014 08:18
by rodrigolima
Hello NeilClough

Try this:


Code: Select all

@echo off
cls

set /p arquivo=<checkFile.txt

for %%i in (%arquivo%) do set verif=%%~ni

echo Verif:  %verif%
pause

tasklist /V | findstr "%verif%">log.txt

::pause

if %ERRORLEVEL% EQU 0 (
echo File is OPEN
) else (
echo FIle is not OPEN
)

if exist log.txt del log.txt

pause


Create a text file called checkFile.txt.
Put inside it the file path, for example:

C:\MyFiles\Folder1\holder.pdf


I tried it here in my machine and It work sucessfully...

Try it and report me.

See You

Re: Problem with Check if File Open

Posted: 24 Feb 2014 22:06
by dbenham
I would use the following:

Code: Select all

2>nul (call;>>"%filename%") && (
  echo "%filename%" is free!
) || (
  echo "%filename%" is in use or is read only!
)


Dave Benham

Re: Problem with Check if File Open

Posted: 26 Feb 2014 00:09
by Liviu
The question sounds familiar http://www.ztw3.com/forum/forum_entry.php?id=117668 ;-) and the only generic answer covering all cases is a utility (not batch) such as sysinternals' handle.exe.

dbenham wrote:I would use the following
Yours is the right answer, but to a slightly different question - whether the file can be written to using the (undocumented) locking/sharing modes of batch redirection against the (unknown) modes that a 3rd party app may have the file opened under. In Windows there are combinations of modes where the file can be both opened, and writeable from multiple programs at a time.

Liviu