Identifying Highest Number in Folder Names

I have a folder containing a number of sub-folders with the same name but with different version numbers ind the end. Example:

  • Folder 23
  • Folder 24
  • Folder 25

I want to get the path for folder with the highest version number so that I can copy it to another folder. But I can't fint out how to identify the folder with the highest number.

I've tried a thousand ways to do this but I did not succeed. Any ideas?

Thanks!

Hi Martin - the key to this is to get the names of the folders sorted in Reverse alphabetical order, which you can do with a Folder Contents Collection:

Here's an example macro:

Test Folder Picking.kmmacros (3.7 KB)

KM 0 2021-11-11_18-39-55

The macro simply looks in the folder containing the sub-folders (specified by the Set Variable action coloured red) and picks out the first folder from the contents which it sorts in reverse alphabetical order and, after picking out the first, it stops

My main folder contained sub-folders

Folder_001
Folder_002
Folder_003
etc.

Test this by changing the red action so the variable is set to your main folder. One you get how this works you'll be able to apply it to your specific use-case.

Good luck!

2 Likes

So simple! Thanks a for the help.

I think @tiffle's code looks good, but here's a shorter version that should display the name of the file with the largest number:

image

It takes advantage of the "ls" command which lists files in alphabetical order, and by adding "*/" it considers only files that start with "Folder" and are directories. Then it extracts the last one with "tail". There're two extra characters at the end; we can cut those off if you don't want them (probably by adding "| sed 's/..$//'" to the end of the line.)

P.S. I typed this up without actually testing it, so there's a 2% chance I've made a typo. You don't need to use my solution, but people who use shell commands like to show off the shortest solution that they can muster.

EDIT: I was assuming that your "numbers" are all the same number of digits in length. I made that assumption from reading tiffle's post. If that's not the right assumption, there are ways to fix this.

2 Likes