重命名文件,使用Autohotkey从文件名中删除不必要的字符 [英] Rename file, remove unnecessary character from file name using Autohotkey

查看:368
本文介绍了重命名文件,使用Autohotkey从文件名中删除不必要的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Autohotkey/RegEx重命名文件(通常是下载的字幕),以丢弃不必要的字符,删除.".以最终重命名的文件仅包含名称和四位数年份的方式进行空间分配.一个例子如下

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. 原始文件名/路径

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

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

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

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

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

最初,我仅打算删除.".通过贡献" Ro Yo Mi ",AHK代码可以删除."到空格(当前代码第1部分)它回答了我最初的问题.

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.

后来我意识到也有可能删除不必要的字符(仅保留名称,年份以及原始文件扩展名). Ro Yo Mi"还尝试添加新的代码行以从文件名重命名不必要的字符串(

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.

推荐答案

说明

未重命名文件的问题是因为未提供路径.因此,AutoHotKey假定将在其当前工作目录中进行更改.由于文件实际上不在AutoHotKey的脚本目录中,因此FileMove命令失败.

此脚本假定您将提供完整路径和文件名.因此,有了这些信息,我便可以删除这些字符并使用AutoHotKey重命名该文件.

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

示例消息框

将'D:\ Folder \ Sub Folder \ the.Hamburger.Hill.1987.BluRay.720p.x264.srt'重命名为'D:\ Folder \ Sub Folder \ Hamburger Hill 1987.srt'

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

这篇关于重命名文件,使用Autohotkey从文件名中删除不必要的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