以下是“文件重命名工具”的源码,关于它的介绍以及使用方法请参考文章:https://blog.csdn.net/sysdzw/article/details/6198257d
打包下载:https://gitee.com/sysdzw/XRename
1 Option Explicit
2 'xrename replace -dir "c:\movie a\" -string /wma$/ig -newstring "rmvb" -type file:/.*\.wma/ -ignorecase yes -log yes -output "c:\list.txt"
3 'xrename replace -dir "C:\Documents and Settings\sysdzw\桌面\XRename\inetfilename" -string "[1]" -newstring "" -log yes
4 'xrename delete -dir "C:\Documents and Settings\sysdzw\桌面\XRename\inetfilename" -string "[1]"
5 '-ignoreExt 忽略处理后缀名
6 '直接从命令行参数获得的数据
7 Dim strCmdSub As String '二级命令
8 Dim strDirectory As String '工作目录
9 Dim strString As String '要替换的字符(可能为正则表达式全体)
10 Dim strNewString As String '替换后的字符
11 Dim strType As String '要替换的对象限定范围的参数,包含对象类型(file|dir|all)和过滤名称的正则表达式
12 Dim isDealSubDir As Boolean '是否递归子目录 默认值:false
13 Dim isIgnoreCase As Boolean '是否忽略字母大小写 默认值:true
14 Dim isIgnoreExt As Boolean '是否忽略处理后缀名 默认值:true
15 Dim isPutLog As Boolean '是否输出处理的log 默认值:false
16 Dim strOutputFile As String '输出文件列表的路径(仅用于XRename listfile命令)
17
18 Dim strStringPattern As String '从strString分离出来,要替换的内容的正则表达式,不包含//等
19 Dim strStringPatternP As String '从strString分离出来,要替换的内容的正则表达式的属性,为(i|g|ig),默认为ig,普通字符串处理会转换成正则表达式处理,所以i会受isIgnoreCase影响
20
21 Dim strGrepTypePre As String '从strType分离出来,是操作对象的类型(file|dir|all)
22 Dim strTypePattern As String '从strType分离出来,是用于根据操作对象的名称进行过滤的正则表达式,不包含//等
23 Dim strTypePatternP As String '从strType分离出来,是用于根据操作对象的名称进行过滤的正则表达式的属性,为(i|g|ig),一般为ig
24
25 Dim strCmd As String '程序完整命令行参数
26 Dim reg As Object
27 Dim matchs As Object, match As Object
28
29 Dim regForReplace As Object '专门用来替换用的
30 Dim regForTestType As Object '专门用来测试范围是否匹配用的
31 Private Declare Function GetShortPathName Lib "kernel32.dll" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
32
33 Sub Main()
34 Set reg = CreateObject("vbscript.regexp")
35 reg.Global = True
36 reg.IgnoreCase = True
37
38 Set regForReplace = CreateObject("vbscript.regexp")
39 Set regForTestType = CreateObject("vbscript.regexp")
40
41 strCmd = Trim(Command)
42 regForReplace.Pattern = "^""(.+)""$" '删除掉最外围的双引号
43 strCmd = regForReplace.Replace(strCmd, "$1")
44 strCmd = Trim(strCmd)
45
46 If strCmd = "" Then
47 MsgBox "参数不能为空!" & vbCrLf & vbCrLf & _
48 "语法如下:" & vbCrLf & _
49 "(1) replace -dir directory -string string1 -new string2 [-type (file|dir|all)[:string3]] [-ignorecase {yes|no}] [-ignoreExt {yes|no}] [-log {yes|no}]" & vbCrLf & _
50 "(2) delete -dir directory -string string1 [-type (file|dir|all)[:string3]] [-ignorecase {yes|no}] [-log {yes|no}]" & vbCrLf & _
51 "(3) listfile -dir directory -string string1 [-type (file|dir|all)[:string3]] [-ignorecase {yes|no}] [-output path]" & vbCrLf & _
52