regex - Rename file, remove unnecessary character from file name using Autohotkey

I'm trying to rename files (usually downloaded subtitle) using Autohotkey/RegEx to discard the unnecessary character, remove “.” to space in a way that the final renamed file will contain only name and the four digit year. An example as follows

  1. Original file name/path

D:\Folder\Sub Folder\

  1. Renamed file should be like this

D:\Folder\Sub Folder\Hamburger Hill

Initially I was intended only to remove the “.”. With contribution of “Ro Yo Mi” the AHK code is able to remove the “.” to space (Current Code Part 1) and it answered my initial question.

Later I realized there might possibility to also remove the unnecessary character (only to keep the name, year and also original file extension). Ro Yo Mi” also attempted with added new lines of code to rename the unnecessary string from the file name (Current Code Part 2). Although the code apparently showing capable to rename (show in the message code) but finally could not rename actually. There might some further upgrade or changes needed to make it operational to do the job as intended. Current status of the code could be found in the given reference.

1 Answer

  1. Carl- Reply



    The problem the file wasn't getting renamed is because the path was not provided. Therefore AutoHotKey assumes that it's current working directory is where the changes will occur. Since the files aren't actually in the AutoHotKey's script directory, then the FileMove command fails.

    This script assumes you'll be providing the fullpath and filename. So with this information this is how I'd remove the characters and rename the file using AutoHotKey.

    #.:: ; Replace all "." (except before extension) with spaces 
    OldCLip := ClipboardAll 
    Send ^c
    ClipWait, 1
    ; MsgBox % Clipboard    ; for testing 
    if ( Clipboard ) { 
        ; set the value
        String := Clipboard
        ; String := "D:\Folder\Sub Folder\"
        ; split string into the desired components: path, filename upto and including year, and extension
        RegexMatch(String, "^(.*\\)(.*?[0-9]{4}).*([.][^.]{3})", SubPart)
        FullPath := SubPart1
        Filename := RegexReplace(SubPart2, "\.", " ")  ; replace dots in the file name with spaces to improve readablity
        Filename := RegexReplace(Filename, "i)^the\s+", "") ; remove the `the` and trailing spaces from the beginning of the filename if it exists.
        Extension := SubPart3
        NewPathFilename := FullPath . Filename . Extension
        strMessage := "Renaming '" . String . "' to '" . NewPathFilename . "'"
        MsgBox, % strMessage
        FileMove, % String, % NewPathFilename
        } ; end if
    Clipboard := OldClip 

    Sample Message Box

    Renaming 'D:\Folder\Sub Folder\' to 'D:\Folder\Sub Folder\Hamburger Hill'

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>