I'm running the following code (on Windows 7, if it makes a difference):
char temp[20000];
sprintf_s(temp, 20000, "\"C:\\Program Files\\Mozilla Firefox\\firefox.exe\" \"http://www.tvtak.com/servlet/Gateway/?C=addShows&channel=%s&show=%s\"", _channels[chId], name);
system(temp);
On running, the console displays:
'C:\Program' is not recognized as an
internal or external command, operable
program or batch file.
'channel' is not recognized as an internal or
external command, operable program or
batch file.
'show' is not recognized
as an internal or external command,
operable program or batch file.
But when I get the value of 'temp' via QuickWatch and paste it to CMD, it works fine. What's going on here?
I should mention that the parameters I'm appending to the string contain non-latin characters. Could this have something to do with it?
You are better off with CreateProcess() to avoid cmd.exe's quoting hell. But if you must use system() you can simply append if 1==1 to the beginning of your command so it doesn't remove the quotes for you.
system("if 1==1 \"C:\Program...");
For more information about this issue, run cmd.exe /? and look for /S.
You need to put entire string into yet another pair of quotation marks. And try to avoid C-style strings in C++ code.
Related
If I have a path(string) with spaces, for example "C:\\Users\\Irina\\Desktop\\POO\\PROIECT POO\\my file.docx" and I want to open that file, I use the system command like this
system(path.c_str())
And I get the following error :
'C:\Users\Irina\Desktop\POO\PROIECT' is not recognized as an internal or external command,
operable program or batch file.
I would really appreciate some help.
Thanks :)
You need to enclose the string in double quotes:
system(('"' + path + '"').c_str());
I have a c++ program that run a command and pass some arguments to it. The code is as follow:
int RunApplication(fs::path applicationPathName,std::string arguments)
{
std::string applicationShortPath=GetShortFileName(applicationPathName);
std::string cmd="\""+applicationShortPath +"\" "+ arguments+" >>log.txt 2>&1 \"";
std::cout<<cmd<<std::endl;
int result=std::system(cmd.c_str());
return result;
}
When I run system command, the cmd window appears shortly and then closes, but the result is 1 and the cmd was not run (the command should generate output which is not generated).
To check that the cmd is correct, I stopped the application just before system line and copy/ paste cmd content to a cmd window and it worked.
I am wondering how can I find why application is not run in system()?
the cmd has this value just before running it:
"D:/DEVELO~3/x64/Debug/enfuse.exe" -w --hard-mask --exposure-weight=1 --saturation-weight=0.328 --contrast-weight=0.164 -o "C:/Users/m/AppData/Local/Temp/1.tif" "C:/Users/m/AppData/Local/Temp/1.jpg" "C:/Users/m/AppData/Local/Temp/2.jpg" >>log.txt 2>&1 "
How can I find why it is not working?
Is there any way that I set the system so it doesn't close cmd window so I can inspect it?
is there any better way to run a command on OS?
Does Boost has any solution for this?
Edit
After running it with cmd /k, I get this error message:
The input line is too long.
How can I fix it other than reducing cmd line?
There are two different things here: if you have to start a suprocess, "system" is not the best way of doing it (better to use the proper API, like CreateProcess, or a multiplatform wrapper, but avoid to go through the command interpreter, to avoid to open to potential malware injection).
But in this case system() is probably the right way to go since you in fact need the command interpreter (you cannot manage things like >>log.txt 2>&1 with only a process creation.)
The problem looks like a failure in the called program: may be the path is not correct or some of the files it has to work with are not existent or accessible with appropriate-permission and so on.
One of the firt thing to do: open a command prompt and paste the string you posted, in there. Does it run? Does it say something about any error?
Another thing to check is how escape sequence are used in C++ literals: to get a '\', you need '\\' since the first is the escape for the second (like \n, or \t etc.). Although it seems not the case, here, it is one of the most common mistakes.
Use cmd /k to keep the terminal: http://ss64.com/nt/cmd.html
Or just spawn cmd.exe instead and inspect the environment, permissions, etc. You can manually paste that command to see whether it would work from that shell. If it does, you know that paths, permssions and environment are ok, so you have some other issue on your hands (argument escaping, character encoding issues)
Check here How to execute a command and get output of command within C++ using POSIX?
Boost.Process is not official yet http://www.highscore.de/boost/process/
I have a build forge job that executes a bat file and intake password as a paramter. If i declare this paramter variable as normal text it works fine. But when I declare password as "Assing Hidden" it doesnt work and throws out error as meniotned below.
Any help is appreciated.
Command I am executing
call MpToSbx.bat SF %account% %password% %REL_NUM% %Track%
Condition
1. Works fine if password is delcared as regular text.
2. When password assigned as hidden it throws out error as below.I feels the command is not iterated correctly some how.
Error Message:
The system cannot find the path specified.
EXEC 'MpToSbx.bat' is not recognized as an internal or external command,
EXEC operable program or batch file.
The most probable cause of this error is that %password% is taken as a single word inside MpToSbx.bat file. To solve this error, first edit that file and change %2 by %~2 (to eliminate quotes in the parameter) and then enclose the password between quotes in the call; for example:
call MpToSbx.bat SF %account% "%password%" %REL_NUM% %Track%
I am getting the following error when using the system() call to run a batch file:
'C:\newfldr\mybatchfiles.bat' is not recognized as internal or external command, operable program or batch file.
error at : system("C:\newfldr\mybatchfiles.bat");
Can anyone explain why?
You very probably should write your string correctly, eg:
system("C:\\newfldr\\mybatchfile.bat");
Remember that the C compiler is interpreting back slash escapes in constant string literals. In particular \n is a newline character, and \\ encodes a backslash.
You could have printed your command string for debugging to find the issue (or use a debugger).
BTW, on Linux you don't have that issue because \ is rarely used in file paths. Did you consider trying Linux? (it is fun).
I am trying to execute a dos command from within my C++ program, however soon as I add quotes to the output filepath (of a redirection) the command no longer gets executed and returns instantly. I've shown an example below of a path without spaces, but since paths may have spaces and thus be quoted for the shell to understand it properly I need to solve this dilemma - and I'm trying to get the simplest case working first.
i.e.
The following WORKS:
sprintf(exec_cmd,"\"C:/MySQL Server 5.5/bin/mysqldump.exe\" -u%s -p%s %s > C:/backup.bak",user,password,db_name);
system(exec_cmd);
The following does NOT work (notice the quotes around the output):
sprintf(exec_cmd,"\"C:/MySQL Server 5.5/bin/mysqldump.exe\" -u%s -p%s %s > \"C:/backup.bak\"",user,password,db_name);
system(exec_cmd);
I'm guessing it is choking somewhere. I've tried the same "exec_cmd" in popen to no avail.
Any help/advice is greatly appreciated.
I don't think your shell (cmd.exe) allows redirection to a file name with spaces. I couldn't make my command.com from DOS 6.22 accept it (I don't have a cmd.exe nearby to test).
Anyway, you can use the --result-file option to pass the redirection to the command itself.
mysqldump ... --result-file="file name" ...