How can I use a comma within a system() shell script in C++?

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

35 Replies - 4602 Views - Last Post: 10 June 2012 - 05:58 AM Rate Topic: -----

#1 computerguy3100  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 04-June 12

How can I use a comma within a system() shell script in C++?

Posted 04 June 2012 - 10:31 PM

Hello, I am trying to create an app that will let users find processes that are taking up to much memory. I am doing this with the following code:

system("tasklist /fi "status eq not responding"");


My compiler isn't liking where I put the commas in.

This is the cmd command (or shell script, whatever) that I am trying to execute: tasklist /fi "status eq not responding".

Where should I put the commas?

Is This A Good Question/Topic? 0
  • +

Replies To: How can I use a comma within a system() shell script in C++?

#2 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,163
  • Joined: 08-January 12

Re: How can I use a comma within a system() shell script in C++?

Posted 04 June 2012 - 11:17 PM

If you want to use quotes, use them in the following way :
system("tasklist /fi \"status eq not responding\"");


This is an escape sequence, telling the complier we are using " as a char.

This post has been edited by aresh: 05 June 2012 - 01:09 AM

Was This Post Helpful? 0
  • +
  • -

#3 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5182
  • View blog
  • Posts: 26,886
  • Joined: 10-May 07

Re: How can I use a comma within a system() shell script in C++?

Posted 04 June 2012 - 11:21 PM

Those are not commas, those are quotes.
Was This Post Helpful? 0
  • +
  • -

#4 computerguy3100  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 04-June 12

Re: How can I use a comma within a system() shell script in C++?

Posted 05 June 2012 - 12:06 AM

View Postaresh, on 04 June 2012 - 11:17 PM, said:

If you want to use commas, use them in the following way :
system("tasklist /fi \"status eq not responding\"");


This is an escape sequence, telling the complier we are using " as a char.


Thx, worked perfectly! And I meant to say quotes sorry wasn't thinking straight. Also, I have been trying to make a feature that allows users to find a process by name for that, I use the following code:

system("tasklist /svc /fi \"imagename eq \"")


How could I program this so that te user then types in the process name he or she wants to find?

Thx everyone, still learning C++.
Was This Post Helpful? 0
  • +
  • -

#5 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,163
  • Joined: 08-January 12

Re: How can I use a comma within a system() shell script in C++?

Posted 05 June 2012 - 01:12 AM

