I would like to add leading zeros to a number such that it always becomes a 4-digit number. For example, if the number is 12, then the converted number is 0012. If it is 122, the the converted number is 0122. If it is 1, the the converted number is 0001.
How can this be done in Autohotkey? Thank you.
I am using Autohotkey ver1.1.15
How to make a 4-digit number by adding leading zeros
Re: How to make a 4-digit number by adding leading zeros
One option:
Code: Select all
#NoEnv
MyNumber := 1
MsgBox, 0, 4-digit Number, % SubStr("000" . MyNumber, -3)
ExitApp
Re: How to make a 4-digit number by adding leading zeros
Another way
Code: Select all
Num := 12
Pack := "0000"
MsgBox, % (SubStr(Pack, 1, StrLen(Pack) - StrLen(Num)) . Num)
Try out CMD class
Re: How to make a 4-digit number by adding leading zeros
@just me ha ha pretty close... that's what I pretty much had
http://stackoverflow.com/a/24100686
http://stackoverflow.com/a/24100686
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
Re: How to make a 4-digit number by adding leading zeros
http://msdn.microsoft.com/en-us/library ... p/ms647550
or you can get this directly from the documentation
DllCall
or you can get this directly from the documentation
DllCall
DllCall
We are troubled on every side‚ yet not distressed; we are perplexed‚
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed;
Telegram is the best way to reach me
https://t.me/ttnnkkrr
If you have forum suggestions please submit a
Check Out WebWriter
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed;
Telegram is the best way to reach me
https://t.me/ttnnkkrr
If you have forum suggestions please submit a
Check Out WebWriter
Re: How to make a 4-digit number by adding leading zeros
How do i add a padding code into this? I'd like to "SetFormat, float, 03.0". But I don't know how and where to add this line of code to.joedf wrote:@just me ha ha pretty close... that's what I pretty much had
http://stackoverflow.com/a/24100686
Re: How to make a 4-digit number by adding leading zeros
You want padding for a float number?
I’m not sure what you mean here.
Do you have code to share?
I’m not sure what you mean here.
Do you have code to share?
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
Re: How to make a 4-digit number by adding leading zeros
https://autohotkey.com/board/topic/2530 ... ng-string/
` & w::
clip := Clipboard
if RegExMatch(clip, "s)(.*?)(\d+)(.*)", m)
Clipboard := m1 . m2+1 . m3
return
I found a code in one of the forums for pasting incrementing string(above code)
But if I have a string "000abc" it returns a value of "1abc". But my desired output is "001abc". How do I achieve that?
` & w::
clip := Clipboard
if RegExMatch(clip, "s)(.*?)(\d+)(.*)", m)
Clipboard := m1 . m2+1 . m3
return
I found a code in one of the forums for pasting incrementing string(above code)
But if I have a string "000abc" it returns a value of "1abc". But my desired output is "001abc". How do I achieve that?
Re: How to make a 4-digit number by adding leading zeros
Code: Select all
Clipboard := m1 . Format("{:03}", m2+1) . m3
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
Re: How to make a 4-digit number by adding leading zeros
Here's a way:
For more Format function examples see FORMAT FUNCTION EXAMPLES here:
jeeswg's characters tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=26486
Code: Select all
q::
vText := "1abc"
MsgBox, % Format("{:06}", vText)
return
jeeswg's characters tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=26486
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: How to make a 4-digit number by adding leading zeros
Worked like a charm. Thanks a lot.
Re: How to make a 4-digit number by adding leading zeros
Just one more thing. Do you know how to let the code detect the format and then apply it.Odlanir wrote:Code: Select all
Clipboard := m1 . Format("{:03}", m2+1) . m3
In the code you specified, it's fixed to 3 leading 0's. I'm wondering if it's possible to write a script that detects the number of leading zero's in the parent string and apply that as the format.
Re: How to make a 4-digit number by adding leading zeros
Sure:
Code: Select all
clip := Clipboard
if RegExMatch(clip, "s)(.*?)(\d+)(.*)", m) {
lm2 := strlen(m2)
Clipboard := m1 . Format("{:0" lm2 "}", m2+1) . m3
}
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
Re: How to make a 4-digit number by adding leading zeros
awesome, thanks
Re: How to make a 4-digit number by adding leading zeros
The more I use this code, the more features I find to add to this script.
Can someone help me with adding few other lines of code to this?
This code works like a charm, but I'd like to add "F2" and "CTRL+A" into this script.
So what I want is to, click on a file and enter the shortcut key, so that it'll rename it without me having to manually press "F2" and "CTRL+A".
Can someone help me with adding few other lines of code to this?
This code works like a charm, but I'd like to add "F2" and "CTRL+A" into this script.
So what I want is to, click on a file and enter the shortcut key, so that it'll rename it without me having to manually press "F2" and "CTRL+A".
Code: Select all
` & w::
clip := Clipboard
if RegExMatch(clip, "s)(.*?)(\d+)(.*)", m) {
lm2 := strlen(m2)
Clipboard := m1 . Format("{:0" lm2 "}", m2+1) . m3
}
sendinput ^v
return
Re: How to make a 4-digit number by adding leading zeros
Select a file ( only one ) in explorer window and press window-key+3
Code: Select all
#SingleInstance, force
#Persistent
#NoEnv
#3::
OldName := Explorer_GetSelection()
if (RegExMatch(OldName,"`n") > 0 ) {
MsgBox % "You must select only one file."
ExitApp
}
if RegExMatch(OldName, "Os)(.*?)(\d+)(.*)", m) {
lm2 := strlen(m[2])
NewName := m[1] . Format("{:0" lm2 "}", m[2]+1) . m[3]
FileMove, % OldName, % NewName
} else {
MsgBox % "The selected file does not contains any digit."
}
return
Explorer_GetSelection(hwnd="") {
hwnd := hwnd ? hwnd : WinExist("A")
WinGetClass class, ahk_id %hwnd%
if (class="CabinetWClass" or class="ExploreWClass" or class="Progman" or class="#32770")
for window in ComObjCreate("Shell.Application").Windows
if (window.hwnd==hwnd)
sel := window.Document.SelectedItems
for item in sel
ToReturn .= item.path "`n"
return Trim(ToReturn,"`n")
}
esc::
ExitApp
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
Re: How to make a 4-digit number by adding leading zeros
The script works perfectly. But I'd like it to work with normal string, not just on files. I've given renaming files as an example.
So, here are the steps I'd like the script to folllow:
So, here are the steps I'd like the script to folllow:
- I'll copy a text from anywhere
- then I'll click somewhere else(notepad/files or any other software)
- then I'll press the hotkey which executes F2 > CTRL+A > then pastes the incremented text
Re: How to make a 4-digit number by adding leading zeros
Is this what you want?
Code: Select all
#SingleInstance, force
#Persistent
#NoEnv
#3::
if RegExMatch(Clipboard, "O)(.*?)(\d+)(.*)", m) {
lm2 := strlen(m[2])
Clipboard := m[1] . Format("{:0" lm2 "}", m[2]+1) . m[3]
send {F2}
Send ^a
send ^v
}
return
esc::
ExitApp
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
Re: How to make a 4-digit number by adding leading zeros
Wow, yeah. I didn't realize it was this simple. Thank you
Re: How to make a 4-digit number by adding leading zeros
Hey Odlanir, I've created a separate thread for this for another query. Can you please look into that?Odlanir wrote:Is this what you want?Code: Select all
#SingleInstance, force #Persistent #NoEnv #3:: if RegExMatch(Clipboard, "O)(.*?)(\d+)(.*)", m) { lm2 := strlen(m[2]) Clipboard := m[1] . Format("{:0" lm2 "}", m[2]+1) . m[3] send {F2} Send ^a send ^v } return esc:: ExitApp
Who is online
Users browsing this forum: filipemb, Google [Bot] and 386 guests