Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Dos Batch Check if file is open

Posted on 2007-12-04
7
Medium Priority
?
4,795 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 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 38

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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

715 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