Using Dispatch to Rename Files
Written by: Bill Mitchell, Application Engineer (PDM Specialist)
There are often data management tasks that are simple, yet get overlooked during a busy project with tight timelines. SOLIDWORKS PDM Professional has an easy-to-use utility called Dispatch which can help enforce data integrity with little or no user interaction.
For example, renaming a new vendor supplied file to contain the vendor name and part number is a task which can easily be automated with Dispatch.
First, the File Card must contain the controls mapped to appropriate variables. The example below contains two values: Vender and Vendor PN.
Next, create a workflow transition to make sure the Vendor and Vendor PN variables are not null (use != as a condition) so the Dispatch script will be able to use the values for the file rename script.
Now that the data prerequisites are in place, launch Dispatch from the Administration application. If Dispatch is not listed under the Add-ins section of the vault it can be added by consulting the PDM Installation Guide.
Right click Dispatch and select Administrate Actions and then Add to create a new Dispatch script.
Set the Activation condition to ‘During State Transition’, select the Transition with the Conditions set to ensure there is data in the card variables to use for the file renaming task. The Activation Time should be set to “After change state has occurred”. This will allow the workflow conditions to be validated before running the script.
For the renaming task to work correctly, the original file extension will need to be parsed from the selected file, retained and then re-used as the scripts executes. This can be done by creating Variables within Dispatch in two steps. First, find the file extension by starting at the end of the file name and search for the first period found. This will return a number value which can then be used to return the actual file extension. The values stored on the file card for Vendor and Vendor PN will also need to be stored in a Dispatch variable. The list of Dispatch variables are shown below:
- FindFileExtension: Find the relative location of the file extension by starting at the end of the file and counting characters until Dispatch find a period.
- VendorName and VendorPN: Values from the file card that must be present for the workflow Transition to complete.
- FileExtension: Uses the selected file name and FindFileExtension variable to return the actual file extension. The ‘7’ at the end allows for both up to 6-character file extensions (including the period).
- NewFileName: Concatenates VendorName, VendorPN and FileExtension variables to rename the file.
Review the Dispatch Help file for assistance with the syntax of the static string values.
Now that the Dispatch variables are defined, the only step remaining is the rename command. Select Add and then select For All Documents. This will run the script for all documents that are selected during the transition.
Next, select Add and the Move/Rename File command. Enter %NameofSelectedFile% or select it from the File Variables section by clicking the -> button. The %NewFileName% Dispatch variable will be the new file name.
Finally, select Add -> End for All Documents to end the script. The resulting Dispatch script should have 3 entries as shown below:
The Dispatch utility available in SOLIDWORKS PDM Professional is a powerful scripting tool that can make redundant tasks easy to automate without introducing performance overhead or time consuming API work.