Renaming Files w/ Loop Files

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
BoredErica1
Posts: 8
Joined: 18 Apr 2022, 14:43

Renaming Files w/ Loop Files

Post by BoredErica1 » 26 Oct 2022, 00:51

Code: Select all

[::		
folderPath := "C:\Reprise\Steam\steamapps\common\Skyrim Special Edition\Screenshots"
Loop, Files, %folderPath%\*.png
{
	FileMove, %folderpath%\%A_LoopFileName%, %folderpath%\Screenshot%A_Space%%A_Index%.png
}
exit
This is supposed to rename all png files in the folder to Screenshot 1, Screenshot 2, etc. Say there are 3 files. What will happen is first screenshot will be renamed "Screenshot 1", then second "Screenshot 2", then 3rd "Screenshot 3"... and then "Screenshot 1" will be renamed "Screenshot 4". It only happens for the first file renamed. Problem doesn't occur if I don't add "screenshot" to the name. So if it were FileMove, %folderpath%\%A_LoopFileName%, %folderpath%\%A_Space%%A_Index%.png I would get " 1.png", " 2.png", " 3.png" as expected.

What is causing this?

Lepes
Posts: 141
Joined: 06 May 2021, 07:32
Location: Spain

Re: Renaming Files w/ Loop Files

Post by Lepes » 26 Oct 2022, 05:15

interesting "bug", solved formatting the "A_index" padding with zero

Code: Select all

		
Edited: I was wrong
Last edited by Lepes on 26 Oct 2022, 06:23, edited 1 time in total.

User avatar
boiler
Posts: 17385
Joined: 21 Dec 2014, 02:44

Re: Renaming Files w/ Loop Files

Post by boiler » 26 Oct 2022, 05:29

If you don’t want to change how they are to be named, a way around this is to use two loops. One loop gets all the file names and stores them in an array, then the next loop renames those files. That way the file list doesn’t change as they’re being looped through.

Lepes
Posts: 141
Joined: 06 May 2021, 07:32
Location: Spain

Re: Renaming Files w/ Loop Files

Post by Lepes » 26 Oct 2022, 06:20

boiler wrote:
26 Oct 2022, 05:29
If you don’t want to change how they are to be named, a way around this is to use two loops. One loop gets all the file names and stores them in an array, then the next loop renames those files. That way the file list doesn’t change as they’re being looped through.
That makes sense.

It seems my code just worked because I got lucky!!
I thought it was related to the "legacy mode", the way AHK passes parameters to FileMove function, Obviously I was wrong.
I will edit my previous message so nobody else get confuse.
Thanks for clarifying !!

BoredErica1
Posts: 8
Joined: 18 Apr 2022, 14:43

Re: Renaming Files w/ Loop Files

Post by BoredErica1 » 26 Oct 2022, 22:12

My current code is:

Code: Select all

f5::	    ;Renames screenshots in screenshot folder based on clipboard.
FolderPath := "C:\Reprise\Steam\steamapps\common\Skyrim Special Edition\Screenshots"
Loop Files, %folderpath%\*.png
	storedPath .= A_LoopFileName "`n"`
	path := StrSplit(storedPath,"`n")				     ;Split long list in storedpath. 	
Count := 0										
Loop, %FolderPath%\*.ess						
Count++	
Loop % 15 {
Path%A_Index% := path[A_Index]                   
	SavePath := Path%A_Index% 		
	FileMove, %folderpath%\%SavePath%, %folderpath%\%Clipboard%%A_Space%%A_Index%.png
	}
Reload
exit
Reason for Reload is, without it the script wouldn't work when I use it a second time. But I noticed it worked when I reloaded the script. Not sure why though. I think it works fine now though...

User avatar
boiler
Posts: 17385
Joined: 21 Dec 2014, 02:44

Re: Renaming Files w/ Loop Files

Post by boiler » 26 Oct 2022, 22:19

That’s because you have an Exit. You should use Return there and get rid of both the Exit and the Reload.

BoredErica1
Posts: 8
Joined: 18 Apr 2022, 14:43

Re: Renaming Files w/ Loop Files

Post by BoredErica1 » 26 Oct 2022, 23:48

boiler wrote:
26 Oct 2022, 22:19
That’s because you have an Exit. You should use Return there and get rid of both the Exit and the Reload.
Thanks for the info. :D

Post Reply

Return to “Ask for Help (v1)”