10-18-2018 11:53 AM
Hi all!
I'm having a problem with my program. I'd like to save a new .txt file each time that I access to a while loop. The thing is that the way that I'm doing, I'm only able to create the first .txt file and when I try to create the second, it gives me an error.
I attach a small example of what I'm trying to do in my big VI to understand it better.
I'd like to save the first .txt file as 1.txt and make a succession like 2.txt, 3.txt, etc.
I hope you know how to help me, thank you in advance!
Solved! Go to Solution.
10-18-2018 11:58 AM
You could use "create file with incremental suffix".
What kind of code is outside the loop? If there is another loop, you could keep track of the suffix in a shift register.
10-18-2018 12:01 PM
How are you running this code? Are you using the "Run Continuous" button? If so, don't. This is not the way to run a program continuously. You need to use a while loop inside your main VI to continue running until some stop condition is met.
You will need to close the current file and then create a new file with the new name. This needs to be done within the main loop which will keep your application running. I recommend that you take a look at state machines. This will help you to design your application.
10-18-2018 02:14 PM
That's a good idea! I've implemented it and now it works.
I also was confusing the while loop where it must go.
Thanks!
10-18-2018 02:16 PM
Hi Mark!
Thanks for your answer, I wasn't using run continuous mode. Anyway, "This needs to be done within the main loop which will keep your application running. I recommend that you take a look at state machines. This will help you to design your application" it has been useful for me.
10-18-2018 02:20 PM - edited 10-18-2018 02:21 PM
@altenbach wrote:
You could use "create file with incremental suffix".
What kind of code is outside the loop? If there is another loop, you could keep track of the suffix in a shift register.
I've heard that VI slows down as you add more files because it has to keep track of them all? Instead, I like to add a timestamp to the name to make it unique. YYYY-MM-DD and time in 24-hour format (either replace the : or delete them, as they aren't allowed in the Windows file name.) Usually to the second is good enough, but you can go into fractional seconds if the files are written very quickly.
10-18-2018 02:26 PM - edited 10-18-2018 02:26 PM
I suppose you could also add the increment [i] to the file name if you don't mind "0.txt" and you don't mind it being limited to "2147483647.txt".
10-18-2018 05:55 PM
@billko wrote:
@altenbach wrote:
You could use "create file with incremental suffix".
I've heard that VI slows down as you add more files because it has to keep track of them all?
You heard right, but it is typically not a problem unless the number of files gets very huge. For example if you already have existing files from previous runs in that folder, this tool ensure that you continue where you left off. (e.g. if you already have file000.txt through file007.txt, it will create file008.txt and it will not try to overwrite existing files). This might be important.
@billko wrote:
YYYY-MM-DD and time in 24-hour format (either replace the : or delete them, as they aren't allowed in the Windows file name.) Usually to the second is good enough, but you can go into fractional seconds if the files are written very quickly.
I just use the date/time in seconds formatted to a fixed width, zero-padded Hexadecimal. Unique AND timezone invariant. 😄
10-18-2018 08:11 PM
@altenbach wrote:
@billko wrote:
@altenbach wrote:
You could use "create file with incremental suffix".
I've heard that VI slows down as you add more files because it has to keep track of them all?
You heard right, but it is typically not a problem unless the number of files gets very huge. For example if you already have existing files from previous runs in that folder, this tool ensure that you continue where you left off. (e.g. if you already have file000.txt through file007.txt, it will create file008.txt and it will not try to overwrite existing files). This might be important.
@billko wrote:
YYYY-MM-DD and time in 24-hour format (either replace the : or delete them, as they aren't allowed in the Windows file name.) Usually to the second is good enough, but you can go into fractional seconds if the files are written very quickly.I just use the date/time in seconds formatted to a fixed width, zero-padded Hexadecimal. Unique AND timezone invariant. 😄
You're always a few steps ahead of me. 😄