Execute Command Line Arguments from LabVIEW

Updated Oct 25, 2023

Environment

Software

  • LabVIEW

Operating System

  • Windows
  • Mac OS X
  • Linux

You can execute command line code from LabVIEW using the System Exec VI. This tutorial walks you through configuring the System Exec VI. 

For questions on commands and their syntax, consult your OS manufacturer's documentation. For instance:

  1. Launch LabVIEW and open a new VI (File >> New VI)
  2. On the block diagram, place a System Exec VI (Functions palette >> Connectivity >> Libraries & Executables >> System Exec VI)
  3. Right-click the command line terminal and select Create Constant. Input the argument needed to launch the command line (e.g. for Windows, cmd). Enter the entire command as you would type it at the DOS prompt, including all parameters.
  4. Right-click the standard input terminal and select Create Constant. This terminal allows you to redirect input to the program that you are running. For example, if you were running a batch file with a "pause" statement, you could input an end of line through the "standard input" to simulate pressing "Enter" on the keyboard.
  5. Determine whether you would like LabVIEW to open the command prompt window.
    • By default, the command prompt window does not open. LabVIEW will open it in the background, run the command, and return the result. This can be adjusted by using /k before the code is sent to the command line (see example below).
    • In certain circumstances, it is necessary to include the new line character (\r\n) after the command.  The termination character that you should use will depend on your operating system.
    • You can run multiple commands by using the & character.
  6. Determine whether you would like to halt code execution until the command prompt returns a result. Configure wait until completion? parameter accordingly.
    • By default, LabVIEW will wait until the result is returned before continuing with the program. Please note that the standard input is ignored when wait until completion? is false.
  7. (Optional) Continue configuring the VI, using LabVIEW content Help as a guide for the other non-required parameters.
  8. (Optional) To view the command line output in LabVIEW, right-click the standard output and select Create Indicator.

The following code executes the dir command on the C:\ directory and outputs the result to a String Indicator. The command line does not open, and the program halts code execution until the command has returned results.

 Note: This image is a LabVIEW snippet, which includes LabVIEW code that you can reuse in your project. To use a snippet, right-click the image, save it to your computer, and drag the file onto your LabVIEW diagram. 

Alternatively, the implementation shown below will open a command prompt window and execute the dir command on the C:\ within the command prompt. The /k argument forces the command prompt window to stay open so that you can see the results of the dir command. Using this method, LabVIEW and the command prompt window will run asynchronously. In other words, after starting the command window and passing the command to it, LabVIEW will continue executing the next VI in your code while the command prompt window responds to the dir command.  

 Note: This image is a LabVIEW snippet, which includes LabVIEW code that you can reuse in your project. To use a snippet, right-click the image, save it to your computer, and drag the file onto your LabVIEW diagram.