SoftwareTipsandTricks Forum

Go Back   SoftwareTipsandTricks Forum > Software > Software Problems and Useful Utilities
User Name
Password


Need help creating a batch file.

Reply
 
Thread Tools Search this Thread Rate Thread Display Modes

  #1  
Old 11-18-2005, 06:38 PM
brock.travis Offline
Registered User
 
Join Date: Nov 2005
Posts: 7
Question Need help creating a batch file.

I'm trying to create a batch file that will terminate images running in the tasklist other than the ones I want running. In other words, I have a list of image names, and I want to TASKKILL all other image names that are not in my list.

I have fooled around with IF statements and such and I just cant get the coding down to make if function the way I would like.

One thing I do not want to have to do is create a batch file that TASKKILL's the images not on my list one by one. If I did that I would constantly have to edit the batch file to add new image names as I installed and uninstalled software and programs. Thanks.

- Travis
Reply With Quote

  #2  
Old 12-01-2005, 10:54 AM
brock.travis Offline
Registered User
 
Join Date: Nov 2005
Posts: 7
Exclamation

bump
Reply With Quote

  #3  
Old 12-01-2005, 07:26 PM
Cache's Avatar
Cache Offline
ST&T Secret Police
 
Join Date: Jun 2004
Location: UK
Posts: 616
Do you know how use a C++ compiler? I could probably make some C++ code to do what you ask. Then you could compile it yourself if you like.

One thing to ask first though, is why do you want to do this. If you want to play games or something that requires alot of system resources you can perform a "Clean Boot" by using MSCONFIG.exe.

Take a look at the link below for more info on perfomting a clean boot:

http://support.microsoft.com/?scid=kb;en-us;331796
Reply With Quote

  #4  
Old 12-01-2005, 10:28 PM
brock.travis Offline
Registered User
 
Join Date: Nov 2005
Posts: 7
Quote:
Originally Posted by Cache
Do you know how use a C++ compiler? I could probably make some C++ code to do what you ask. Then you could compile it yourself if you like.

One thing to ask first though, is why do you want to do this. If you want to play games or something that requires alot of system resources you can perform a "Clean Boot" by using MSCONFIG.exe.

Take a look at the link below for more info on perfomting a clean boot:

http://support.microsoft.com/?scid=kb;en-us;331796

Yeah I knew about msconfig but I only want to turn all those services off right before I load Battlefield2 to play. But I want those services running when I'm not playing. I dont really want to edit msconfig every time. I have visual studio so if you wouldnt mind laying it out for me that would be great.
Reply With Quote

  #5  
Old 12-04-2005, 08:14 AM
Cache's Avatar
Cache Offline
ST&T Secret Police
 
Join Date: Jun 2004
Location: UK
Posts: 616
Quote:
Originally Posted by brock.travis
Yeah I knew about msconfig but I only want to turn all those services off right before I load Battlefield2 to play. But I want those services running when I'm not playing. I dont really want to edit msconfig every time. I have visual studio so if you wouldnt mind laying it out for me that would be great.

You want to stop services too? I'm seeing a huge problem with this. You can't just forcibly termiate some processes or services without causing Windows to auto-reboot. msconfig, although may seem a pain to change each time, is by far the best way.

Are you sure you still wan't to do this?
Reply With Quote

  #6  
Old 12-04-2005, 06:06 PM
brock.travis Offline
Registered User
 
Join Date: Nov 2005
Posts: 7
Quote:
Originally Posted by Cache
You want to stop services too? I'm seeing a huge problem with this. You can't just forcibly termiate some processes or services without causing Windows to auto-reboot. msconfig, although may seem a pain to change each time, is by far the best way.

Are you sure you still wan't to do this?

Services have image names too. I do have the list of required services so I will not be terminating anything that would cause my computer to reboot or not function properly. The command in CMD is TASKKILL [IMAGENAME] /f (Force terminate). The /f trigger gives the operating system no option, it will close the targeted image name even if it create undesired results.

