Sign in to follow this  
Followers 0
RAMMRODD

Trying to rename music files without certain text

10 posts in this topic

So I have a lot of music and most have the artist and featuring artists in the file name. Some also have the year and or track number....so I wanna remove those. I've made up a script (if you can even call it that, its really ugly and probably not correct) that can read a file name and remove certain characters. My problem is that I cant seem to change the file after the final text is established. Im not sure how I should go about renaming since Im pretty sure you cant just rename in auto it. So I thought copy the file to a new folder with the correct name and then delete the original then once the file was done do some folder/directory switching to get the folder back in the original spot. If anyone has any advice I'd love to hear it, I'd also like to know if there is an easier way to do this (IE a prebuilt program), but I really want to finish this script if at all possible.

Chad

P.S. I tried to make a GUI for this and I cant seem to get it working. I want to have a list of removable strings listed with check boxes next to them so that the user can remove text if they choose.Some songs have artists names in them, so the text should not be deleted. I'd also like to add in an update feature, like if check box A and C are checked then pressing OK will display the file name with the checked text removed then a final OK to finalize the change. Any info on how to do this would be great.

#include <string.au3>
#Include <File.au3>
$search = FileFindFirstFile("*.mp3")  
$blank=""

While 1
    $file = FileFindNextFile($search) 
    $originalfile=$file
    If @error Then ExitLoop
    ;MsgBox(4096, "File:", $file)
    $text=_StringProper($file)
    $text = StringReplace($text, "(", $blank) 
    $text = StringReplace($text, "-", $blank)
    $text = StringReplace($text, ")", $blank)
    $text = StringReplace($text, "_", $blank)
    $original="Original: "& $File
    $new="New: "& $text
    filewrite("log.txt",$file & " changed to " & $text&@CRLF &@crlf)
    msgbox(4,"File: ",$original & @CRLF &@crlf & $new)
    ;msgbox(4,"File: ",$originalfile)
    
    FileCopy($originalfile,"C:\Music\"&$text,9)
    filedelete($originalfile)
    ;dirmove("C:\new\*.*",@MyDocumentsDir"\Corrected Songs")
WEnd
;dircopy($location,"\")
FileClose($search)

I commented out a few things I had tried but wasn't sure about so go easy if I miss some noob options.

Share this post


Link to post
Share on other sites



What you're looking for to rename a file is actually FileMove...

From the HelpFile for Filemove:

Because AutoIt lacks a "FileRename" function, use FileMove to rename a file!

For your benefit, perhaps to give you some ideas, this is what I use for performing a similar function:

#Include <File.au3>
#include <GUIConstants.au3>
#include <Misc.au3>
#include <string.au3>
#include <WindowsConstants.au3>
#NoTrayIcon
opt("WinTitleMatchMode", 2)
Opt("TrayIconHide", 1)
Opt("GUIOnEventMode", 1)

_Singleton ("Only one Song Corrector")

#Region Global Delcarations
Global $version = "2.2"
Global $paste
Global $file
Global $inpTrimFileread
Global $inpSpecialTrimread
Global $inpReplace1read
Global $inpReplace2read
Global $chkAdd
Global $chkTrim
Global $chkStripChars
Global $inpAdd
Global $inpTrimFile
Global $inpSpecialTrim
Global $chkRegExp
Global $chkReplace
Global $inpReplace1
Global $inpReplace2
Global $inpRegExp
Global $editChanged
Global $chkStripTrackNumber
Global $btnMakeChanges
Global $editCurrent
Global $chkUnderscores
Global $chkFixDashes
#EndRegion

_CreateGUI()
_FilleditCurrent()

GUISetState(@SW_SHOW)

While 1 
    Sleep (100)
WEnd

Func _CreateGUI()
GUICreate("Song Corrector " & $version, 706, 580)
GUISetOnEvent ($GUI_EVENT_CLOSE, "_GUI_EVENT_CLOSE")
$editCurrent = GUICtrlCreateEdit("", 16, 10, 321, 281, -1, $WS_EX_CLIENTEDGE)
$editChanged = GUICtrlCreateEdit("", 368, 10, 321, 281, -1, $WS_EX_CLIENTEDGE)

$chkAdd = GUICtrlCreateCheckbox("Add", 16, 307, 50, 20)
GUICtrlSetOnEvent (-1, "_chkAdd")
$inpAdd = GUICtrlCreateInput("", 75, 307, 150, 20, -1, $WS_EX_CLIENTEDGE)
GUICtrlSetState(-1, $GUI_Disable)
GUICtrlCreateLabel("to the start of each filename", 240, 309, 299, 20)

$chkTrim = GUICtrlCreateCheckbox("Trim", 16, 339, 50, 17)
GUICtrlSetOnEvent (-1, "_chkTrim")
$inpTrimFile = GUICtrlCreateInput("", 75, 339, 25, 20, -1, $WS_EX_CLIENTEDGE)
GUICtrlSetState(-1, $GUI_Disable)
GUICtrlCreateLabel("characters from the start of each filename", 112, 341, 299, 20)

$chkStripChars = GUICtrlCreateCheckbox("Strip", 16, 377, 57, 20)
GUICtrlSetOnEvent (-1, "_chkStripChars")
$inpSpecialTrim = GUICtrlCreateInput("", 75, 377, 89, 20, -1, $WS_EX_CLIENTEDGE)
GUICtrlSetState(-1, $GUI_Disable)
GUICtrlCreateLabel("out of each filename", 176, 379, 148, 20)

$chkStripTrackNumber = GUICtrlCreateCheckbox("Remove Track numbers from filenames", 16, 412, 305, 20)

$chkReplace = GUICtrlCreateCheckbox("Replace", 16, 446, 61, 20)
GUICtrlSetOnEvent (-1, "_chkReplace")
$inpReplace1 = GUICtrlCreateInput("", 80, 446, 89, 20, -1, $WS_EX_CLIENTEDGE)
GUICtrlSetState(-1, $GUI_Disable)
GUICtrlCreateLabel("with", 178, 448, 32, 20)
$inpReplace2 = GUICtrlCreateInput("", 214, 446, 89, 20, -1, $WS_EX_CLIENTEDGE)
GUICtrlSetState(-1, $GUI_Disable)

$chkRegExp = GUICtrlCreateCheckbox("Strip Out Regular Expression", 16, 481, 155, 20)
GUICtrlSetOnEvent (-1, "_chkRegExp")
$inpRegExp = GUICtrlCreateInput ("", 180, 481, 410, 20)
GUICtrlSetState(-1, $GUI_Disable)

$chkUnderscores = GUICtrlCreateCheckbox('Replace Underscores "_" with spaces', 16, 516, 250, 20)

$chkFixDashes = GUICtrlCreateCheckbox('Replace Dashes "-" with " - "', 16, 551, 250, 20)

$btnShowMe = GUICtrlCreateButton("Show Me The Changes", 432, 350)
GUICtrlSetOnEvent (-1, "_btnShowMe")
GUICtrlSetState (-1, $GUI_DEFBUTTON)

$btnMakeChanges = GUICtrlCreateButton("Make The Changes", 440, 380)
GUICtrlSetOnEvent (-1, "_btnMakeChanges")
GUICtrlSetState(-1, $GUI_Disable)

ControlFocus ( "Song Corrector", "", $btnShowMe )
EndFunc

#Region GUI Functions
Func _btnShowMe()
    ShowMeMakeChanges("Show")
EndFunc

Func _btnMakeChanges()
    ShowMeMakeChanges("Change")
EndFunc

Func _chkAdd()
    If GUICtrlRead($chkAdd) = $GUI_CHECKED Then
        GUICtrlSetState($inpAdd, $GUI_Enable)
    ElseIf GUICtrlRead($chkAdd) = $GUI_UNCHECKED Then
        GUICtrlSetState($inpAdd, $GUI_Disable)
    EndIf
EndFunc

Func _chkTrim()
    If GUICtrlRead($chkTrim) = $GUI_CHECKED Then
        GUICtrlSetState($inpTrimFile, $GUI_Enable)
    ElseIf GUICtrlRead($chkTrim) = $GUI_UNCHECKED Then
        GUICtrlSetState($inpTrimFile, $GUI_Disable)
    EndIf
EndFunc

Func _chkStripChars()
    If GUICtrlRead($chkStripChars) = $GUI_CHECKED Then
        GUICtrlSetState($inpSpecialTrim, $GUI_Enable)
    ElseIf GUICtrlRead($chkStripChars) = $GUI_UNCHECKED Then
        GUICtrlSetState($inpSpecialTrim, $GUI_Disable)
    EndIf
EndFunc

Func _chkReplace()
    If GUICtrlRead($chkReplace) = $GUI_CHECKED Then
        GUICtrlSetState($inpReplace1, $GUI_Enable)
        GUICtrlSetState($inpReplace2, $GUI_Enable)
    ElseIf GUICtrlRead($chkReplace) = $GUI_UNCHECKED Then
        GUICtrlSetState($inpReplace1, $GUI_Disable)
        GUICtrlSetState($inpReplace2, $GUI_Disable)
    EndIf
EndFunc

Func _chkRegExp()
    If GUICtrlRead($chkRegExp) = $GUI_CHECKED Then
        GUICtrlSetState($inpRegExp, $GUI_Enable)
    ElseIf GUICtrlRead($chkRegExp) = $GUI_UNCHECKED Then
        GUICtrlSetState($inpRegExp, $GUI_Disable)
    EndIf
EndFunc

Func _GUI_EVENT_CLOSE()
    Exit
EndFunc
#EndRegion



Func _FilleditCurrent()
    $FileList = _FileListToArray (@ScriptDir, "*.mp3", 1)
    If @error Then
        MsgBox ( 0, "No .mp3 files in folder", "There are no .mp3 files in this folder. Put the Song Corrector in a folder that has .mp3 files and try again." )
        Exit
    EndIf

    Dim $munge
    For $x = 1 to $FileList[0]
        $munge &= $FileList[$x] & @CRLF
    Next
    GUICtrlSetData($editCurrent, $munge)
EndFunc

Func ShowMeMakeChanges($ActionRequired) 
    If $ActionRequired = "Show" Then
        Dim $munge = ""
        GUICtrlSetData($editChanged, "")
        
        $Array = _FileListToArray (@ScriptDir, "*.mp3", 1)
        If @error Then Exit
        
        For $x = 1 To $Array[0]
            $munge &= Rename($Array[$x]) & @CRLF
        Next
        
        GUICtrlSetData($editChanged, $munge)
        GUICtrlSetState($btnMakeChanges, $GUI_Enable)
        GUICtrlSetState($btnMakeChanges, $GUI_DEFBUTTON)

    ElseIf $ActionRequired = "Change" Then
        $Array = _FileListToArray (@ScriptDir, "*.mp3", 1)
        If @error Then Exit
        
        For $x = 1 To $Array[0]     
            FileMove($Array[$x], Rename($Array[$x]), 1)
        Next
    
        MsgBox(0, "Finished", "Done!", 3)
        
        GUICtrlSetData ($editChanged, "")
        GUICtrlSetData ($editCurrent, "")
        GUICtrlSetState($btnMakeChanges, $GUI_Disable)
        _FilleditCurrent()
        
        GUICtrlSetState ($chkAdd, $GUI_UNCHECKED)
        GUICtrlSetState ($chkTrim, $GUI_UNCHECKED)
        GUICtrlSetState ($chkStripChars, $GUI_UNCHECKED)
        GUICtrlSetState ($chkStripTrackNumber, $GUI_UNCHECKED)
        GUICtrlSetState ($chkReplace, $GUI_UNCHECKED)
        GUICtrlSetState ($chkRegExp, $GUI_UNCHECKED)
        GUICtrlSetState ($chkUnderscores, $GUI_UNCHECKED)
        GUICtrlSetState ($chkFixDashes, $GUI_UNCHECKED)
        
        GUICtrlSetData ($inpAdd, "")
        GUICtrlSetData ($inpTrimFile, "")
        GUICtrlSetData ($inpSpecialTrim, "")
        GUICtrlSetData ($inpReplace1, "")
        GUICtrlSetData ($inpReplace2, "")
        GUICtrlSetData ($inpRegExp, "")
        
        GuiCtrlSetState ($inpAdd, $GUI_Disable)
        GuiCtrlSetState ($inpTrimFile, $GUI_Disable)
        GuiCtrlSetState ($inpSpecialTrim, $GUI_Disable)
        GuiCtrlSetState ($inpReplace1, $GUI_Disable)
        GuiCtrlSetState ($inpReplace2, $GUI_Disable)
        GuiCtrlSetState ($inpRegExp, $GUI_Disable)
    EndIf
EndFunc   ;==>MakeChanges

Func Rename($file)  
    If GUICtrlRead($chkAdd) = $GUI_CHECKED Then
        $file = GUICtrlRead($inpAdd) & $file
    EndIf
    
    If GUICtrlRead($chkTrim) = $GUI_CHECKED Then
        If GUICtrlRead($inpTrimFile) = "" Then $inpTrimFileread = 0
        $file = StringTrimLeft($file, GUICtrlRead($inpTrimFile))
    EndIf
    
    If GUICtrlRead($chkStripChars) = $GUI_CHECKED Then
        If GUICtrlRead($inpSpecialTrim) <> "" Then
            $file = StringReplace($file, GUICtrlRead($inpSpecialTrim), "", 0, 1)
        EndIf
    EndIf
    
    If GUICtrlRead($chkStripTrackNumber) = $GUI_CHECKED Then
        $file = StringRegExpReplace($file, "(?:\s|\A)([0-9]{1,3})(?:\s|\z)", "")
    EndIf
    
    If GUICtrlRead($chkReplace) = $GUI_CHECKED Then
        $inpReplace1read = GUICtrlRead($inpReplace1)
        $inpReplace2read = GUICtrlRead($inpReplace2)
        $file = StringReplace($file, $inpReplace1read, $inpReplace2read)
    EndIf
    
    If GUICtrlRead($chkRegExp) = $GUI_CHECKED Then
        If StringRegExp ($file, GUICtrlRead ($inpRegExp), 0) Then
            $RegExp = StringRegExp ($file, GUICtrlRead ($inpRegExp), 1)
            $file = StringReplace($file, $RegExp[0], "")
        EndIf
    EndIf
    
    If GUICtrlRead($chkUnderscores) = $GUI_CHECKED Then
        $file = StringReplace ($file, "_", " ")
    EndIf
    
    If GUICtrlRead($chkFixDashes) = $GUI_CHECKED Then
        $file = StringReplace ($file, "-", " - ", 1)
    EndIf
    
    $file = _StringProper($file)
    
    $file = StringReplace($file, "Id ", "I'd ", 0, 1)
    $file = StringReplace($file, "Nobodys ", "Nobody's ", 0, 1)
    $file = StringReplace($file, "Aint ", "Ain't ", 0, 1)
    $file = StringReplace($file, "Youre ", "You're ", 0, 1)
    $file = StringReplace($file, "Ol ", "Ol' ", 0, 1)
    $file = StringReplace($file, "Livin ", "Livin' ", 0, 1)
    $file = StringReplace($file, "Mommys", "Mommy's", 0, 1)
    $file = StringReplace($file, "Shouldve", "Should've", 0, 1)
    $file = StringReplace($file, "Drinkin ", "Drinkin' ", 0, 1)
    $file = StringReplace($file, "Im ", "I'm ", 0, 1)
    $file = StringReplace($file, "Somethings ", "Something's ", 0, 1)
    $file = StringReplace($file, "Ill ", "I'll ", 0, 1)
    $file = StringReplace($file, "Cant ", "Can't ", 0, 1)
    $file = StringReplace($file, "Dont ", "Don't ", 0, 1)
    $file = StringReplace($file, ".Mp3", ".mp3", 0, 1)

    If StringRegExp($file, "[[:word:]]'[[:upper:]]", 0) Then
        $RegExp = StringRegExp($file, "(?:[[:word:]]')([[:upper:]])", 4)
        
        For $i = 0 To UBound($RegExp) - 1
            $match = $RegExp[$i]
            $file = StringReplace ($file, "'" & $match[1], "'" & StringLower($match[1]), 0, 1)
        Next
    EndIf

    If StringRegExp ($file, "'[[:upper:]]", 0) Then
        $RegExp = StringRegExp ($file, "'[[:upper:]]", 3)
        For $x = 1 to UBound($RegExp)-1
            $file = StringReplace ($file, $RegExp[$x], StringLower($RegExp[$x]))
        Next
    EndIf
    
    If StringRegExp ($file, "\s'[[:lower:]]", 0) Then
        $RegExp = StringRegExp ($file, "\s'[[:lower:]]", 3)
        For $x = 1 to UBound($RegExp)-1
            $file = StringReplace ($file, $RegExp[$x], StringUpper($RegExp[$x]))
        Next
    EndIf
    
    $file = StringStripWS ( $file, 5 )
    
    Return $file
EndFunc   ;==>Rename

Share this post


Link to post
Share on other sites

Wow I had no idea there was THE EXACT thing I wanted already made. Im happy and sad, but at least I know it can work.Yours is exactly what I was looking for idk if there is anything I'd want to change

Share this post


Link to post
Share on other sites

So can filemove be used to just "move" it to the same location it currently is?

Example

C:/Badfilename filemove c:/goodfilename

or is moving it somewhere else required? I was checking your code and I think you just moved it to the same location under a different name...just not 100%

Share this post


Link to post
Share on other sites

You can move it to the same location, so if your filename was C:\Example.txt you could do:

FileMove ("C:\Example.txt", "C:\DifferentName.txt")

Simple as that. >_<

Share this post


Link to post
Share on other sites

Oh wow I knew about the filemove being the way to rename but not filemove to the same location....genius

Share this post


Link to post
Share on other sites

Is there a way to take a list of artists and have them removed from the file?

Example

$text = StringReplace($text, "Lil",$blank)

Where "Lil" could be linked to an external list of all my artists.

I have a few files that are like:

01 Rob Zombie - Living Dead Girl

02 Eminem - The Real Slim Shady

So I could manually remove the numbers, artists and dashes but is there a way to do it with an external list that I could just continually update as I get new artists?

Share this post


Link to post
Share on other sites

When you say "removed from the file", do you mean delete them from the folder?

Share this post


Link to post
Share on other sites

After over a year (WOW!) I'm looking to start this up again. So, how do I go about making a list of things to remove and applying it without having to go in and manually adding it to the script?

Example

Eminem - The Real Slim Shady

So I could type "Eminem" into an input box and then have the script add that to a list that is applied to each song. To help a little more, Im thinking of loading say 10 songs, they all have different artists and all have the artist in the title. Im trying to be able to add all 10 artists into the list 1 after another and then not worry about having to add that artist again.

Thnx

PS I thought I'd keep this thread since it has all the background info in it still

Share this post


Link to post
Share on other sites

Hi.

You are reinventing the wheel.

Use 14aren (one for a rename)

Regards, Rudi.


Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0