Help please

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
The Great One
Posts: 23
Joined: 02 Jan 2010 20:54

Help please

#1 Post by The Great One » 02 Jan 2010 23:20

I need some help I can't get this to work. I am trying to write a batch that will check for a specific process. If the process is running it will check it again and if the process is off it will start it. Here is what i have so far but i keep getting a generic error.

@echo off

:THERE
Start chrome.exe
:HERE
tasklist /fi "STATUS eq chrome.exe" >nul
If errorlevel 1 goto THERE else goto HERE

pause

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#2 Post by avery_larry » 04 Jan 2010 15:48

What is the generic error?

viroid
Posts: 8
Joined: 04 Jan 2010 15:49

#3 Post by viroid » 04 Jan 2010 16:04

I was under the impression that if ELSE was used with IF then it needed to be formatted with parrenthesis...

IF ERRORLEVEL 1 (DO ONE) ELSE (DO TWO)

IF ERROR LEVEL 1 (
DO ONE
)
ELSE
(
DO TWO
)



Also, I've had issues using GOTO statements within if statements. I've often done something like ECHO. & GOTO HERE in order to get it to work correctly because it wouldn't work with just a GOTO HERE.

The Great One
Posts: 23
Joined: 02 Jan 2010 20:54

#4 Post by The Great One » 04 Jan 2010 18:45

Thanks for replying. I tried to use the parenthesis but it did not help. It keeps giving me infinite generic errors. I wonder if there is a different way to code it? All I want it to do is reopen a program every time it is closed.

p.s. I'm new to dos coding.

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

#5 Post by !k » 05 Jan 2010 08:01

The Great One
I have a Russian M$ "Windows", so I do not know, really need to look for "Running" or another word. Check it before use, otherwise you bury surf windows

Code: Select all

@echo off
:nono
tasklist /v /fi "imagename eq calc.exe" 2>nul |findstr "Running" >nul ||start calc.exe
goto :nono

Or, if you do not need to check the Hanging application, then simply write:

Code: Select all

@echo off
:load
tasklist |findstr "calc.exe" >nul ||start calc.exe
goto :load

viroid
Posts: 8
Joined: 04 Jan 2010 15:49

#6 Post by viroid » 05 Jan 2010 16:37

I just attempted to perform a similar function using TASKLIST, it didn't appear to return a different errorlevel when the filter found the task.

The Great One
Posts: 23
Joined: 02 Jan 2010 20:54

#7 Post by The Great One » 05 Jan 2010 16:46

Thanks for all your help guys. It works now using !k's code

@echo off
:nono
tasklist /v /fi "imagename eq calc.exe" 2>nul |findstr "Running" >nul ||start calc.exe
goto :nono

I did post another topic that has not been answered so any help with that would be appreciated too.

viroid
Posts: 8
Joined: 04 Jan 2010 15:49

#8 Post by viroid » 05 Jan 2010 17:35

I got it working using FOR ...

:LOOP

FOR /F "skip=3 usebackq tokens=1" %%i in (`TASKLIST /FI "IMAGENAME eq CALC.EXE"`) DO (
IF NOT "%%i"=="" Echo Process is Running
SLEEP 1
)

GOTO LOOP

Post Reply