Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Dos Batch Check if file is open

Posted on 2007-12-04
7
Medium Priority
?
4,862 Views
Last Modified: 2009-12-16
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?
0
Comment
Question by:Fraser_Admin
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 17

Assisted Solution

by:Shanmuga Sundaram
Shanmuga Sundaram earned 600 total points
ID: 20405299
As far as I know, it is not possible to check whether the file is running. Eventhough if the file is in running, why dont you copy that file. Are you facing any issues while copying while the file is running?
0
 
LVL 17

Expert Comment

by:Shanmuga Sundaram
ID: 20405306
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.
0
 

Author Comment

by:Fraser_Admin
ID: 20405331
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.
0
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
LVL 39

Accepted Solution

by:
BillDL earned 1400 total points
ID: 20405892
What version of Windows will this be run on?

If Windows XP, you could do something like this to get a list of running programs at the command line and redirect to a *.txt file, and then use the Find command to search for the specific program file name in the text file.  The error code zero returned by the Find command indicates success, and 1 means failure to find the name of the executable in the list of running programs.

This is the commented file.  Just delete all the echoed prompts and pause commands and add your commands in the success section.  Just change the file name against the SET line to test, ie. test with a bogus filename, and then test with a correct one before using the code.  You can use the %PROGNAME% variable in your COPY command too, eg.
copy %PROGNAME% C:\Windows\System32


@echo off
set PROGNAME=YourVBfile.exe
::
tasklist > %TEMP%\tasks.txt
find /i "%PROGNAME%" %TEMP%\tasks.txt > nul
if errorlevel 1 goto :ERR
::
echo.
echo The error code %errorlevel% indicates success
echo The executable %PROGNAME% is not running
echo Copying VB exe file to destination ...
echo.
pause
goto :end
::
:ERR
echo.
echo The error code %errorlevel% indicates failure
echo The executable %PROGNAME% is running, or there
echo was some other error with the Find command.
echo.
echo Aborting process ...
echo.
pause
goto :end
::
:end
del %TEMP%\tasks.txt > nul
set PROGNAME=
EXIT

If you have Windows 9x, then there will probably be a way to capture a list of running programs, but I will have to think about this.
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 20405969
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

0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 20405975
Please ignore my post. I didn't see Bill's post and his does the same thing.
0
 
LVL 39

Expert Comment

by:BillDL
ID: 20409656
..... 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)
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Integration Management Part 2
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

879 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question