ahk script rename all files in a directory

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Bluerose555
Posts: 112
Joined: 20 Mar 2017, 03:02

ahk script rename all files in a directory

Post by Bluerose555 » 30 Dec 2020, 06:14

Id like to create a script that would rename all files in a directory to the same name (note all files will have different file extensions) So I would rename like this:

name_of_file_1.SMX
name_of_file_2.EFF
name_of_file_3.LIT

renamed to:

0000.SMX
0000.EFF
0000.LIT

I've looked around at various renaming scripts, but none mention naming the file to the same name. Also I need this to work without any user input, as it will be part of a larger script I intend to create.

Thanks!
Last edited by Bluerose555 on 30 Dec 2020, 15:27, edited 1 time in total.

User avatar
Epialis
Posts: 858
Joined: 02 Aug 2020, 22:44

Re: ahk script rename all files in a directory

Post by Epialis » 30 Dec 2020, 07:01

This should do the trick Be careful to make sure you have this is the folder you want to change the names, or it could get messy... I'll add a hotkey just in case. It doesn't effect the ahk script file that is running, just the other files. You should probably change the script to be in one folder and the files in the other to be on the safe side :) This is a working example.

@Bluerose555

Code: Select all

F8::
inFolder := A_ScriptDir
outFolder := A_ScriptDir

Loop, %inFolder%\*.*
{
    newName := SubStr("0000", 1)

    ; Rename file
    FileMove, %A_LoopFileFullPath%, %outFolder%\%newName%.%A_LoopFileExt%

    ; Confirm
    msgBox, File renamed!
}
return 
Esc::
ExitApp

User avatar
Epialis
Posts: 858
Joined: 02 Aug 2020, 22:44

Re: ahk script rename all files in a directory

Post by Epialis » 30 Dec 2020, 07:24

Or a little more condensed

Code: Select all

SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
F8::
Loop, *.*
{
    newName := SubStr("0000", 1)

    ; Rename file
    FileMove, %A_LoopFileFullPath%, %newName%.%A_LoopFileExt%

    ; Confirm
    msgBox, File renamed!
}

return 
Esc::
ExitApp

Bluerose555
Posts: 112
Joined: 20 Mar 2017, 03:02

Re: ahk script rename all files in a directory

Post by Bluerose555 » 30 Dec 2020, 15:25

Thanks for this. Before I test anything out, can you explain to me why you provided warnings. What are the risks of using this script? I did look into some of the commands in your script, and it has been quite educational.


Cheers!

User avatar
Epialis
Posts: 858
Joined: 02 Aug 2020, 22:44

Re: ahk script rename all files in a directory

Post by Epialis » 30 Dec 2020, 15:41

Bluerose555 wrote:
30 Dec 2020, 15:25
Thanks for this. Before I test anything out, can you explain to me why you provided warnings. What are the risks of using this script? I did look into some of the commands in your script, and it has been quite educational.


Cheers!

I gave a warning because if you ran this on your desktop, then that would really mess your files up... or if you put it in the wrong folder, as it renames all the files that are in the same folder. You can remove the msgbox so that it doesn't repeat itself each time if changes the filename.

*EDITED, you should also remove the hot key F8 after you test, so that you don't have to manually do this, as you wanted this automatic. :) Blessings.

Bluerose555
Posts: 112
Joined: 20 Mar 2017, 03:02

Re: ahk script rename all files in a directory

Post by Bluerose555 » 31 Dec 2020, 00:44

Hey thanks again. The script worked, but it renamed the script too. Is there a way to prevent this? You mentioned maybe putting the script in another folder, but this doesn't make sense, as it wouldn't know where the renamed files are located.

CHeers!

User avatar
Epialis
Posts: 858
Joined: 02 Aug 2020, 22:44

Re: ahk script rename all files in a directory

Post by Epialis » 31 Dec 2020, 01:55

This will skip the ahk file... just rename the autohotkey file to what you want. Sorry, I didn't realize it would change a filename that was in use. But this fixes it for ya. Blessings.

Code: Select all

SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
Loop, *.*
{
    newName := SubStr("0000", 1)

if FileExist(Rename.ahk){
	
}
    FileMove, %A_LoopFileFullPath%, %newName%.%A_LoopFileExt%

}
return 
Esc::
ExitApp

Bluerose555
Posts: 112
Joined: 20 Mar 2017, 03:02