My only problem is creating a batchfile in which I can list the imagenames (system, system idle process, svchost, explorer.exe, etc.) that I wish not to terminate by means of using IF statements or what not.

EX.

IF IMAGENAME eq System Idle Process
return false
ELSE IF IMAGENAME eq System
return false
ELSE IF IMAGENAME eq smss.exe
return false
ELSE IF IMAGENAME eq csrss.exe
return false
ELSE IF IMAGENAME eq winlogon.exe
return false
ELSE IF IMAGENAME eq services.exe
return false
ELSE IF IMAGENAME eq lsass.exe
return false
ELSE IF IMAGENAME eq svchost.exe
return false
ELSE IF IMAGENAME eq spoolsv.exe
return false
ELSE IF IMAGENAME eq explorer.exe
return false
ELSE IF IMAGENAME eq rundll32.exe
return false
ELSE IF IMAGENAME eq nvsvc32.exe
return false
ELSE IF IMAGENAME eq alg.exe
return false
ELSE IF IMAGENAME eq wscntfy.exe
return false
ELSE IF IMAGENAME eq cmd.exe
return false
ELSE IF IMAGENAME eq wuauclt.exe
return false
ELSE IF IMAGENAME eq tasklist.exe
return false
ELSE IF IMAGENAME eq wmiprvse.exe
return false
ELSE TASKKILL /F /FI "IMAGENAME"

Although this does not actualy function, it was be best way for me to explain what I am trying to do.
Reply With Quote

  #7  
Old 12-04-2005, 06:17 PM
Cache's Avatar
Cache Offline
ST&T Secret Police
 
Join Date: Jun 2004
Location: UK
Posts: 616
Taskkill /IM %imagename% /F

But yes I know what you want. I'll see what I can do, check back tomorrow.

Basically I'll just use C++ as a sort of extended batch file. What I'm thinking is:

Read from a file containing the white list.
Redirect TASKLIST to a text file.
Read from the TASKLIST text file, and compare each imagename from that file with the white list. If no match is found, TASKKILL the imagename.
Reply With Quote

  #8  
Old 12-04-2005, 06:21 PM
brock.travis Offline
Registered User
 
Join Date: Nov 2005
Posts: 7
Quote:
Originally Posted by Cache
Taskkill /IM %imagename% /F

But yes I know what you want. I'll see what I can do, check back tomorrow.

Basically I'll just use C++ as a sort of extended batch file. What I'm thinking is:

Read from a file containing the white list.
Redirect TASKLIST to a text file.
Read from the TASKLIST text file, and compare each imagename from that file with the white list. If no match is found, TASKKILL the imagename.

Awesome, sounds great and thanks for your help.
Reply With Quote

  #9  
Old 12-05-2005, 08:03 PM
Cache's Avatar
Cache Offline
ST&T Secret Police
 
Join Date: Jun 2004
Location: UK
Posts: 616
OK, here it is, in all it's nastiness lol.

1. Compile the code as a C++ Console application.

2. Create two text files (.txt), one named "white_list.txt" and the other "srv.txt" (without the quotes). These must be in the same direcotry as the program.

3. Place all the image names for processes you DO NOT want to kill in the file "white_list.txt". The format should be:

System Idle Process
System
smss.exe
.ect

4. Place all the internal names of the services you DO want to kill in the file named "srv.txt". The format should be the same as above. You can find the internal names from the following link:
http://www.ss64.com/ntsyntax/services.html

ex:

Alerter
sysmonLog
ALG
.ect

When you open you program you can either 'TASKKILL' processes/services or 'START' them. Don't delete the file 'restart_proc.dat', it will be deleted automatically after you select the START option.

This is about as rough as it gets, so if you have any problems just let me know.

