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

• (3 Pages)
• 1
• 2
• 3

## 35 Replies - 7670 Views - Last Post: 10 June 2012 - 05:58 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: '//www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=281769&amp;s=c0a1c8a23ac2097c43a22dba950c96f0&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 computerguy3100

• New D.I.C Head

Reputation: 0
• Posts: 49
• 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

• It's a 16-Bit World!

Reputation: 273
• Posts: 4,258
• 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

### #3 no2pencil

• Professor Snuggly Pants

Reputation: 6500
• Posts: 30,458
• 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.

### #4 computerguy3100

• New D.I.C Head

Reputation: 0
• Posts: 49
• 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

aresh, 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++.

### #5 aresh

• It's a 16-Bit World!

Reputation: 273
• Posts: 4,258
• 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.



### #6 computerguy3100

• New D.I.C Head

Reputation: 0
• Posts: 49
• 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

aresh, 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!!

### #7 aresh

• It's a 16-Bit World!

Reputation: 273
• Posts: 4,258
• 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.

### #8 computerguy3100

• New D.I.C Head

Reputation: 0
• Posts: 49
• 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

aresh, 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".......

### #9 aresh

• It's a 16-Bit World!

Reputation: 273
• Posts: 4,258
• 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 ??

### #10 computerguy3100

• New D.I.C Head

Reputation: 0
• Posts: 49
• 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

aresh, 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

### #11 aresh

• It's a 16-Bit World!

Reputation: 273
• Posts: 4,258
• 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

### #12 computerguy3100

• New D.I.C Head

Reputation: 0
• Posts: 49
• 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

aresh, 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

Thx but I just get the error "process "" not found".....

### #13 aresh

• It's a 16-Bit World!

Reputation: 273
• Posts: 4,258
• 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.

### #14 computerguy3100

• New D.I.C Head

Reputation: 0
• Posts: 49
• 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

aresh, 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");
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?

### #15 aresh

• It's a 16-Bit World!

Reputation: 273
• Posts: 4,258
• 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.