重命名文件,使用 Autohotkey 从文件名中删除不必要的字符

Rename file, remove unnecessary character from file name using Autohotkey

我正在尝试使用 Autohotkey/RegEx 重命名文件(通常是下载的字幕)以丢弃不必要的字符,删除“.” space 最终重命名的文件将仅包含名称和四位数年份。例子如下

  1. 原始文件name/path

D:\Folder\Sub Folder\Hamburger.Hill.1987.BluRay.720p.x264.srt

  1. 重命名的文件应该是这样的

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

最初我只是想去掉“.”。在“Ro Yo Mi” the AHK code is able to remove the “.” to space () 的贡献下,它回答了我最初的问题。

后来我意识到也可以删除不需要的字符(只保留名称、年份和原始文件扩展名)。 Ro Yo Mi”还尝试添加新的代码行来重命名文件名中不需要的字符串 ()。虽然代码表面上显示可以重命名(显示在消息代码中)但最终实际上无法重命名。可能需要进一步升级或更改才能使其按预期运行以完成工作。代码的当前状态可以在给定的参考中找到。

描述

文件没有重命名的问题是因为没有提供路径。因此 AutoHotKey 假设它的当前工作目录是发生更改的地方。由于文件实际上不在 AutoHotKey 的脚本目录中,因此 FileMove 命令失败。

此脚本假定您将提供完整路径和文件名。因此,有了这些信息,这就是我删除字符并使用 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

消息框示例

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