Code:
#include <iostream> #include <string> #include <fstream> #include <vector> using namespace std; int main(int argc, char *argv[]) { vector<string> white_list; vector<string> task_list; string choice; cout << "\n1: Taskkill\n"; cout << "2: Start"; cout << "\n\nCHOICE: "; cin.sync(); cin >> choice; cout << endl; if (choice == "1") { system("TASKLIST > tasklist.txt"); string pre; ifstream in; in.open("white_list.txt"); if (in.is_open()) { while (getline(in, pre)) { if (pre != "\0") { white_list.push_back(pre); } } in.close(); } else if (! in.is_open()) { cout << "\n" << "Error: file not found \"white_list.txt\"\n" << flush; system("PAUSE"); return 1; } string t_pre; ifstream t_in; t_in.open("tasklist.txt"); if (t_in.is_open()) { while (getline(t_in, t_pre)) { if ((t_pre.substr(0,10) != "Image Name") && (t_pre.substr(0,10) != "==========") && (t_pre != "\0")) { task_list.push_back(t_pre.substr(0,25)); } } t_in.close(); } else if (! t_in.is_open()) { cout << "\n" << "Error: file not found \"tasklist.txt\"\n" << flush; system("PAUSE"); return 1; } system("@echo off"); system("DEL /F /Q tasklist.txt"); system("@echo on"); for (int i = 0; i < task_list.size(); i++) { for (int j = 0; j < white_list.size(); j++) { string::size_type pos = task_list[i].find(white_list[j],0); if (pos != string::npos) { task_list[i] = "\0"; break; } } } ofstream restart; restart.open("restart_proc.dat"); string command; for (int k = 0; k < task_list.size(); k++) { if (task_list[k] != "\0") { restart << task_list[k] << "\n"; command = "TASKKILL /F /IM " + task_list[k]; system(command.c_str()); } } restart.close(); vector<string> services; string s_pre; ifstream s_in; s_in.open("srv.txt"); if (s_in.is_open()) { while (getline(s_in, s_pre)) { if (s_pre != "\0") { services.push_back(s_pre); } } s_in.close(); } else if (! s_in.is_open()) { cout << "\n" << "Error: file not found \"srv.txt\"\n" << flush; system("PAUSE"); return 1; } for (int l = 0; l < services.size(); l++) { if (services[l] != "\0") { command = "SC stop " + services[l]; system(command.c_str()); } } } else if (choice == "2") { vector<string> restart; string r_pre; ifstream r_in; r_in.open("restart_proc.dat"); if (r_in.is_open()) { while (getline(r_in, r_pre)) { if (r_pre != "\0") { restart.push_back(r_pre); } } r_in.close(); } else if (! r_in.is_open()) { cout << "\n" << "Error: file not found \"restart_proc.dat\"\n" << flush; system("PAUSE"); cout << endl; } string command; for (int i = 0; i < restart.size(); i++) { if (restart[i] != "\0") { command = "START " + restart[i]; system(command.c_str()); } } system("@echo off"); system("DEL /F /Q restart_proc.dat"); system("@echo on"); vector<string> restart_srv; string s_pre; ifstream s_in; s_in.open("srv.txt"); if (s_in.is_open()) { while (getline(s_in, s_pre)) { if (s_pre != "\0") { restart_srv.push_back(s_pre); } } s_in.close(); } else if (! s_in.is_open()) { cout << "\n" << "Error: file not found \"srv.txt\"\n" << flush; system("PAUSE"); cout << endl; } for (int j = 0; j < restart_srv.size(); j++) { if (restart_srv[j] != "\0") { command = "SC start " + restart_srv[j]; system(command.c_str()); } } } system("PAUSE"); return EXIT_SUCCESS; }

Last edited by Cache : 12-05-2005 at 08:15 PM.
Reply With Quote

  #10  
Old 12-08-2005, 04:51 PM
Cache's Avatar
Cache Offline
ST&T Secret Police
 
Join Date: Jun 2004
Location: UK
Posts: 616
What a effin' piss take! You not even going to post back saying if this works for you?

Tell you what, I wont be doing anything like this for people in such a rush anymore.
Reply With Quote

  #11  
Old 12-09-2005, 03:08 PM
brock.travis Offline
Registered User
 
Join Date: Nov 2005
Posts: 7
Quote:
Originally Posted by Cache
What a effin' piss take! You not even going to post back saying if this works for you?

