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
Solved

Dos Batch Check if file is open

Posted on 2007-12-04
7
4,590 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 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 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