Re: ahk script rename all files in a directory

Post by Bluerose555 » 03 Jan 2021, 14:57

hey thanks again, but this still renames the AHK script :cry:

User avatar
Epialis
Posts: 858
Joined: 02 Aug 2020, 22:44

Re: ahk script rename all files in a directory

Post by Epialis » 03 Jan 2021, 15:12

Bluerose555 wrote:
03 Jan 2021, 14:57
hey thanks again, but this still renames the AHK script :cry:
Sorry, my syntax was wrong in the above... try this:

Code: Select all

Loop, *.*
{
    newName := SubStr("0000", 1)
 If (A_LoopFileExt != "ahk")
    FileMove, %A_LoopFileFullPath%, %newName%.%A_LoopFileExt%
}
return 
Esc::
ExitApp
Last edited by Epialis on 03 Jan 2021, 15:54, edited 1 time in total.

teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: ahk script rename all files in a directory

Post by teadrinker » 03 Jan 2021, 15:33

Preferable in this way:

Code: Select all

newName := "0000"
ListToRename := []
Loop, Files, %A_ScriptDir%\*.*
{
   if !(A_LoopFileAttrib ~= "H|R|S" || A_LoopFileName = A_ScriptName)
      ListToRename.Push(A_LoopFilePath)
}

for k, filePath in ListToRename {
   SplitPath, filePath,, dir, ext
   FileMove, %filePath%, %dir%\%newName%.%ext%
}

User avatar
Epialis
Posts: 858
Joined: 02 Aug 2020, 22:44

Re: ahk script rename all files in a directory

Post by Epialis » 03 Jan 2021, 15:40

teadrinker wrote:
03 Jan 2021, 15:33
Preferable in this way:

Code: Select all

newName := "0000"
ListToRename := []
Loop, Files, %A_ScriptDir%\*.*
{
   if !(A_LoopFileAttrib ~= "H|R|S" || A_LoopFileName = A_ScriptName)
      ListToRename.Push(A_LoopFilePath)
}

for k, filePath in ListToRename {
   SplitPath, filePath,, dir, ext
   FileMove, %filePath%, %dir%\%newName%.%ext%
}
Ah... I love other ways lol.. Thank you! This works too, however it renames any other AHK extensions in the directory, as to why I included those to not be renamed.

teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: ahk script rename all files in a directory

Post by teadrinker » 03 Jan 2021, 15:45

It was not instructed not to rename the .ahk, only the script itself.

User avatar
Epialis
Posts: 858
Joined: 02 Aug 2020, 22:44

Re: ahk script rename all files in a directory

Post by Epialis » 03 Jan 2021, 15:53

teadrinker wrote:
03 Jan 2021, 15:45
It was not instructed not to rename the .ahk, only the script itself.
No worries.. I was thinking long term; in case other .ahk needed to be in the same directory. Thank you for the snippet, I'm trying to learn as best I can. :bravo: :bravo:

Bluerose555
Posts: 112
Joined: 20 Mar 2017, 03:02

Re: ahk script rename all files in a directory

Post by Bluerose555 » 03 Jan 2021, 18:10

thanks for the help my friends!

swedahk
Posts: 2
Joined: 01 Dec 2022, 13:24

Re: ahk script rename all files in a directory

Post by swedahk » 01 Dec 2022, 13:44

A challenge which I could not solve with above code examples is to first retrieve the creation date from the properties of the files to be renamed. This timestamp I would like to use in the new file name. I understand that you have to create a file list, but somehow you also need to add the file creation date to this list?

teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: ahk script rename all files in a directory

Post by teadrinker » 01 Dec 2022, 17:12

@swedahk
Welcome to the AHK forum!
See Associative Arrays.

Code: Select all

fileList := []
Loop, Files, %A_ScriptDir%\*.*
{
   fileList.Push({path: A_LoopFilePath, time: A_LoopFileTimeCreated})
}

for k, v in fileList {
   MsgBox, % v.path . "`n" . v.time
}

swedahk
Posts: 2
Joined: 01 Dec 2022, 13:24

Re: ahk script rename all files in a directory

Post by swedahk » 03 Dec 2022, 15:12

@teadrinker
Thank you very much! Works perfect and shows I still have a lot to learn :D

Post Reply

Return to “Ask for Help (v1)”