Example Code

Create unique filename by auto-increment path

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Software

  • LabVIEW

Code and Documents

Attachment

Overview

This example offers a solution to the problem of simple unique filenames.

 

 

Description

This VI takes in a path, checks if the file name already existed at that location, and if it is it will auto-increment the file name, the same way windows operating system does, by adding or incrementing numbers in parenthesis at the end of the file name (1), (2), ... (n)
It has the option to only perform this operation on the path if the file already exists in the specified location. Otherwise, the name remains the same.
Warning: This function will increment any number enclosed by parenthesis.

example(s) if files exist:
c:\foldername\foldername\file.txt becomes c:\foldername\foldername\file(1).txt
c:\foldername\foldername\file(1).txt becomes c:\foldername\foldername\file(2).txt
c:\foldername\foldername\file(594).txt becomes c:\foldername\foldername\file(595).txt

 

 

Requirements

LabVIEW 2012 (or compatible)

 

 

Steps to Implement or Execute Code

1. Download and open the VI "Creating Unique File Name_LV2012_NI Verified"

2. Choose the file path
3. Choose if you want to modify the path only if file exists
4. Run VI

 

 

Additional Information or References

 

 BD.PNG

 

 

 

**This document has been updated to meet the current required format for the NI Code Exchange.**

---------------------------------
[will work for kudos]

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
SGIE
Member
Member
on

Nice work, just what I was googling for.

Note however that it doesn't check if the file 'unique path' exists already.

A simple loop around it (with a pre-defined limit to prevent recursion problems?) might be useful.

Replace "\." with "\.[^\.]+$" to prevent input filenames like "1234.abc.pdf" to become "1234(1).abc.pdf"

T.Svitak
Member
Member
on

Works great. I put a while loop around it like said above and moved the path and unique path outside of the loop. I then turned the tunnels into shift registers and had the loop shutdown  when the case structure was false (true to stop). This keeps looking for a file until one is available.

In my main code you can then initialize with a file path and run shift registers for the file path. This will make it so every time the code goes to save, it will not need to keep searching for a file from 0 to say 500. The shift register will remember that it left off on file xxx499.pdf and then look for 500. This saves a lot of time.

Using LV12

valeria_poli
Member
Member
on

Hi,

I'm trying to use this program as a sub-vi. I have a problem, because even if I put the while loop as suggested, the program can't recognize that the output unique path already exist in the fold. So, the purpose to have files with progressive numbers fails. Let me explain better with an example:

I want to save:

peak.txt. The file is generated. OK.

I want to save peak.txt again, but I don't want to overwrite. The sub-vi works, and I have

peak(1).txt. OK.

I want to save peak.txt again, without overwrite.

It fails. It saves peak(1).txt and not peak(2).txt. And so on.

I hope I well explain the problem.

Can someone give me an help? Any ideas?

Thanks,

V.

T.Svitak
Member
Member
on

Post your VI or send me a private message.

valeria_poli
Member
Member
on

I'm not able to post here my .vi, I put two images, of the sub-vi and of the sub-vi included in my vi. Thanks

unique_file_name.jpgsub_vi_included .png

T.Svitak
Member
Member
on

I've done a few things different than you, since my program I always want to check for a unique file, I got rid of the first true/false statement and made it look for a file every time. Right now you are still using a switch to determine if you want to check or not. This is fine and still will work, I just didn't need that. Next you will see how I terminate my while loop. You have a run if true and I have a stop if true. I wanted my program to continue to run until it pulled a file name that wasn't found on the computer.... Basically, The main true structure in my loop means that it has found a file name on the computer that you have typed in, it then incriments that file name and saves it to the case structure, at the same time it tells the loop stop that it is false and to continue running. The next loop it will check the name again with files on the computuer, if the name is not found on the computer then  the main true/false structure will go to false. In that loop I have a T (true) constant and only the file refernum going through it. This true tells the loop to end and the filepath leaves the while loop and returns the name you want to save.

This program will continue to run at the loop execution rate you tell it to and will also continue to run until it finds a unique file. Once found, the loop ends and the unique file name is returned.

I have also changed a bit of the text format being called ( the ([0-9]+) in order to change how the file looks, I didn't want the () around my numbers.

(you will see I have the loop rate called on my program, this is simply because I have this code running in a low priority save loop in a larger program and don't need it to execute extremely fast.)

unique file name 1.PNG

parthabe
Trusted Enthusiast
Trusted Enthusiast
on

Hi Rex,

 

Would it not be prudent to keep the Unique Path as type Path instead of type String, since the former would be platform-independent?!

- Partha ( CLD until Oct 2024 🙂 )