Solved

Dos Batch Check if file is open

Posted on 2007-12-04
7
4,470 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
Comment Utility
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
Comment Utility
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
Comment Utility
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 to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 38

Accepted Solution

by:
Insignificant Volunteer earned 350 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Please ignore my post. I didn't see Bill's post and his does the same thing.
0
 
LVL 38

Expert Comment

by:Insignificant Volunteer
Comment Utility
..... 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now