ahk script rename all files in a directory
-
- Posts: 112
- Joined: 20 Mar 2017, 03:02
ahk script rename all files in a directory
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!
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.
Re: ahk script rename all files in a directory
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
@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
Re: ahk script rename all files in a directory
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
-
- Posts: 112
- Joined: 20 Mar 2017, 03:02
Re: ahk script rename all files in a directory
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!
Cheers!
Re: ahk script rename all files in a directory
Bluerose555 wrote: ↑30 Dec 2020, 15:25Thanks 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.
-
- Posts: 112
- Joined: 20 Mar 2017, 03:02
Re: ahk script rename all files in a directory
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!
CHeers!
Re: ahk script rename all files in a directory
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
-
- Posts: 112
- Joined: 20 Mar 2017, 03:02
Re: ahk script rename all files in a directory
hey thanks again, but this still renames the AHK script
Re: ahk script rename all files in a directory
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.
-
- Posts: 4331
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: ahk script rename all files in a directory
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%
}
Re: ahk script rename all files in a directory
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 wrote: ↑03 Jan 2021, 15:33Preferable 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% }
-
- Posts: 4331
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: ahk script rename all files in a directory
It was not instructed not to rename the .ahk, only the script itself.
Re: ahk script rename all files in a directory
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.teadrinker wrote: ↑03 Jan 2021, 15:45It was not instructed not to rename the .ahk, only the script itself.
-
- Posts: 112
- Joined: 20 Mar 2017, 03:02
Re: ahk script rename all files in a directory
thanks for the help my friends!
Re: ahk script rename all files in a directory
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?
-
- Posts: 4331
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: ahk script rename all files in a directory
@swedahk
Welcome to the AHK forum!
See Associative Arrays.
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
}
Re: ahk script rename all files in a directory
@teadrinker
Thank you very much! Works perfect and shows I still have a lot to learn
Thank you very much! Works perfect and shows I still have a lot to learn