Multi platform way of executing a file

Hi, what's the multi platform way of executing a file? in windows I'd want it to detect what it needs to run it with (via extension name) and then execute it. So script.ps1 should be executed with powershell, script.bat with cmd, program.exe should be ran as a regular executable, etc. In unix it should do the standard thing, the equivalent of ./script.sh or ./elf-program

If I try to use

Command::new(&file).spawn();
ERROR crond: error executing "C:\\Users\\gonzalezo\\test.ps1": %1 is not a valid Win32 application. (os error 193)

After some research I've found that I have to call ShellExecute somehow.

The easiest solution would be to call start somehow. Am I in the right track?

Don't be hesitant to check for a crate if the standard library doesn't support something: Open — Rust utility // Lib.rs looks like what you want in this case.

Not convinced about that (but haven't tried it, on my phone): If it resolves to xdg-open on Linux I believe it would open a script in an editor rather than run it. Couldn't tell you about Windows or MacOS though.

2 Likes