problem with system() command. plz help
Oct 5, 2011 at 9:03am UTC
[tt]hello
I' m new to programmming and i really need your help.
I am trying to make a programm which will request directory from user and then use that in system() command to search files.
The problem is that I can't make a console programm, use the user input for system() command.
Can someone help me plz.
Any suggestion will be appriciated.
Sry for a noob question.
Here is my code.:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#include <iostream>
#include <windows.h>
#include <string>
// Read user input and use it in system() command//
int system(const char *command);
using namespace std;
int main()
{
int x;
cout << "SPECIFY THE DISK YOU WANT TO SCAN" << endl;
cin >> x;
system("forfiles.exe /p " + x:\\" /s /m *.pdf" );
return 0;
}
Last edited on Oct 5, 2011 at 9:04am UTC
Oct 5, 2011 at 12:51pm UTC
A couple of things...
Rather than store the input as an int, why not a char?
Also, using a string to build your system "command line" may make it easier to see what you're doing. Have a look:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#include <iostream>
#include <windows.h>
#include <string>
// Read user input and use it in system() command//
int system(const char *command);
using namespace std;
int main()
{
char x;
cout << "SPECIFY THE DISK YOU WANT TO SCAN" << endl;
cin >> x;
string cmdLine = "forfiles.exe /p " ;
cmdLine += x;
cmdLine += ":\\ /s /m *.pdf" ;
system(cmdLine.c_str());
return 0;
}
Oct 5, 2011 at 2:09pm UTC
It worked!!!
Thank you so much my friend. you helped me alot :)
Topic archived. No new replies allowed.