9
$\begingroup$

I'm trying to use the Win7 command prompt from a Mathematica notebook.

I'd like to do a couple simple things, but I can't seem to get the syntax straight:

  • open explorer.exe in a specific location
  • open a command prompt and run several commands

The Wolfram tutorial on external programs says:

Note that when you use Run, you must not preface commands with exclamation points. Run simply takes the textual forms of the arguments you specify, then joins them together with spaces in between, and executes the resulting string as an external shell command.

This seems to work as it should:

Run["C:\\Windows\\explorer.exe"]

But this snippet seems to ignore the additional parameters when I call it from Mathematica:

Module[{cmd="C:\\Windows\\explorer.exe /e, & \"" <> $UserAddOnsDirectory <> "\""},
  Print@cmd;
  Run[cmd]]

even though the syntax as displayed by the Print statement is correct syntax and works as expected from the OS command line.

$\endgroup$
  • 2
    $\begingroup$ I recommend using RunProcess (and related StartProcess, KillProcess), much easier to set up complicated command chains. It constructs the actual command to conform to the actual platform it is run on, concerning paths, spaces, escape characters, etc. $\endgroup$ – István Zachar May 2 '15 at 13:49
  • $\begingroup$ On my system (Win7 x64) all possible forms (Run["C:/windows/explorer /e"], Run["C:\\windows\\explorer /e"], Run["C:/windows/explorer", "/e"], Run["C:\\windows\\explorer", "/e"]) work the same way as direct executing from the command line (within the cmd process) of the command explorer /e: it opens the "My Documents" folder. $\endgroup$ – Alexey Popkov May 2 '15 at 19:26
1
$\begingroup$

The following seems to work on Win7 for opening explorer.exe in a specified directory using a command line parameter:

Module[{cmd = "C:\\Windows\\explorer.exe /e, \"" <> $UserAddOnsDirectory <>"\""},
Print@cmd;
Run[cmd]]

Also, Rui Liu (Wolfram Technical Support) provides this detailed answer regarding the vagaries of the Run command for Windows systems.

$\endgroup$
  • $\begingroup$ Actually you don't need to use Run for opening a specific folder, it is sufficient to use SystemOpen, for example SystemOpen[$UserAddOnsDirectory]. $\endgroup$ – Alexey Popkov Jun 10 '15 at 14:19
  • $\begingroup$ @AlexeyPopkov that's good to know. Looks like SystemOpen works for a lot of things as long as the corresponding OS Mime type is set up properly. $\endgroup$ – dionys Jun 10 '15 at 14:35
  • $\begingroup$ Good point about the fragility of SystemOpen (its dependence on the ability of the OS to handle the corresponding MIME type correctly)! I don't know although what corresponds to the directory path? The file:// protocol as in SystemOpen["file://c:"]? $\endgroup$ – Alexey Popkov Jun 10 '15 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.