Link to home
Start Free TrialLog in
Avatar of Fraser_Admin
Fraser_AdminFlag for Canada

asked on

Dos Batch Check if file is open

I am creating a batch file to transfer a vb6 executable.  I need to know if the file is open in order to run the batch.  ie. if they are in the executable and using it exit out of the batch, if the exe is not running then i want to do the copy.  is there any way from within a dos batch you can tell if a file is running?
SOLUTION
Avatar of Shanmuga Sundaram D
Shanmuga Sundaram D
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
if required, write a vb code using api. Store the running exe filename in a separate text file and then check for the files before copying and try.
Avatar of Fraser_Admin

ASKER

yes it will not copy the file if it is running.

i could just create a text file when the file is opened and close when it is closed or something.  i will try that.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Could you use tasklist to do this?
@echo off
 
setlocal
 
set progName=my.exe
 
set found=N
 
for /f "tokens=*" %%a in ('tasklist ^| findstr /i "%progName%" 2^>NUL') do set found=Y
 
if "%found%"=="Y" echo Found %progName%
if not "%found%"=="Y" echo Did not find %progName%

Open in new window

Please ignore my post. I didn't see Bill's post and his does the same thing.
..... but not quite so professionally or eloquently as your code Steve.  Mine's always old and chunky from only being used to using DOS 7 :-)  Good that we were on the same wavelength though.

Thanks Fraser. Glad you found something of use.  There is something I remembered about the exit (return) codes for the FIND command that you may wish to bear in mind.  It can return an errorlevel code of 2 as well as a zero and 1.  Stick in another IF Errorlevel check for 2 and a Goto to cover the code 2 for completeness if you wish.

0  - Completed successfully (at least one match)
1  - Completed successfully but no matches found
2  - Did not complete due to an error (eg. file not found)