Batch Task Automation

August 24, 2009 at 01:31:06
Specs: Windows XP
I need to create a batch file that will run an exe that will test out the readability of a CD. The process is supposed to work out as follows:

1. The PC & OS loads up, and the CD checking .exe will automatically execute
2. Another batch file will need to check whether or not this program is still executing, say, every 25 seconds - it will do this by creating a text file that lists all currently running processes and searching through the list to see if the process is present

3. If the process is present, the program will loop to the beginning and wait again, and check again
4. If the process isn't present, then the PC will restart and the process will start all over again.

My code is as follows:

set tempfile=process.txt
tasklist > %tempfile%
tasklist /fi "readtest.exe"
if errorlevel 0 if not errorlevel 1 goto IsRunning
echo This process is running
goto exit
del %tempfile%

The problem I am having is that the program is loading OK, but the text file isn't getting created, and so the checking on whether the process is still active is therefore also malfunctioning.

Any help would be hugely appreciated as I have been trying to get this to work for too long!

See More: Batch Task Automation

Report •

August 24, 2009 at 06:24:23
If you did something like this, it would bypass the need to have a text file, All you have to do is replace readtest.exe with the name of you CD checking program, then every 25 seconds it will check to see if that program is running, if not it will give the user 5 seconds before restarting, the only way to cancel the restart would be in that 5 second window the user would have to open a command prompt and type "shutdown /a" to abort the shutdown process.

echo off


tasklist | find /i "readtest.exe" >nul

if %errorlevel% EQU 0 (
echo:CD Checking service running...
ping -n 25 >nul
goto loop

shutdown /r /f /t 5 /c "System rebooting to re-start CD Checking service."

Report •

August 24, 2009 at 20:01:48
Here is another way to do it, this way actually writes to a textfile that is stored in the your temp directory while it is being used. In you example above when I run it on my computer the first thing I get is an error 'cuase wait is not a valid command then it creates the textfile, then I'm not sure what you have going on with the next command.
So if you want it to run without actually creating a textfile, use the commands in response 2, if you actually need it to read from a textfile, i.e. a homework assignment, here you go,

@echo off

REM Change the Process to the one you want to look for.
set process=readtest.exe

tasklist > "%temp%\process.txt"
for /f "usebackq" %%a in ("%temp%\process.txt") do (
if %%a equ %process% (
echo %%a is running...
ping -n 25 >nul
goto begin
del /q "%temp%\process.txt"
shutdown /r /f /t 5 /c "System rebooting to re-start %process%."

Report •

August 25, 2009 at 03:30:19
Thanks for your replies.
The code for the inclusion of the text file is giving me an error - on the command line it's saying '/f "usebackq" was unexpected at this time'
Any ideas on why this is so?

Report •

Related Solutions

August 25, 2009 at 05:07:26
Check to make sure that all of the spacing is there, double check to make sure that there is a space between /f and "usebackq"

Report •

Ask Question