Jump to content

IF statement batch files. Help needed


linux_n0ob

Recommended Posts

I am using

findstr

to see if a word appears in a text file, if it does i want to taskkill a specific task. How do you write a if statement for that. example ... I am looking for firefox.exe in tasklist.txt, so i'd use

findstr "firefox.exe" tasklist.txt

if it exists than i want it to automatically

taskkill firefox.exe

  I can't figure out how to right this as an

if

statement in the command prompt

Link to comment
Share on other sites

I am using
findstr

to see if a word appears in a text file, if it does i want to taskkill a specific task. How do you write a if statement for that. example ... I am looking for firefox.exe in tasklist.txt, so i'd use

findstr "firefox.exe" tasklist.txt

if it exists than i want it to automatically

taskkill firefox.exe

  I can't figure out how to right this as an

if

statement in the command prompt

You seem to need alot of help with batch files. Let me find you some tutorials.

http://www.chebucto.ns.ca/~ak621/DOS/BatBasic.html

http://www.tnd.com/camosun/elex130/dosbatchtutor1.html

http://home7.inet.tele.dk/batfiles/batfiles.htm

http://www.techtutorials.info/dbatch.html

http://www.instructables.com/id/Batch-Tutorial---Take-1/

http://furies.senecac.on.ca/~cpang/ios100/dosbatch.html

http://blog.satyr.nl/post/ms_dosmsdos_batc...l_and_reference

http://www.astahost.com/info.php/batch-files_t2149.html

Link to comment
Share on other sites

@echo off

findstr /i "firefox.exe" tasklist.txt

if errorlevel 0 taskkill /im firefox.exe

"if errorlevel 0 taskkill /im firefox.exe" written this way will execute no matter what you have before it. It isn't checking anything and even on a line by itself will try to run if its th eonly thing in the batch script.

@echo  :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo  Simple Batch Script to search for task and kill it if it exists
@echo  This was written to work on XP (or NT)
@echo  Standard DOS lets you SET ERROR LEVEL but apperntly NT does not 
@echo  do it this way? Don't Quote me on that. Anyway, here is a 
@echo  quick script to show what happens
@echo  when it catches the error in section one, and then when it is 
@echo  successfull, it will complete the task, in the 'retry' section.
@echo  This is just for demonstrations.
@echo  12/23/2007 - DigiP
@echo  :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
pause
cls
::tasklist > tasklist.txt
::Give us an error level check from get go just for demonstrations.
::    ECHO ERRORLEVEL = %ERRORLEVEL%
@echo off
del tasklist.txt
findstr /C:"notepad.exe" tasklist.txt 
ECHO ERRORLEVEL = %ERRORLEVEL%

:: %ERRORLEVEL% ==1 means I can not find string I am searching for
:: In actuality, it means if 1 == 1 then error out.

IF %ERRORLEVEL% == 1 goto:poo
If %ERRORLEVEL% == 0 taskkill /F /IM notepad.exe
ECHO %ERRORLEVEL%

::above for testing and demonstrations::

:retry
@echo Now lets try it with a field to search against.
@echo WARNING: I will continue to loop forever until 
@echo you open the program you are searching for!
@echo For this reason, there is a pause on the next line.
@echo Use CTRL-C to cancel the program from an infinite loop.
@echo Do not remove the pause and leave unattended or you risk
@echo crashing or using 100% cpu cycles.
pause
cls
tasklist > tasklist.txt
findstr /C:"notepad.exe" tasklist.txt 
ECHO ERRORLEVEL = %ERRORLEVEL%

:: %ERRORLEVEL% ==1 means I can not find string I am searching for

IF %ERRORLEVEL% == 1 goto:poo
IF %ERRORLEVEL% == 0 taskkill /F /IM notepad.exe

@echo Hooray, we found it, now lets get the hell out of here!
pause
exit

:poo
@echo String Not Found!
:: Now lets do it with a file to search against! 
:: Warning, this will just loop if it can not find notepad open! Re write the script to end(EXIT)
:: if not found! 
::
:: This script was just written to illustrate simple bat scripting! Not as a fully funtional script. 
:: It can be modified to do more and complete clean on exit. I will might also add the full clean version 
:: in another post but this scripts is self explanatory!
goto:retry

This is just an example script I whipped up. Nothing special, but it gets the job done quick and dirty. Read through all my comments for warnings!

Link to comment
Share on other sites

A cleaner version:

@echo off
:: tasklist > tasklist.txt overwrites file
:: tasklist >> tasklist.txt appends the file. We do not want dupes so use > not >>
tasklist > tasklist.txt
findstr /IC:"notepad.exe" tasklist.txt 
cls
:: %ERRORLEVEL%==1 means I can not find string I am searching for
IF %ERRORLEVEL%==1 goto:poo
IF %ERRORLEVEL%==0 echo Hooray, we found it. Now lets kill it and end the script!
pause
TASKKILL /F /IM NOTEPAD.EXE
::Clean up files
del tasklist.txt
goto:bottom
:poo
@echo Task not found!
::Clean up files
del tasklist.txt
::Only needed when run from a command prompt in a cmd window,
::uncomment the next line if you just want to exit all together when done.
::exit 
pause
:bottom
echo Done.
::Only needed when run from a command prompt in a cmd window,
::uncomment the next line if you just want to exit all together when done.
::exit

Link to comment
Share on other sites

You seem to need alot of help with batch files. Let me find you some tutorials.

gee... I wonder if that's why he's asking for help...

One of the ways I learned was by actually looking at working code and the comments in the code. Seeing what it does always helped me. Although, it would be better to see if he could have posted his progress in the event that he learned anything at all....

Link to comment
Share on other sites

@echo off
tasklist | findstr /i "calc.exe"
goto :%errorlevel%
:0
taskkill /im calc.exe && goto :eof
:1
echo Process not found

That's even better. I didn't even think to pipe tasklist directly into findstr. :)

I know mine looks like a mess but I try to leave as many comments so people will understand the reason behind the process...Great code by the way.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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