An easy way to make the "system" command OS independent

I have an extensive script library in my workplace that uses a lot of system commands to create directories. Previously, all this work was done in Linux, so everything was hardcoded with forward slashes "/", but now we want to make it compatible with Windows.

At this point, trying to create a directory with system('mkdir ../dir')

causes an error because the Windows command line uses backslashes "\" to create directories.

Is there an easy way to tell windows to treat the hardcoded forward slashes as backslashes?

My only thing to do now is make a command if(ispc)

and set the variable to "\" or "/" respectively, but I want to know if there are alternatives.

Thank!

+3


source to share


1 answer


There is probably a way to automate the procedure, but I would recommend a one-off general solution. Unless we are talking about thousands of cases, you should be able to do it manually.



The function is a dedicated function for this purpose, it will always return the correct file separator for each OS. So just go to your files and replace everything with (probably some as well). filesep

\

' filesep '

[...]

+6


source







All Articles