Page 1 of 1
Renaming Files using txt-template
Posted: 18 Oct 2021, 12:39
by mc_useless
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!
Re: Renaming Files using txt-template Topic is solved
Posted: 18 Oct 2021, 15:50
by mikeyww
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!
Re: Renaming Files using txt-template
Posted: 20 Oct 2021, 16:33
by mc_useless
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..?
Re: Renaming Files using txt-template
Posted: 20 Oct 2021, 17:04
by mikeyww
Try it!