Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I'm playing around with some code that's calling external command-line commands on a windows platform, and when doing this, the command prompt will pop up quickly and then disappear. This is not a huge bother, if you are calling something once, however if you have a loop running and each call takes some time, it becomes a major annoyance. Consider the following code:

 Table[Pause[1/2]; Run[ "dir" ];, {3}];

Is there any way to avoid this behavior, or calling an external program though other methods then by Run[].

share|improve this question

3 Answers

up vote 8 down vote accepted

On Windows 7 using ReadList instead of Run suppresses the window:

Table[Pause[1/2]; ReadList["!dir", String], {3}];

If you don't need the output of the command using Read in place of ReadList should be a bit faster and more memory efficient as it only reads the first line.

This use of "!command" in place of a file is at least partially documented under OpenRead:

On systems that support pipes,OpenRead["!command"] runs the external program specified by command, and opens a pipe to get input from it.

share|improve this answer
The example was simply an illustration of the behavior of the problem. I'm not actually interested in the output of dir. – jVincent Jun 16 '12 at 1:54
Yea, I'm not interested in the output, but this does solve my problem. I mistakenly though that this was calling dir specifically and not arbitrary command line code. It works perfectly however. Thank you. Do you know where the documentation for this is, I can't seem to find it under ReadList, is it a general thing that "file" and "!command" are can be interchanged when reading from streams? – jVincent Jun 16 '12 at 1:59
1  
@jVincent I'm glad it's working for you. Regarding documentation I've been asked before and I still do not have a good answer. I'm also not sure where "!command" can be used; I've only used Read and ReadList with it myself. – Mr.Wizard Jun 16 '12 at 2:05
@jVincent I just realized I read your first comment backward; in light of that I suggest you use Read["!dir", String] as it should be a little faster; I'll update my answer with this. – Mr.Wizard Jun 16 '12 at 2:07
1  
OpenRead (and OpenWrite) both do support !command. Thus there is a good chance that functions which read or write files will understand that way to open a pipe for reading or writing as they most probably will use these functions (or lower level equivalents). I know that Import also supports it (surprise: that's even documented :-). The limitiation "on symstems that support pipes" seems somewhat outdated, AFAIK all systems where newer versions of Mathematica do run on support pipes. – Albert Retey Jun 16 '12 at 9:41
show 3 more comments

You can call an external (shell) command cmd without showing a command window by using the pipe syntax "!"<>cmd. This can be used in place of a filename with any Mathematica function that opens a file for reading. For example:

  • Import["!dir","Text"]
  • OpenRead["!dir"], followed by Read, ReadList etc to grab the command output.
  • ReadList["!dir"]
  • BinaryReadList["!dir"]
  • Find["!dir", "tmp.txt"] will return the first lines of cmd output containing "tmp.txt"

These functions can be found in guide/LowLevelFileOperations in the Documentation Center.

The general rules used for resolving file names are documented in the Files and Streams tutorial. This is something that has had improvements for Version 9, e.g. URLs can now appear where filenames can go and the resource will be opened by making an HTTP request.

share|improve this answer
Table[Pause[1/2]; Import["!dir","Text"];, {3}];

Import can also be used to pipe command line output straight into Mathematica.

<<"!dir"

Also works.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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