Solved

Dos Batch Check if file is open

Posted on 2007-12-04
7
4,622 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 17

Assisted Solution

by:Shanmuga Sundaram
Shanmuga Sundaram earned 150 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 38

Accepted Solution

by:
BillDL earned 350 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 38

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Bat File Script - HELP 2 34
How to remove unwanted words? 34 67
Renaming with batch file 9 80
Windows Batch File - pass parameter with embedded space 7 23
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

733 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