Counter starts from 10000 when I run Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
idkz
Posts: 5
Joined: 26 Aug 2023, 14:52
Contact:

Counter starts from 10000 when I run

Post by idkz » 26 Aug 2023, 14:59

Hi There,

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

idkz
Posts: 5
Joined: 26 Aug 2023, 14:52
Contact:

Re: Counter starts from 10000 when I run

Post by idkz » 26 Aug 2023, 15:26

It's actually starting from a bunch of random starting points, there;s really no consistency. Sometimes it will go 1 - 10 - 100 - 10000, sometimes it will start from 1 and work properly, sometimes it will start from 10000, it's all over the place

User avatar
DevWithCoffee
Posts: 54
Joined: 13 Oct 2020, 12:16

Re: Counter starts from 10000 when I run

Post by DevWithCoffee » 26 Aug 2023, 15:38

I changed line 70 to this to parse and the error doesn't seem to happen:

Code: Select all

    NewPath := Dir "\" NewFilename  ; Add timestamp to the filename
    ;FileMove, % A_LoopFileLongPath, % NewPath
	Tooltip % "NewPath:`n" NewPath "`nCounter: " counter
	Sleep 100

idkz
Posts: 5
Joined: 26 Aug 2023, 14:52
Contact:

Re: Counter starts from 10000 when I run

Post by idkz » 26 Aug 2023, 16:02

DevWithCoffee wrote:
26 Aug 2023, 15:38
I changed line 70 to this to parse and the error doesn't seem to happen:

Code: Select all

    NewPath := Dir "\" NewFilename  ; Add timestamp to the filename
    ;FileMove, % A_LoopFileLongPath, % NewPath
	Tooltip % "NewPath:`n" NewPath "`nCounter: " counter
	Sleep 100
Interesting.. I had ChatGPT assisting me with some of this and it gave me the tooltip and sleep (I still don't really get what the purpose of the tooltip is in this context), which i thought was unnecessary so I deleted those. Turns out they are necessary? The sleep 100 does slow down the script a bit :l but I guess that's all I can do. Would've rather used Python but my company has only approved AHK for my team..

Appreciate your help!

idkz
Posts: 5
Joined: 26 Aug 2023, 14:52
Contact:

Re: Counter starts from 10000 when I run

Post by idkz » 26 Aug 2023, 17:32

DevWithCoffee wrote:
26 Aug 2023, 15:38
I changed line 70 to this to parse and the error doesn't seem to happen:

Code: Select all

    NewPath := Dir "\" NewFilename  ; Add timestamp to the filename
    ;FileMove, % A_LoopFileLongPath, % NewPath
	Tooltip % "NewPath:`n" NewPath "`nCounter: " counter
	Sleep 100
Actually I just ran it again and it's still exhibiting weird behaviour..
image.png
image.png (6.28 KiB) Viewed 299 times

User avatar
DevWithCoffee
Posts: 54
Joined: 13 Oct 2020, 12:16

Re: Counter starts from 10000 when I run  Topic is solved

Post by DevWithCoffee » 26 Aug 2023, 18:42

idkz wrote:
26 Aug 2023, 16:02
Interesting.. I had ChatGPT assisting me with some ...
First rule of becoming a programmer, don't use AI of any kind.

I believe that the AHK Loop is passing file by file, and when that happens, if a file is created during this process, it will be queued as a new file.
The problem is not in your code, but it is possible to adapt using Array to record all the files and rename based on the saved list.

Edited:

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, , 0, 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
orifname := []
newfname := []
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
    }
	orifname.Push(A_LoopFileLongPath)
    newfname.Push(Dir "\" NewFilename)  ; Add timestamp to the filename
}
for index in orifname
{
	FileMove, % orifname[index], % newfname[index]
}
msgbox Renaming Complete
return

GuiClose:
ExitApp
I did the test with a folder with 263 files and it worked

idkz
Posts: 5
Joined: 26 Aug 2023, 14:52
Contact:

Re: Counter starts from 10000 when I run

Post by idkz » 26 Aug 2023, 19:29

DevWithCoffee wrote:
26 Aug 2023, 18:42
idkz wrote:
26 Aug 2023, 16:02
Interesting.. I had ChatGPT assisting me with some ...
I believe that the AHK Loop is passing file by file, and when that happens, if a file is created during this process, it will be queued as a new file.
The problem is not in your code, but it is possible to adapt using Array to record all the files and rename based on the saved list.
That seems to have done the trick, thank you once again! I assumed something like that was happening and that AHK is just so slow that in between iterations something was happening, cause I run the exact same code in python and it doesn't seem to have a problem with no need for the arrays, very clever adaptation though! I'm a bit rusty, too much HTML/CSS/react lol.
First rule of becoming a programmer, don't use AI of any kind.
:lol: Normally I only supplement with ChatGPT I try not to rely on it too much but I haven't written AHK in so long. It actually did pretty good to be honest!

User avatar
DevWithCoffee
Posts: 54
Joined: 13 Oct 2020, 12:16

Re: Counter starts from 10000 when I run

Post by DevWithCoffee » 26 Aug 2023, 22:39

You could even do this with Batch Script, the AHK Loop in Files mode probably saves memory size so it resets on every request. Test in version 2.0.5 to see what happens.

Post Reply

Return to “Ask for Help (v1)”