For that, you will have to use strings. String Concatenation. Something along the lines of
string text,process_name;
text="tasklist /svc /fi \"";
cout << "Enter process name." ;
getline(process_name);
text+=process_name;
text+=" eq \"";
system("text.c_str()); //You need a C-String, because system() does not take std::string.


Was This Post Helpful? 0
  • +
  • -

#6 computerguy3100  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 04-June 12

Re: How can I use a comma within a system() shell script in C++?

Posted 05 June 2012 - 11:20 PM

View Postaresh, on 05 June 2012 - 01:12 AM, said:

For that, you will have to use strings. String Concatenation. Something along the lines of
string text,process_name;
text="tasklist /svc /fi \"";
cout << "Enter process name." ;
getline(process_name);
text+=process_name;
text+=" eq \"";
system("text.c_str()); //You need a C-String, because system() does not take std::string.



Thank You, but when I put in the
system("text.c_str());
, my compiler won't take it. I think it might be because there is no comma at the end. Where would I put that. Also, how could I program in an option for the user to run the "taskkill -f -im" script on the process he or she typed?

THX SO MUCH!!
Was This Post Helpful? 0
  • +
  • -

#7 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,163
  • Joined: 08-January 12

Re: How can I use a comma within a system() shell script in C++?

Posted 06 June 2012 - 01:15 AM

My mistake, it should be
system(text.c_str());


Stupid quote. And well, you can something on your own, can't you ?? Try it, and if still have problems, feel free to ask more questions. But at least give it a try, armed with the knowledge which you received earlier.
Was This Post Helpful? 0
  • +
  • -

#8 computerguy3100  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 04-June 12

Re: How can I use a comma within a system() shell script in C++?

Posted 06 June 2012 - 09:54 AM

View Postaresh, on 06 June 2012 - 01:15 AM, said:

My mistake, it should be
system(text.c_str());


Stupid quote. And well, you can something on your own, can't you ?? Try it, and if still have problems, feel free to ask more questions. But at least give it a try, armed with the knowledge which you received earlier.


Thx but now it just comes up with "The search filter cannot be recognized".......
Was This Post Helpful? 0
  • +
  • -

#9 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,163
  • Joined: 08-January 12

Re: How can I use a comma within a system() shell script in C++?

Posted 06 June 2012 - 10:00 AM

How would you EXACTLY type the command in your cmd ??
Was This Post Helpful? 0
  • +
  • -

#10 computerguy3100  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 04-June 12

Re: How can I use a comma within a system() shell script in C++?

Posted 06 June 2012 - 10:07 AM

View Postaresh, on 06 June 2012 - 10:00 AM, said:

How would you EXACTLY type the command in your cmd ??


In CMD to display all running processes i would type: tasklist /fi "status eq not responding"

then to kill the process I would type: taskkill -f -im processname.exe
Was This Post Helpful? 0
  • +
  • -

#11 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,163
  • Joined: 08-January 12

Re: How can I use a comma within a system() shell script in C++?

Posted 06 June 2012 - 10:21 AM

Hmm... So let me give you an example regarding the killing part.
string text,process_name;
text="taskkill -f -im \"";
cout << "Enter process name." ;
getline(cin,process_name);
text+=process_name;
text+="\"";
system(text.c_str());


As you can see, this works perfectly (try it if you haven't done so yet) !! So, the error in first case is the structure of typing ( you can add a "cout << text ;" at the end of this code to see what exactly is being sent to CMD. You will find that is not correct in first case. So, rectify it. Change the syntax or whatever :D
Was This Post Helpful? 0
  • +
  • -

#12 computerguy3100  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 04-June 12

Re: How can I use a comma within a system() shell script in C++?

Posted 06 June 2012 - 02:06 PM

View Postaresh, on 06 June 2012 - 10:21 AM, said:

Hmm... So let me give you an example regarding the killing part.
string text,process_name;
text="taskkill -f -im \"";
cout << "Enter process name." ;
getline(cin,process_name);
text+=process_name;
text+="\"";
system(text.c_str());


As you can see, this works perfectly (try it if you haven't done so yet) !! So, the error in first case is the structure of typing ( you can add a "cout << text ;" at the end of this code to see what exactly is being sent to CMD. You will find that is not correct in first case. So, rectify it. Change the syntax or whatever :D


Thx but I just get the error "process "" not found".....
Was This Post Helpful? 0
  • +
  • -

#13 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,163
  • Joined: 08-January 12

Re: How can I use a comma within a system() shell script in C++?

Posted 06 June 2012 - 02:11 PM

Using tasklist, get the list of processes. Then, get the name of your task from there. For instance, if you use Google Chrome, open it and then run my program. Enter "chrome.exe" as program name, and it will terminate. So, the name of process has to be obtained from tasklist.
Was This Post Helpful? 0
  • +
  • -

#14 computerguy3100  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 04-June 12

Re: How can I use a comma within a system() shell script in C++?

Posted 06 June 2012 - 10:52 PM

View Postaresh, on 06 June 2012 - 02:11 PM, said:

Using tasklist, get the list of processes. Then, get the name of your task from there. For instance, if you use Google Chrome, open it and then run my program. Enter "chrome.exe" as program name, and it will terminate. So, the name of process has to be obtained from tasklist.


I have tried setting that all up this way:

	if (choice==8)
		system("color 04");
		system("CLS");
		system("tasklist");
		string text,process_name;
		text="taskkill -f -im \text.c_str())\"";
		cout << "Now type the name of a process to kill. *You will loose any unsaved data (the feature uses the taskkil -f -im command, which ends the process by force. If you are not an administrator, you may not be able to end certain processes. Last but not least, do not end a process if you don't know what it is, as you may crash the computer. You have been warned.*" << endl;
		getline(cin,process_name);
		text+="\"";
		system(text.c_str());
		system("PAUSE");
		break;


and when i try it i get "text.c_str()) process not found." any ideas?
Was This Post Helpful? 0
  • +
  • -

#15 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,163
  • Joined: 08-January 12

Re: How can I use a comma within a system() shell script in C++?

Posted 07 June 2012 - 02:09 AM

Yeah, I believe I know what the problem is. I am omitting the line 7 of your code, and this is what you are doing :
system("tasklist");
string text,process_name;
//What the hell is this initialization ?? Why are you writing text.c_str() inside quotes ?? Why the / before text.c-str() ??
text="taskkill -f -im \text.c_str())\""; 
//I think above line meant that your string currently holds the following
//taskkill -f -im [TAB] ext.c_str())"
//After taking this input, what are you doing with it ?? NOTHING !! So why ask for an input ??
getline(cin,process_name);
text+="\"";


The correct code will be
system("tasklist");
string text,process_name;
text="taskkill -f -im \""; 
getline(cin,process_name);
text+=process_name;
text+="\"";


Just do me a favour, understand what I have done, and then try to implement it. That way, you will do less mistakes. And this code works perfectly for me, so it should also work for you.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3