Jump to content

Simple two program start batch file and task manager


Drivejunky95

Recommended Posts

Hi all

 

I am new (today) to machine script. This is something I put together and will be trying tomorrow.

 

 

cd /d "C:\path"
Start Filename.bat

cd /d "H:\path"

Start Program.exe

 

:CHECK
ping -n 10 localhost 1> NUL
TASKLIST /v /fi "IMAGENAME eq program.exe" 2>&1 > NUL
IF ERRORLEVEL 1 GOTO CHECK

 

taskkill /f /im cmd.exe /t

 

 

If all goes well, filename.bat will run. Then a program will run. CMD will then check for program.exe until not found (exited), then close CMD.

 

In this case program.exe does not work with start /wait

 

I am wondering if the check will slow down my machine if run for a few hours.

 

I am using Windows 7 x64, SSD, 16GB RAM and AMD 8350.

 

Considering inserting a small timeout, but seems silly.

 

Is there a way to slow down the check, but still close immediately if program.exe is closed?

Link to comment
Share on other sites


Side notes.

Always use a name or just a pair of double quotes when using the START command.

http://ss64.com/nt/start.html

You cannot really use TASKLIST like that, as it doesn't set an ERRORLEVEL, see:

http://stackoverflow.com/questions/4969595/exit-status-of-tasklist-in-batch-file

http://stackoverflow.com/questions/162291/how-to-check-if-a-process-is-running-via-a-batch-script

I am not sure to understand the original idea, can you provide some details on what you actually *need* or *want* to do?

jaclaz

Link to comment
Share on other sites

No problem:)

 

Filename.bat is a looping xcopy /d with 5min timeout

 

Program.exe is an application that makes frequent saves

 

I need something that will start the .bat and the program, then close the .bat when the program is closed.

Edited by Drivejunky95
Link to comment
Share on other sites

I still have no understanding of the final GOAL. :blushing:

 

I was expecting a description of the expected result or scope and - unless of course it is a trade secret of some kind - the contents of the batch and what is the actual "program.exe" would be another piece of useful info (as I am completely failing to imagine an executable that would not work with "START /W" :unsure: ).

 

You need to be aware of the risk :ph34r: of (possibly) slipping on a chocolate covered banana :w00t: or more generally fall in the XYZ problem:

http://homepage.ntlworld.com/jonathan.deboynepollard/FGA/put-down-the-chocolate-covered-banana.html

 

jaclaz

Link to comment
Share on other sites

Ok,

 

When I start program.exe I also want a looping batch file to start (see Filename.bat). This will save new files from program.exe to another directory.

 

What isn't happening is when I close program.exe, the looping batch file is not closing, hence the **taskkill /f /im cmd.exe /t**

 

I think I have narrowed it down some.

 

Filename.bat

@echo off
:loop
xcopy /y "C:\path" "H:\path" /d
timeout /t 20
goto loop

 

So using:

 

Example.bat

@echo off

cd /d "H:\path"
program.exe

cd /d "C:\path"
call Filename.bat

 

:CHECK

timeout /t 10> nul
TaskList /nh /fi "IMAGENAME eq program.exe" | find "program.exe" 2>&1 > nul
IF ERRORLEVEL 1 GOTO CHECK

 

taskkill /f /im cmd.exe /t

 

Works, but prevents the CHECK due to no return of control from the looping Filename.bat

Edited by Drivejunky95
Link to comment
Share on other sites

But why the two batches?

 

I mean (IF I have understood the issue) wouldn't this single batch do nicely? :unsure:

@echo offcd /d "H:\path"program.execd /d "C:\path":loopxcopy /y "C:\path" "H:\path" /dtimeout /t 20:CHECKTaskList /nh /fi "IMAGENAME eq program.exe" | find "program.exe" 2>&1 > nulIF %ERRORLEVEL% NEQ 0 GOTO :looptaskkill /f /im cmd.exe /t

The ERRORLEVEL syntax has been modified, though the one you used is fine, because of this (it's only a habit, not really-really needed in most cases):

http://www.robvanderwoude.com/errorlevel.php

 

jaclaz

Link to comment
Share on other sites

Almost works,

 

Example.bat

@echo off
 
cd /d H:\path
Start /b program.exe
 
cd /d C:\path
call Filename.bat
 
And inside
 
Filename.bat
@echo off
:loop
timeout /t 10 > NUL
xcopy /y "C:\path" "H:\path" /d
TaskList /nh /fi "IMAGENAME eq program.exe" | find "program.exe" 2>&1 > nul
IF ERRORLEVEL 1 goto loop
 
exit
 
This is working, but the looping error level section is exiting while program.exe is still open
Link to comment
Share on other sites

Awesome man! You killed it!:)

 

Set the NEQ error level to 1 and "IF ERRORLEVEL NEQ" fixed it! 

 

Just for giggles I tried it in the original script and it worked!

 

So that means using the ERRORLEVEL syntax like this in Windows 7 returns negative values!:)

Link to comment
Share on other sites

Awesome man! You killed it! :)

 

Set the NEQ error level to 1 and "IF ERRORLEVEL NEQ" fixed it! 

 

Just for giggles I tried it in the original script and it worked!

 

So that means using the ERRORLEVEL syntax like this in Windows 7 returns negative values! :)

Good :thumbup , I guess that which ERRORLEVEL is set should be verified in the two cases (when the FIND actually finds the program.exe and when it doesn't) but the main thing is that it works :).

 

jaclaz

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...