Hwo can I retrieve a number from a folder name and convert to integer?


I have a task where I need to read the name of a folder which will be in this format:

1 - Some name here
22 - Another name here

I need to strip out the number (which is easily done by just grabbing the first two characters and then replacing any spaces), but then put a leading zero on any single digits. But I am not clear how I convert that initial number (which is actually text) to a number so that I can apply that formatting.

Probably a really simple one, but I can't work out how.

Hi @Lloydi,

Assuming you have managed to get the folder name into KM variable, use RegEx to get the number before the space.

RegEx.kmmacros (2.7 KB)

Ah, that's the part I already worked out (not using regEx, mind). The crux of the question was really this:

Once I have the number, it is a string/text. If the number is a single digit I want to put a leading zero on it. What is the best way to do that?

I had thought that if it were a number (variable calculation) I could format it, but I don't know how to convert that string to an integer in KM. There are probably a number of ways I can achieve it (maybe check the length of the number, as text, and then prepend the 0 if the length is 1.

Add one more action to it:


Ah, interesting ... you can check if a text variable is <= without having to convert it to an integer first. Nice. OK, well that is very useful to know, thanks :slight_smile:

Instead of using an IF/THEN Action, you can simplify with this Set Variable to Calculation action using the "With Format" to provide left-padding of "0".