Problem with Check if File Open

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
NeilClough
Posts: 1
Joined: 11 Feb 2014 18:08

Problem with Check if File Open

#1 Post by NeilClough » 11 Feb 2014 18:16

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!
)

rodrigolima
Posts: 21
Joined: 19 Jul 2013 11:35
Location: Brazil

Re: Problem with Check if File Open

#2 Post by rodrigolima » 24 Feb 2014 08:18

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

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Problem with Check if File Open

#3 Post by dbenham » 24 Feb 2014 22:06

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

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: Problem with Check if File Open

#4 Post by Liviu » 26 Feb 2014 00:09

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

Post Reply