File Renaming with a_Index

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Kapitano
Posts: 31
Joined: 07 Sep 2019, 10:58

File Renaming with a_Index

Post by Kapitano » 21 Nov 2022, 11:25

This script renames all files in a folder as a prefix and a 3-digit serial number.

Code: Select all

s_Folder := DirSelect("*E:\" , 3 , "Select Folder:")
s_Prefix := InputBox("Prefix: ").Value
Loop Files , s_Folder "\*.*", "R"
	FileMove(a_LoopFileFullPath , a_LoopFileDir "\" s_Prefix " " Format("{:03}" , a_Index) "." a_LoopFileExt)
It works fine if the prefix is not contained as a space-separated word in the folder name, but if it is, the serial number gets a "1" in front of it. If there are more than 1000 files in the folder, we get "10", then "11" etc.

So when the folder name doesn't contain the prefix, we get this, as expected:

Before:
E:\Test Folder\Red.png
E:\Test Folder\Green.png
E:\Test Folder\Blue.png
After (Prefix="Colour"):
E:\Test Folder\Colour 001.png
E:\Test Folder\Colour 002.png
E:\Test Folder\Colour 003.png
But when it does, we get this:

After (Prefix="Test"):
E:\Test Folder\Test 1001.png
E:\Test Folder\Test 1002.png
E:\Test Folder\Test 1003.png
And with more than a thousand files, we get:

After (Prefix="Test"):
E:\Test Folder\Test 10001.png
E:\Test Folder\Test 10002.png
E:\Test Folder\Test 10003.png
...
E:\Test Folder\Test 10999.png
E:\Test Folder\Test 11001.png
E:\Test Folder\Test 11002.png
E:\Test Folder\Test 11003.png
In these cases it works fine again:

After (Prefix="Test Rename"):
E:\Test Folder\Test Rename 001.png
E:\Test Folder\Test Rename 002.png
E:\Test Folder\Test Rename 003.png
After (Prefix="!Test"):
E:\Test Folder\!Test 001.png
E:\Test Folder\!Test 002.png
E:\Test Folder\!Test 003.png
But not here:

After (Prefix="Testing"):
E:\Test Folder\!Testing 1001.png
E:\Test Folder\!Testing 1002.png
E:\Test Folder\!Testing 1003.png
I don't know whether this is an issue with AHK, or with Windows. My workaround is to rename the folder "!Test Folder" and use "Test" as the prefix.

Return to “Ask for Help (v2)”