MATLAB Answers

Problem running an exe with path containing spaces, while saving command output

28 views (last 30 days)
Hello,
I have a weird problem in MATLAB. I'm trying to run an exe, say test.exe. This is in a folder/directory which has spaces in the name. C:\users\abc\test folder\test.exe. I'm saving the path to a variable so I don't have to type it out everytime, and the code is cleaner.
I see a very weird problem. The space in the path causes a problem only if I have to save the command output. Interestingly, the problem doesn't occur if I run the exe without any arguments. In this case the cmdout is properly logged (like most exes, this exe gives usage instructions when you run it without any arguments)
exe_path = 'C:\users\abc\test folder\test.exe';
% No output saving - no problem
system([exe_path,' arg1'])
>> NO PROBLEM
% Output saving - problem
[status, cmdout] = system([exe_path,' arg1'])
>> ''C:\users\abc\test' is not recognized as an internal or external command, operable program or batch file'
% Interesting twist - no arg1 works fine
[status, cmdout] = system(exe_path)
>> NO PROBLEM
I've tried double quotes, single quotes, single and double quotes (based on some other answers), but I can't fix this isssue. The simple solution would be to move the exe or rename the folder, but this is a common install path for anyone in my ecosystem installing the tool and there are a lot of dependent dlls that the exe uses. Moving all this to a new folder will place a lot of dependencies when I will have to distribute the utility I am trying to create in MATLAB.
Let me know if there is a fix for the above problem.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 25 Feb 2021
Specify double-quotes at each end of the path, e.g.:
exe_path = '"C:\users\abc\test folder\test.exe"';

More Answers (0)

Products


Release

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by