Jump to content
Macro Express Forums

Padding a 2 digit integer


Recommended Posts

I'm probably making heavy weather of this...

 

I'm working with filenames like

FrankSinatra06.jpg, always with two numbers before the extension.

 

I want to change that to FrankSinatra07.jpg, Similarly I'd change EltonJohn09.bmp to EltonJohn10.bmp and so on, increasing the number by one.

 

So my macro must extract those two as text, convert them to integers, add 1, and convert that back to text, finally re-assembling the new filename as

FrankSinatra07.jpg

 

Except that 07 text converts to 7 as an integer. I've been trying in vain to find a command that will pad out the integer 7 to 07. Is there one I've missed? Or must I resort to replacing a space in the resulting text by '0' to achieve this? And only at that position, as there could also be spaces in the main name. Seems messy for such a relatively trivial bit of fomatting.

 

--
Terry, East Grinstead, UK

Link to comment
Share on other sites

I've never found one. Whenever I do something like this it's pad and replace.

 

Maybe you should consider make a feature request to ISS asking them to add the ability to specify the padding character.

Link to comment
Share on other sites

Oh and you should treat them as separate text variables and concatenate. The name and the number that is. Take the first variable representing the file name and copy the last two digits to a new variable and then delete the last two digits form the original. Increment, pad, replace, then add the new one to the original string in the rename command.

Link to comment
Share on other sites

Thanks Cory, that was quick!

 

OK, that's what I went ahead and did meanwhile, but looks like that's the only way. It was only 3 extra commands to add the necessary If Variable, but it just seems so crude.

 

I've emailed Insight with a feature request.

 

--
Terry, East Grinstead, UK

Link to comment
Share on other sites

I had this prompt reply from Insight:

 

Thanks for the suggestion. Integer values are defined as whole numbers and do not contain leading zeros. So I don't think we can make that change. You will need to continue converting it back to text and adding the 0 back.

So I've just added this as a Code Snippet for future use. I called it 'Pad an integer',

 

If Variable %nNumberNew% Is Less Than "10"
Variable Modify String: Replace "%tNumberNew%" in %tNumberNew% with "0%tNumberNew%" // Adds a 0
End If

--
Terry, East Grinstead, UK

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...