LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Converting number with trailing zeros to string

I'm trying to modify a program to search for the latest version of a file and open that one instead of the older one. The file naming scheme and file contents are beyond my control so I can't change them to make this easier unfortunately.

Basically, I get a list of file names like so:

file_34568_1_1.xml

file_34568.xml

file_35046_1_10.xml

file_35046_1_4.xml

file_35046.xml

etc...

Where the _#_# is the revision number and can range from _1_1 to _255_255 and each file can have multiple revisions.

I was trying to take in the file name string, strip _#_# for each file_# convert it to a float #.# and find the maximum. Then take that max #.# convert it back to _#_#, put that back into the file name and insert it into a new array of files to open that only contains the latest file versions of each file. The problem is, when I format the numbers back into strings (number to frac/exp string), I lose zeros:

1.1 ---> 1.1

1.10 ---> 1.1

3.250 ---> 3.25

If I define the precision, I get:

1.1 ---> 1.100

1.10 ---> 1.100

3.250 ---> 3.250

Which of course you see the problem. I can get the maximum revision just fine, I just want to know how to get my number to a string properly. I've tried also reading the text from the the numeric control but it does the same thing. Any suggestions?

(I'm maybe intermediate skill level in labview but i tend to overthink things, so if you see a simpler way to do this please share!) Thank you! Edit: LabVIEW 14

 

0 Kudos
Message 1 of 10
(3,074 Views)

Hi.

If you have problems in reconstruction the string you had once, the answer is quite simple, keep it or at least it's position in the list. I don't know how you get the max #.# out of the list (in a single time or by comparing new values to the higher you had), but it shouldn't be that hard to keep the string file_#_# that got you the high value (in case you compare string by string) or it's position in the list of string (if you do it in one sweep by getting the index of the maximum). That way you won't have to reconstruct the string with your 0 padding problem.

I hope this will give you a good idea for solving your problem.

Message 2 of 10
(3,064 Views)

Post your VI so we can see what conversion functions and format strings you are using, then can play with them to see if we can make it work.

Message 3 of 10
(3,059 Views)

You're converting it to a float so that you can sort them easily, but what you actually have is two U8 values (0 to 255) that you need to evaluate, so the best way to do that is to make the two U8s into a U16!  

 

So, get your numbers the way you do now, except as U8 values, converting them if need be (Numeric -> Conversion -> To Unsigned Byte Integer).  Then, join them together with the "join numbers" primitive (found in Numeric -> Data Manipulation).  Sort by that U16.  Then split them apart again with "split number" on the same palette.

Message 4 of 10
(3,051 Views)

@Kyle97330 wrote:

so the best way to do that is to make the two U8s into a U16!


My initial thought was similar, except use a cluster of 2 U8s.  Either way will get you the desired result.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 5 of 10
(3,040 Views)

Another approach is to convert filename to a cluster of string (using the underscore as a separator), then to sort the resulting array of clusters (all the filenames) and then to convert back the resulting sorted array of clusters to your filenames.

 

Cluster Sort.png

 

Ben64

Message 6 of 10
(3,020 Views)

Assume the file names are of the form <name>_<Major>_<Rev1>_<Rev2>, where <name> is a common string (e.g. "file"), <Major> is a U16 (basically a 5-digit number, technically <65536), and <Rev1> and <Rev2> are non-zero U8  (e.g. 1 .. 255), or blank.  Note that all of the examples you displayed fit this pattern.

 

If we assume that you want it sorted by Major, then Rev1, then Rev2, it's simple:  Form a Cluster with Filename as the second element and File Value (U32) as the first element.  You need to parse the original FileName to isolate Major, Rev1, and Rev2, making the latter two elements 0 if they are not present.  Form File Value by simply joining Major to the Join of Rev1 and Rev2, like this, sort the Cluster, retrieve the last (newest) member, and extract the FileName:

File Value.png

Incidentally, your original post had _1_10 before _1_4, instead of after (larger Rev2).

 

Bob Schor

Message 7 of 10
(3,014 Views)

@person33425 wrote:

I'm trying to modify a program to search for the latest version of a file and open that one instead of the older one.


How about getting the modification dates instead? 😄

Message 8 of 10
(3,004 Views)

Unfortunately, the (stupid) file format is pre-ordained and fixed.  At least he's not doing what a colleague of mine did, namely to make the file creation date-and-time (to the tenth of a second, yet) part of the filename -- talk about long and human-hostile!  The other problem with "order by date" is that if the file is modified in any way, there goes "Date Modified".

 

If the versions don't get too large, one could always think about the recently-introduced "Create File with Incrementing suffix, but that doesn't help with pre-existing files ...

 

Bob Schor

Message 9 of 10
(2,976 Views)

Sorry for the late response! Thanks for all of your replies! They were all excellent suggestions! After looking them over and after a good nights rest and a fresh morning coffee i found an alternative method that seems

to be working. Sorry I couldn’t post the code, it was part of a large block. 

0 Kudos
Message 10 of 10
(2,937 Views)