From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

File Explorer Sort

Solved!
Go to solution

I would like to sort files and folders EXACTLY the way File Explorer does on Windows 10.

Gone through a number of example programs from the forum but they do not work exactly.

 

Apparently File Explorer calls a function called StrCmpLogicalW.

Is there a way I could call this function from LabVIEW?

 

Appreciate any suggestions on how this can be done with LabVIEW.

0 Kudos
Message 1 of 16
(3,401 Views)

There is of course, with the Call Library Node. But as the W at the end of the name indicates, does this function use Unicode strings while LabVIEW strings are ANSI. So you would have to convert every single file name with the function MultiByteToWideChar() first. This will get somewhat complicated to to yourself but fortunately there are functions in this thread, that do this already. You probably want to download and install the ni_lib_unicode_2.0.0.4.vip file from that list.

However it would be possible to create a similar comparison function fully in LabVIEW. The magic is to recognize numbers as whole numbers and sort the strings first alphabetically and if the part up to the number is the the same compare the numbers as whole instead of alphabetically.

Rolf Kalbermatter
My Blog
Message 2 of 16
(3,371 Views)

Thank you rolfk.

Following your first paragraph, do you have an example on how should I convert the ANSI to Unicode then parse this into the Call Library Node?

0 Kudos
Message 3 of 16
(3,345 Views)

image.png

Like this.

Benoit

0 Kudos
Message 4 of 16
(3,332 Views)

Thanks Bseguin.

Can anyone show me how to feed this converted Unicode string into the StrCmpLogicalW function in the Call Library Node?

0 Kudos
Message 5 of 16
(3,321 Views)

It's as "simple" as this (but the used ASCI to UTF16 function is of course Windows only too, but logically correct unlike the other proposed solution which will fail for any ASCII code point above 127):

String Compare Logical.png

Rolf Kalbermatter
My Blog
Download All
0 Kudos
Message 6 of 16
(3,314 Views)

Thank you rolfk .

 

Can you please elaborate more on your statement "but the used ASCI to UTF16 function is of course Windows only too, but logically correct unlike the other proposed solution which will fail for any ASCII code point above 127"?

 

 

0 Kudos
Message 7 of 16
(3,299 Views)

Since my VI uses a Call Library Node to call a Windows API, it won't be possible to execute it on non-Windows LabVIEW platforms.

 

The function as proposed by bseguin does convert the ASCII codes into pseudo UTF16LE by converting all the ASCII codes 0x00 ..0xFF to 16 bit code points 0x0000 ... 0x00FF. That is only correct for the 7-bit ASCII codes 0x00 ... 0x7E. Above 0x007F the mapping between ASCII and Unicode is not the same anymore. Windows Latin-1 (codepage 1252) uses with some exceptions the same extended characters as ISO IEC8859-1 but is not the same. But if your configured local is not using Latin-1 then this mapping gets for those extended ASCII codes completely wrong.

 

Rolf Kalbermatter
My Blog
0 Kudos
Message 8 of 16
(3,284 Views)

Rolfk, this is excellent support. Thank you.

 

Your code works intermittently at times.

When I set String 1 to 1.1 and String 2 to 1, I will get a return value of 1 (this is correct).

But then I set String 2 to 1.1 (equal to String 1), I get a return value of -1 (it should be 0).

After this when I set String 2 back to 1 (same as the first step), the return value will now change to -1 (it should be 1).

 

Do you know why this is happening?

 

Capture.JPG

 

 

0 Kudos
Message 9 of 16
(3,269 Views)

Try with this subVI instead.

Rolf Kalbermatter
My Blog
Message 10 of 16
(3,264 Views)