Renaming Files using txt-template Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
mc_useless
Posts: 2
Joined: 18 Oct 2021, 12:18

Renaming Files using txt-template

Post by mc_useless » 18 Oct 2021, 12:39

So my dad has tons of music and wants to rename them.

i want the code to remove sections of a name, like
remove "- TSM" completely (spaces included)

I really dont know how i should set this up, ive found code on editing Filenames,
but i would really like a seperate txt file for text to remove,
for future use maybe.
Im really lost here, sorry.

What i mean is have a Textdocument set up like this:
"- TSM"
"- KaraO"



a function to edit "- TSM" to something else would be great too

So maybe a txt called "edit.txt" and "remove.txt" would be in the same DIR as the Script.

Thanks a lot! :thumbup:

User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: Renaming Files using txt-template  Topic is solved

Post by mikeyww » 18 Oct 2021, 15:50

Code: Select all

music  = %A_ScriptDir%\music                       ; Music directory
subs   = %music%\subs.txt                          ; Substitutions
If !FileExist(subs) {
 MsgBox, 48, Error, Substitution file not found. Aborting.
 Return
} Else FileRead, subsText, %subs%
sub := StrSplit(subsText, "`r`n")                  ; Put the sub lines into an array
Loop, Files, %music%\*.mp3, R                      ; Loop through MP3 files
{ fn := A_LoopFileName
  For each, line in sub
   part := StrSplit(line, "|"), fn := StrReplace(fn, part.1, part.2)
  If (fn = A_LoopFileName)                         ; If name has not changed,
   Continue                                        ;  continue to the next iteration;
  FileMove, %A_LoopFilePath%, %A_LoopFileDir%\%fn% ; otherwise, rename the file
  If ErrorLevel
   MsgBox, 48, Error, An error occurred while renaming the file.`n`n%A_LoopFilePath%
}
MsgBox, 64, Success, Done!
Attachments
subs.txt
(19 Bytes) Downloaded 13 times

mc_useless
Posts: 2
Joined: 18 Oct 2021, 12:18

Re: Renaming Files using txt-template

Post by mc_useless » 20 Oct 2021, 16:33

mikeyww wrote:
18 Oct 2021, 15:50

Code: Select all

music  = %A_ScriptDir%\music                       ; Music directory
subs   = %music%\subs.txt                          ; Substitutions
If !FileExist(subs) {
 MsgBox, 48, Error, Substitution file not found. Aborting.
 Return
} Else FileRead, subsText, %subs%
sub := StrSplit(subsText, "`r`n")                  ; Put the sub lines into an array
Loop, Files, %music%\*.mp3, R                      ; Loop through MP3 files
{ fn := A_LoopFileName
  For each, line in sub
   part := StrSplit(line, "|"), fn := StrReplace(fn, part.1, part.2)
  If (fn = A_LoopFileName)                         ; If name has not changed,
   Continue                                        ;  continue to the next iteration;
  FileMove, %A_LoopFilePath%, %A_LoopFileDir%\%fn% ; otherwise, rename the file
  If ErrorLevel
   MsgBox, 48, Error, An error occurred while renaming the file.`n`n%A_LoopFilePath%
}
MsgBox, 64, Success, Done!
Okay so would it be able to do
Loop, Files, %music%\*.*, R
because there arent just mp3 files in there..?

User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: Renaming Files using txt-template

Post by mikeyww » 20 Oct 2021, 17:04

Try it! :)

Post Reply

Return to “Ask for Help (v1)”