Rename Files Programmatically in LabVIEW

Updated Oct 7, 2020

Reported In

Software

  • LabVIEW

Issue Details

There is no Rename VI on the File I/O palette. How can I rename a file in LabVIEW?

Solution

Method 1: Use the Move function from the File I/O palette which moves a file from one file path location to another. However, for the new location, provide the same location, but with a different file name.
  1. Right-click on the block diagram and select Programming»File I/O»Advanced File Functions»Move
  2. Wire the Source Path terminal with the current full path and name of the file to be renamed (Such as C:\Folder1\Folder2\...\Filename.txt in Windows).
  3. Use the Strip Path and Build Path functions found on the File I/O palette to replace the old file’s name with the desired new name. Wire the newly formed full path to the Target Path terminal of the Move function. This can be seen in the image below.
 
Method 2: An alternate method is to use the Copy function to create a copy of the old file with a new name and then the Delete function to delete the old file.
Note: The path wired to the Target Path terminal may be different if you wish to move the location of the file as well as rename it.

Note:If you rename a VI loaded in memory, the file on disk will be renamed, but the VI in memory will still have the old name. When you close it, you will be prompted to save it with the old name. Unless you change that name to the new name, you will have two copies.

Note:If you are using C:\ as your file path or destination, you will likely get an error.  This error occurs because you do not have permissions to copy files directly to or from the C drive without administrative privileges.  Use another folder path instead.

Method 3: A third method is recommended if you wish to rename very large files or folders. The above methods are inefficient in this case and will take a long time to complete. The VI posted below uses a Call Library Function Node to call the MoveFileA function in the Windows API.
To use this method, set up the Call Library Function Node in the following way:
  1. On the Function tab, specify kernel32.dll as the Library name or path
  2. Select MoveFileA from the Function name drop-down menu. 
  3. Verify that Run in UI thread and stdcall (WINAPI) are selected. 
  4. On the Parameters tab, create three parameters:
    1. Name: return type, Type: Numeric, Data Type: Signed 32-bit Integer
    2. Name: arg1, Type: String, Constant, String format: C String Pointer, Minimum size: <None>
    3. Name: arg2, Type: String, Constant, String format: C String Pointer, Minimum size: <None>
  5. Your final Function Prototype will appear as int32_t MoveFileA(const CStr arg1, const CStr arg2).
Method 4:  Renaming files is a function which can be called using the command line.  The following example shows how to run a command line to change the file name. 
  1. The first segment of the code is cmd /k cd <Containing Folder> . This tells the System Exec.vi to navigate to the Containing Folder. The /k allows multiple command lines to be entered in the same string of text.
  2. The second segment is & rename <Old File Name> <New File Name> . The & indicates that there is a second command to execute.  It will rename the folder from <Old File Name> to <New File Name> .
Method 5 - Renaming All Files in a Folder at Once:  It is possible to rename all the files in a folder using any of the above methods.  The code below demonstrates expanding method one to rename all the files in a specific folder. 
Using the Recursive File List.vi, and Get File Extension.vi, method one is applied to every file in C:\temp folder.
 
 
 

Attachments