Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

windows C system call with spaces in command

I cannot make system calls with spaces in the names and parameters. For example:

system("c:\\program files\\something\\example.exe c:\\my files\\example.txt");

I have tried escaping in every way I know how, and NOTHING works. I have tried:

system("\"c:\\program files\\something\\example.exe\" \"c:\\my files\\example.txt\"");

and

system("c:\\program^ files\\something\\example.exe c:\\my^ files\\example.txt");

Neither work. I still get 'c:\program' is not a recongnised internal or external command

This is really driving me mad... I need to call and pass parameters that have spaces in them. I cannot use the short notation for reasons I won't go into.

I have tried with ' quotes instead of " quotes, still doesn't work. I have tried putting quotes around the whole thing and quotes around the spaces and that doesn't work.

Does anyone know how to do it properly?

like image 238
myforwik Avatar asked Apr 15 '10 03:04

myforwik


People also ask

How do you pass a path with spaces in CMD?

Use quotation marks when specifying long filenames or paths with spaces. For example, typing the copy c:\my file name d:\my new file name command at the command prompt results in the following error message: The system cannot find the file specified. The quotation marks must be used.

Can you have spaces in CMD?

Command-line environments like the Windows Command Prompt and PowerShell use spaces to separate commands and arguments—but file and folder names can also contain spaces. To specify a file path with a space inside it, you'll need to “escape” it.

What is a space in command line?

The command line uses the space character to separate the name of the file being renamed from the new name of the file. When a file contains spaces, it must be surrounded in quotation marks to ensure the proper file is renamed. Otherwise, the command line interprets the words following each space as new file names.

How do I run an EXE from command prompt?

Type "start [filename.exe]" into Command Prompt, replacing "filename" with the name of your selected file. Replace "[filename.exe]" with your program's name. This allows you to run your program from the file path.


1 Answers

Edit: OK, figured it out by stepping into the system() call: you need an extra set of quotes around the whole thing due the fact that it internally calls cmd /c yourinput

So this works for me:

system("\"\"c:\\program files\\internet explorer\\iexplore.exe\" \"www.stackoverflow.com\"\"");

A bit of a mess, isn't it?

like image 130
EMP Avatar answered Oct 01 '22 22:10

EMP