I have a simple file renaming GUI script that just takes a directory path, and renames all the files inside. It just iterates over each file and adds the new filename + "-" + counter index. For some reason, whether I use A_Index, or a counter variable, it will behave normally sometimes, starting at 1. But if I am still running the same instance of the script and run the script again, it will start the counter index at 10000. I cannot figure out why. It only started doing this when I tried testing it on a folder with 2278 files in it, was behaving pretty normally when I had 10 files in the folder.
Code: Select all
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
#SingleInstance, Force ; Ensures only one instance of the script is running
;Create the GUI, store inputs in variables
Gui, Add, Text, , Path:
Gui, Add, Edit, vDir w270
Gui, Add, Button, w70 h23 gBrowseDir, Browse
Gui, Add, Text, , New Filename:
Gui, Add, Edit, vNewPrefix w270
Gui, Add, Text, , Extension (Optional):
Gui, Add, Edit, vExtension w270
Gui, Add, Text, , Action:
Gui, Add, DropDownList, vAction, Add|Replace
Gui, Add, Button, x+40 w100 h30 gSubmit, OK
Gui, Show, Center, File Renamer
return
; Browse dialog if clicked to select folder
BrowseDir:
FileSelectFolder, SelectedDir, 3, Custom Title, Files to Rename
if (SelectedDir) {
GuiControl,, Dir, % SelectedDir
}
return
Submit:
Gui, Submit, NoHide
; Validation if user enters non-existent folder path or enters nothing
if (!FileExist(Dir)) {
MsgBox, Invalid Path
return
} else if (StrLen(NewPrefix) = 0) {
MsgBox, No Filename Entered
return
} else if (StrLen(Action) = 0) {
MsgBox, No Action Selected
return
}
counter := 0
Loop, Files, % Dir "\*.*"
{
parts := StrSplit(A_LoopFileName, ".")
baseName := parts[1]
ext := parts[parts.MaxIndex()]
If StrLen(Extension) = 0 {
NewExt := "." . ext
} Else {
NewExt := "." . Extension
}
counter += 1
If (Action = "Add") {
NewFilename := NewPrefix . "-" . baseName NewExt
} Else If (Action = "Replace") {
NewFilename := NewPrefix "-" counter NewExt
}
NewPath := Dir "\" NewFilename ; Add timestamp to the filename
FileMove, % A_LoopFileLongPath, % NewPath
}
msgbox Renaming Complete
return
GuiClose:
ExitApp