Jeromy Anglim's Blog: Psychology and Statistics

Tuesday, March 9, 2010

Running Command Line Programs in Windows

Many of the major open source programs that I use (e.g., R, LaTeX, Eclipse) run on Windows, Linux, and Mac. These programs tend to be designed for sophisticated computer users. Among other things, assumptions are often made about knowledge of the command line. What is second nature for a Linux user may be novel to a non-technical Windows user. One of the aims of this blog is to facilitate the use of these open source tools by social science researchers. Thus, this post sets out a few ideas related to running programs at the command line, which I have found useful when working with programs like R, Eclipse, and TexnicCenter.

There are several reasons why the ideas below are useful:
  • Some programs only run on the command line (e.g., many latex related programs)
  • Some features of programs only run on the command line 
  • Command line arguments can be put into batch files which can make repetitive tasks more efficient
  • Understanding how the command line works helps to diagnose problems and configure software, particularly when setting up multiple programs that are designed to work together (e.g., TeXnicCenter with latex or pdftex; Eclipse with R; etc.)

There are many ways to run programs that make use of the concepts of the command line. The following discusses some common scenarios in Windows.

Command Line: The command line can be opened by typing Windows+R, cmd, [ENTER]
A useful tool is the Open Command Window Here in the Windows Power Tools. It allows you right click a folder in Windows Explorer and open the command prompt with the same current directory.
Typing: help brings up the standard list of commands. I learnt the basic commands years ago and they do come in handy from time to time. While you probably already know these commands, if you don't (perhaps you've grown up in a Post-Dos Windowed world), the following are perhaps the most important for getting started:
  • cd (changing directory)
  • dir (for listing files in a directory)
  • the basics of wild card characters e.g., *.txt to list all txt files

  • md (make a directory)
  • copy (copying files)
  • xcopy (copying directories and subdirectories - good for backup, although I prefer xxcopy)
  • del: (delete files)

Also, see this discussion on StackOverflow regarding PowerShell and CygWin.

Run: Type Windows+R

Windows Shortcuts: Optional arguments can be given to a program to make it open in a different way. These optional arguments can be recorded in Shortcuts such that opening the shortcut loads the programs with the options activated.
For example, Create a new shortcut and specify the target as follows:
"C:\Program Files\R\R-2.9.1\bin\Rgui.exe" --internet2
This will open R (at least on my system) using a proxy server for internet connectivity.

Batch files: Batch files are text files with a *.bat extension used to run commands on windows. Here's an introductory tutorial. One easy way to run command line programs on a file is to create a batch file in the directory, type the command into the batch file (right click - edit), and then run the command.

From an External Program: Some programs facilitate the running of external programs. Such features are most commonly found in text editors designed for programming. For example, TeXnicCenter allows for output profiles (see more info below).

AutoHotKey (Also see AutoIt): AutoHotKey is a program that allows for the creation of scripts to automate various tasks. It can also be used to set up shortcut keys. You could use it to run command line programs such as batch files. Or you can write elements of a batch file directly into the script.

Programs and files are stored in particular directories. In order for a program to run, Windows needs to be able to find it. Thus, if you wanted to run "latex" by typing "latex" in the command line, "latex" would need to be located somewhere in the path.
A file can be found because:
  • The file name is given and it is in the working directory
  • The file name is given and the directory that it is located in is specified in the "path" environment variable
  • A relative directory and file name is given and the file is relative to the working directory
  • An absolute directory and file name is given
  • In some instances variables are used to denote some aspect of the path (e.g., for program files, system directory, and so on)

When adventuring into the world of open source software a failure to have your path variable set correctly is a common cause of problems. Victor Laurie lists some of the Environment Variables that can be used in paths on Windows and also explains how to edit the path.

Many programs have command line options. These options alter the behaviour of the program.

Learning about command line arguments
If you are new to a program, you may want to learn about the command line options that are available. The following are some common ways of obtaining help. 
  • At the command prompt type: help [program] ; e.g., help cd ; This tends to work with Windows commands.
  • At the command prompt type:  [program]/? ; e.g., cd/?; This tends to work with Windows commands
  • At the command prompt type:  [program] --help ; e.g., R -- help ; or latex --help ; This tends to work with programs coming from the Linux world.
  • If the program has a GUI, open the GUI and go to the help menu.
  • Do a search on Google with search terms: [ProgramName] and "command line" and perhaps a word like "options", "switches", or "arguments": e.g., for Jabreflatex, MS Word

The help should specify the order of arguments and whether arguments are optional or required. 

Options are often prefaced by a special character such as "-", "--", or "/". For example, rgui --vanilla starts R in Windows with a particular set of options; or cd /d d: changes the working directory to "d:" and the "/d" switch permits a change of drive. Some are specified using attribute=value syntax (e.g., latex -interaction=nonstopmode ... specifies a particular mode of interaction in LaTeX).

Files, folders, and other arguments that contain spaces require particular care. They often need to be enclosed in quotation marks.

Some programs also provide variables such that when you run the program the variables are replaced with their current value. These typically follow a special syntax and often start with a special character (e.g., %). This is used extensively with programming text editors to integrate files within the editor with external programs such as compilers.

Some Examples


  1. Thanks for this Jeromy,

    Question: what text editor do you use?
    I'm using Notepad++ with an R plugin and I've got a problem that when I try to pass through code (tell notepad++ to run a particular line) it opens up a second R commandline rather than just running it in the one I have open. Any suggestions?

  2. @permut
    I currently use Eclipse with StatET for R.

    I used ESS with Emacs for about a month. It wasn't very intuitive for a Windows user. However, I am considering returning to Emacs one day.

    I do like Notepad++, but I've never tried it with R. So, sorry, I can't really help with your particular problem. One random thought I had: DDE settings are sometimes used to ensure that programs open in an active process instead of starting a new process.

  3. Nice post. Using the Windows command line is almost a forgotten art. I was surprised when I gave a couple people commandline programs and found out they didn't know how to run them.

    Another R-related reason to use the command line involves Sweave. If you use Sweave interactively from R, you might find Sweave's state polluted by your R session's state. Running Sweave from the command line ensures that you're running in a clean state. I wrote more about this problem here.

  4. @John Thanks for the tip. I'm in the process of refining my workflow for combining R and LaTeX.

  5. If you do want all the power of Linux and command line without leaving your windows box, andLinux seems to integrate the two very well. Download, run installer, reboot, and you'll have a bash shell on your desktop and access to all of these tools via a single install command.

  6. @Ken.
    Thanks for the suggestion. I have been toying with learning to use Linux. Perhaps andlinux would be a good way to test the waters.

  7. @permut

    I am using Tinn-R and enjoy it very much. It's available for Win* only, so if you work in both Win* and Linux you have to switch to Emax sometimes.