Programmatically Replace LabVIEW VIs Using VI Scripting

Programmatically Replace LabVIEW VIs Using VI Scripting

VI scripting provides powerful tools for automating common development tasks. Scripting is the backbone of LabVIEW QuickDrop, allowing developers to create macros that can be invoked using keyboard shortcuts.

This blog describes how to use LabVIEW VI scripting to automate the duplication of a VI and its references into a new VI.


Over years of LabVIEW development, DMC has created a powerful and flexible state machine architecture that is well-suited for industrial applications due to its framework of States, Events, and Actions. Each state machine has its own associated typedef Enums for States, Events, and ActionsLearn more about DMC's State Machine.

Many of DMC’s LabVIEW applications start from the “Template” state machine VI. To start from the Template, we used to copy the Template state machine and its associated CTL files into a new folder, rename the VI and the CTLs, and then open the new VI and point LabVIEW to the “missing” files that were renamed using the new name.

Copy template VIs to a specific filename

This process was simple, but tedious, especially on complicated projects that used multi-state machine architectures.

Manually relinking VI dependencies is a pain


For a recent FedEx project, I decided to create a tool that uses VI scripting to automatically copy a template state machine to a new folder, rename the VI and CTLs, replace all references to the “template” CTL files with the newly created CTL files, and add the new VI to the project.

All the user has to do is enter the file path to the template they wish to copy (the default location of the Template is automatically populated), and enter a name for the new state machine.


My tool for automatically duplicating a VI consists of the following actions. VI scripting is used in steps 3, 5, and 6:

  1. Create new folder – easy using the “Create Folder” VI.

    Screenshot of Content Help Create Folder Screen.
  2. Copy and rename file – easy using “Copy” VI, replacing the old “Template” name with the user-specified name. The one trick at this step is that I also copy the original CTL files with the Template name so that the new VI can be opened by the scripting VIs without searching for missing CTLs.
  3. Replace Template CTLs 
    1. Use the VI scripting Traverse for GObjects VI to find all references to the template CTL files, and replace the CTLs with the newly created files.

      Screenshot of traverse block diagram and replace.
    2. The code above: checks the name of each typedef enum and cluster. If the name matches the name of one of the Template files that needs to be replaced, it is replaced with the corresponding new file. The replacement is performed using a VI Scripting Invoke Node.
    3. Repeat for the front panel. This time we search for controls and indicators that match the Template CTLs we want to replace:

      Screenshot of traverse front panel and replace.
    4. After the front panel and block diagram replacements are complete, save the new VI. Now this VI has no references to the template CTLs.

      Screenshot of save the VI.
  4. Delete original Template CTLs.

    Screenshot of delete original template CTLs.
  5. Resolve conflicts
    1. Since the VI script modifies files on disk while the project is open, the replacements can result in conflicts in the project.
    2. Never fear, VI scripting can find and resolve conflicts. My tool recursively browses project items to find those that are in conflict.

      Screenshot of find conflicts.
    3. To resolve the conflict, my tool deletes the conflicting file and re-adds the file to the project.

      Screenshot of delete conflicts.
  6. Add the new state machine to the project.
    1. I made this step an option that the user can deselect from the front panel. If you’re using an auto-populating folder, this step can cause problems. It’s best to leave the auto-populating folder alone.

      Screenshot of add items to project.
  7. Edit the new state machine icon.
    1. I found the LabVIEW Icon API here: ..\vi.lib\LabVIEW Icon API\Launch Icon This makes it convenient to edit the icons of newly-created VIs so that you don’t end up with lots of VIs with the template icon.

      Screenshot of edit icon.
  8. Notify the user that the duplication process is complete


This duplication tool has helped us save a lot of time when creating new VIs from our template, and it also reduces the possibility of making a mistake in the duplication process. The VI scripting principles I used to develop this tool could be used for any process that involves manipulating VI files on disk or in the project.

Some ideas include:

  • Batch renaming of files in a folder, class, or library
  • Creating a VI based on a typedef data type (perhaps a functional global variable)
  • Copying a VI and its dependencies

Happy VI scripting!

Learn more about DMC's LabVIEW Programming.


Eric West
# Eric West
Thanks Russell, that's a good tip too. I've used *.vit files as a time saver for simpler templates (like a subVI template with a standardicon and error case already wired), but I didn't know about the *.ctt feature. I tried it out and it was very easy to duplicate the whole template and rename the new .ctl files. No relinking was needed. However, since we have several ctl files, saving from the template required me to type 5 new file names. The duplicator tool is still faster, but VI templates are certainly less work to develop. I enjoyed developing this tool to familiarize myself with the power of VI scripting.
Russell Blake
This is a really nice implementation of VI Scripting when you want to reuse a .vi or .ctl file extension. Another way you can achieve this same behavior is to save all your VIs as .vit and all your CTLs as .ctt. This way when you create a new instance of the template, LabVIEW will automatically prompt you for a new name for each file and all the dependencies will be copied properly.

Here is a link about creating vi templates (.vit):

Post a comment

Name (required)

Email (required)

Enter the code shown above:

Related Blog Posts