Format() function Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
CodeKiller
Posts: 44
Joined: 02 Sep 2014, 04:14

Format() function

24 May 2024, 03:21

Hello,

I am looking at the Format() function but I don't understand at all how it is used (nor if it is useful for my script).

What I want is to create a file renaming function with index at the end like 000 001 002 etc.

It is easy to get the digit part of the file but I don't see how can I add 1 to the last currently used index.

If I have test.txt.053 then the next file should be test.txt.054

But if I do 053 + 1 it will give 54.

Is there a way to use format to add missing 0 in front of the index ?
Or it is not at all used for that.

Thanks !

PS : an easy way is just to count the length and loop on adding "0" but it is ugly or use the RegExMatch to get 0 if any and other digits.
Rohwedder
Posts: 7774
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Format() function  Topic is solved

24 May 2024, 04:29

Hallo,
try:

Code: Select all

Name = test.txt.053
RegExMatch(Name, "\d+$", Number)
MsgBox,% Number
NextName := Format("test.txt.{:03}", Number+1) ; {:03} = total Number width is 3
MsgBox,% NextName
or:

Code: Select all

Name = test.txt.053
RegExMatch(Name, "^(.*?)(\d+)$", Part)
MsgBox,% Part1 " > " Part2
NextName := Format(Part1 "{:0" StrLen(Part2) "}", Part2+1)
MsgBox,% NextName
CodeKiller
Posts: 44
Joined: 02 Sep 2014, 04:14

Re: Format() function

24 May 2024, 05:09

Rohwedder wrote:
24 May 2024, 04:29
Hallo,
try:

Code: Select all

Name = test.txt.053
RegExMatch(Name, "\d+$", Number)
MsgBox,% Number
NextName := Format("test.txt.{:03}", Number+1) ; {:03} = total Number width is 3
MsgBox,% NextName
or:

Code: Select all

Name = test.txt.053
RegExMatch(Name, "^(.*?)(\d+)$", Part)
MsgBox,% Part1 " > " Part2
NextName := Format(Part1 "{:0" StrLen(Part2) "}", Part2+1)
MsgBox,% NextName
Nice !
Now I see how it works !

Thanks again ! :-)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Mateusz53, MrHue, mstrauss2021 and 320 guests