The Run Command tool allows you to extend the capabilities of Alteryx by running external commands the same as you would from the command line. Need to run a Python script? Gzip a file? Utilize tabcmd? All of this can be done using the Run Command tool.
I’m going to walk through the configuration for two specific examples: Moving a file created with Alteryx to another location and running a Python script. These will show what the Run Command tool does and how to use it to call programs from Alteryx (you’ll find the files used in this example attached at the bottom of the post). First, understand that Run Command has three major steps, two of which are optional:
- Write Source (Optional): Take the data from the Alteryx data stream and write it to a file or database table. Same as Output tool.
- Run External Program (Not Optional): Run a command.
- Read Results (Optional): Read a file back into the Alteryx data stream. Same as Input tool.
To follow along with the second example, you will need to install Python 2.7
Copy a File Created by Alteryx
To copy a file from one location to another, you can use the xcopy command from the Windows command line. https://technet.microsoft.com/en-us/library/cc771254.aspx
The /Y option prevents Windows from asking permission to overwrite a file.
xcopy from_herewrite-to-file.csv to_here /Y
To run the same command from Alteryx:
Here, Alteryx writes the data in the data stream to the file write-to-file.csv listed in Write Source, just like an Output tool. The file is located in the folder from_here. We will copy the CSV file to the folder to_here using the xcopy command. There is no connection to the Run Command output, so Read Results is not required and no data will be output.
Run a Python Script
First, you need to download and install Python 2.7: https://www.python.org/downloads/
To run a Python script with options, you will need use the Python command in the directory where you installed Python. For me that is C:Python27
If you look in script.py you can see that it will read the CSV file alteryx-to-script.csv, add an ID column that is named according to the option -f and then write to the CSV script-to-alteryx.csv.
To run the same command from Alteryx:
Here, Alteryx writes the data stream to a file alteryx-to-script.csv the same as an Output tool. Then script.py, which is configured to read the file output from Alteryx, is run and the ID column is added. For this run, the ID column will be named recordID because of the option -f recordID. The script then writes script-to-alteryx.csv which is read into Alteryx from Read Results just like an Input tool. Write Source and Read Results are necessary when the command in Run External Program needs to access the data in Alteryx. Note also that in the Command box, if you have installed Python in a different location, you will need to adjust the call from C:Python27python.exe to your installation location.
You can also edit your system PATH variable to include the path to Python so that you only need to type the command and not the full path.
The Configuration Options
Write Source
If the Run Command tool has an input connection the data from that connection will be written to the file or database specified. This works the same as an Output tool. The expectation is that the data written will be read by the external command.
Command
The name and full path (unless your system path variable is set) for the command that you want to run. In these examples, I am using xcopy and Python but yours could be anything (e.g. curl, Rscript, tabadmin, etc.)
Options
Any arguments including options, file names, etc. that are specific to the command you need to run. In the second example, I am running the command Python with two options: the name of the script to run (script.py) and the name of the ID column (-f recordID).
Working Directory
The default working directory for Run Command is the location where the Alteryx module is saved. This is equivalent to using the change directory (cd) command. You can specify a different working directory if necessary. Note that the working directory change does not apply to files listed in Write Source or Read Results. It only applies to options in the Run External Program box.
Read Results
The file or database table specified here will be read by Alteryx upon successful completion of the command. This is equivalent to the Input tool. The expectation is that the data manipulated by the external command can now be used in Alteryx for further processing. This step is optional and if no table or file is supplied Alteryx will simply run the command and no data will be read into the data stream.
Run Silent Minimized
You may notice that when you run a module that contains the Run Command tool, the command prompt window will briefly pop up, print output and disappear. Run Minimized will keep the command prompt minimized when the module is run. Run Silent will prevent the command prompt window from opening at all.