Run a program under Windows

Introduction

This example shows how to run a command line program on Windows. There are two ways to do this: Using os.execute() which returns exit (success or error) codes, or io.popen() which returns the output from the command.

We created two wrapper functions to make the calls a little more friendly: The Execute() function that implements friendlier exit codes for os.execute(), and ExecuteWithOutput() that reads and returns io.popen() output.

If you have any questions please contact us at support@interfaceware.com.

Using the Code [top]

  • Import the Run a program under Windows channel from the Builtin: Iguana Files repository
  • Experiment with the code to find out how it works
  • Adapt the code to your own requirements
  • Use the Execute() function to run a command and return friendly exit codes – wraps os.execute()
  • Use the ExecuteWithOutput() function to run a command and return the command output – wraps io.popen()

This is the github code for the main module:

How it works [top]

The code implements two wrapper/helper functions:

  • The Execute() function that implements friendlier exit codes for os.execute()
  • The ExecuteWithOutput() function that reads and returns io.popen() output

Examples calls for each functions are included in main(). The Execute() function is useful when you want to run a program and ensure that it ran correctly, or take action depending on the error returned. The ExecuteWithOutput() function is useful when you want to process the results returned by the program you are calling.

More information [top]