Rename file, remove unnecessary character from file name using Autohotkey

百般思念 提交于 2019-12-10 12:26:42

问题


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\Hamburger.Hill.1987.BluRay.720p.x264.srt

  1. Renamed file should be like this

D:\Folder\Sub Folder\Hamburger Hill 1987.srt

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:


Description

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 
Clipboard=
Send ^c
ClipWait, 1
; MsgBox % Clipboard    ; for testing 

if ( Clipboard ) { 

    ; set the value
    String := Clipboard
    ; String := "D:\Folder\Sub Folder\the.Hamburger.Hill.1987.BluRay.720p.x264.srt"

    ; 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 
return

Sample Message Box

Renaming 'D:\Folder\Sub Folder\the.Hamburger.Hill.1987.BluRay.720p.x264.srt' to 'D:\Folder\Sub Folder\Hamburger Hill 1987.srt'



来源:https://stackoverflow.com/questions/37089063/rename-file-remove-unnecessary-character-from-file-name-using-autohotkey

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!