Tell you what, I wont be doing anything like this for people in such a rush anymore.

Hey sorry man. It’s finals week. I've been trying to get visual studio to install so I could compile it but it keeps asking me for some prerequisite disk. So I haven’t even compiled the code yet. I was gunna wait to post till I could compile it.

And who said i was in a rush. My first post was November 18th.
Reply With Quote

  #12  
Old 12-09-2005, 03:18 PM
Cache's Avatar
Cache Offline
ST&T Secret Police
 
Join Date: Jun 2004
Location: UK
Posts: 616
Ooops

Maybe I should wait a bit longer before I open my mouth in future. It's just people come on here asking for help, then you try to help them and they never get back. Normally that doesn't bother me at all-- it's just this is slightly more than just posting a link, or whatever. I thought you had left this thread for dead. Sorry about that.

You could try downloading and installing Dev-C++. It's realy easy to use. If you can't figure it out just post back.

If you want Dev-C++ just pick a download location from the link below:
http://prdownloads.sourceforge.net/d....9.2_setup.exe
Reply With Quote

  #13  
Old 12-09-2005, 11:16 PM
brock.travis Offline
Registered User
 
Join Date: Nov 2005
Posts: 7
Well it didnt work. It didnt taskkill anything. But i didnt put anything in the srv.txt file. does that have anything to do with the taskkill function?
Reply With Quote

  #14  
Old 12-10-2005, 05:16 AM
ghostdog Offline
Registered User
 
Join Date: Dec 2005
Posts: 4
Quote:
Originally Posted by brock.travis
Well it didnt work. It didnt taskkill anything. But i didnt put anything in the srv.txt file. does that have anything to do with the taskkill function?

i used perl to do this. Donno whether it's what you want.

$currenttaskfile = "tasks.txt"; #store current tasks

my $cmd = q(tasklist /NH > $currenttaskfile);
system($cmd); #tasks.txt is created.


open(FILE, "$currenttaskfile") or die "Cannot open $currenttaskfile: $!";
while ( my $line = <FILE>) {
next if $line =~ /^$/;
next if $line =~ /System Idle Process|System|smss.exe|csrss.exe|winlogon.exe|ser vices.exe|lsass.exe/;
next if $line =~ /svchost.exe|spoolsv.exe|explorer.exe|rundll32.exe| nvsvc32.exe|alg.exe|wscntfy.exe/;
next if $line =~ /cmd.exe|wuauclt.exe|tasklist.exe|wmiprvse.exe/;
my @image = split(/\s+/, $line) ;
$image2kill = $image[0];
$tokillcmd = qq(TASKKILL /F /IM $image2kill);
#print $tokillcmd . "\n";
system($tokillcmd); #actual killing.
}

close(FILE);
Reply With Quote

  #15  
Old 12-10-2005, 05:28 AM
sourcez Offline
Registered User
 
Join Date: Feb 2005
Location: UK
Posts: 49
Send a message via MSN to sourcez
note what Cache said:

"Place all the internal names of the services you DO want to kill in the file named "srv.txt". "

so nothing will happen UNLESS you put the names of all the processes u want to kill in that file.

To cache - thanks for the piece, im will be using it myself to close apps before opeining BF2 :P

Regards
__________________
Am i not answering? Send me a PM!
Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
nero startsmart problem stoo999 Hardware Problems 7 08-09-2006 12:20 PM
Batch file to move a file WorldBuilder Software Problems and Useful Utilities 1 11-03-2005 06:22 AM
A little help needed cdb252 Windows XP 0 06-23-2005 02:15 PM
rewriter not working! skoundrel Windows XP 6 05-29-2005 02:31 PM
Deleting a file using a batch file in Windows scheduler JP-D Windows NT/2000/2003 2 05-01-2005 03:07 PM



All times are GMT -5. The time now is 12:05 AM.


Designed by eXtremepixels. Powered by vBulletin Version 3.5.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
SEO by vBSEO 2.3.2 © 2005, Crawlability, Inc.