发布于2026-06-29 阅读(0)
扫一扫,手机访问
聊一个实用的小工具——VBS文件批量处理。很多时候,我们需要对特定文件夹下的文件进行批量操作,比如根据内容关键字来重命名文件。下面这段脚本,就是专门为解决这类需求设计的。

我们先看核心脚本文件:rename.vbs
rename.vbs
'关键字配置文件地址
Const config = "E:cleandatakey.txt"
'要检查的文件夹
Const dir = "D:Loghtml"
'日志保存路径
Const LogDir = "E:cleandataLog"
'全局对象
set fso=createobject("scripting.filesystemobject")
Dim keywordList(10000)
Rem : =========== 启动主程序
Dim starttime , Endtime
starttime = Now
Call main()
endtime = Now
Set fso = Nothing
msgbox "恭喜!操作已完成。时间从:" & starttime & " 到 " & endtime ,4096,"文件重命名"
Rem : =========== 主程序
Sub main()
wscript.echo "开始。。。" & Now
Call GetKeyWord()
Call getFiles(dir)
End Sub
Rem : =========== 读取配置文件
Sub GetKeyWord()
set sdir = createobject("scripting.dictionary")
set file = fso.opentextfile(config)
do while file.atendofstream<>true
m=m+1
sdir.add m,file.readline
Dim word
word = sdir(m)
' wscript.echo word
If Len(Trim(word) )>0 Then
KeywordList(m)= word
End If
Loop
file.close
Set file = Nothing
End Sub
Rem : =========== 获取文件列表
Sub getFiles(path)
Set folder = fso.GetFolder(path)
Set subfolder = folder.subfolders
Set file = folder.files
For Each s_file In file
'wscript.echo s_file.path
checkWord s_file.path
Next
For Each s_subfolder In subfolder
getFiles(s_subfolder.path) '递归调用
Next
End Sub
Rem : =========== 比较配置文件,判断是否包含关键字
Sub checkWord(path)
'wscript.echo path
Dim content , file
Set file = fso.opentextfile(path, 1, false)
content = file.readall
file.close
Set file = Nothing
For i=0 To UBound(keywordList)
word = keywordList(i)
If InStr(content, word )>0 And Len(word)>0 Then
wscript.echo path & " 已匹配到:" & word
' Set file = Nothing
RenameSubPage path
Exit For
End If
Next
End Sub
Rem : =========== 将文件重命名
Sub RenameSubPage(path)
If fso.fileexists(path) =True Then
Dim target , ext
ext = ".bak"
target = path & ext
' ===== 方法一
fso.movefile path , target
' ===== 方法二
'Set f = fso.getfile( path)
'f.name = f.name & ext
'f.close
'Set f = Nothing
WriteLog target
End If
End Sub
Rem : =========== 处理日志
Sub WriteLog(strmsg)
Dim logtxt
logtxt = LogDir & "dellog-" & Year(Now) & "-" & Month(Now) & "-" & Day(Now) & ".txt"
Dim f
If fso.fileexists(logtxt) Then
Set f = fso.opentextfile(logtxt, 8 )
Else
Set f = fso.opentextfile(logtxt, 2, true)
End If
f.writeline strmsg
f.close
Set f = Nothing
' ===== 方法2
' Set objShell = CreateObject("Wscript.Shell")
' cmd = "%comspec% /k echo " & strmsg & " >> " & logtxt & " && exit"
' objShell.Run(cmd) ,vbhide
' 挂起允许,防止在任务管理器里产生过多的 cmd.exe 进程 ,如果有多个进程,请用 taskkill /f /im cmd.exe 关闭
' Set objShell = Nothing
Wscript.Sleep 5
End Sub
整个脚本的思路很清晰。它首先从一个配置文件中读取所有关键字,然后递归遍历指定目录下的所有文件,对每个文件的内容进行扫描。一旦发现文件中包含配置中的某个关键字,就立刻对这个文件进行重命名——在原文件名后添加.bak后缀。最后,所有被重命名的文件路径都会被记录到当天的日志文件中。
这里有一个关键点:key.txt 的格式需要留意。每一行就是一个关键字,例如:
关键字一
关键字一
一行一个,干净利落。脚本在读取时,会自动过滤空行。
如果场景更简单:不需要扫描文件内容,而是直接根据文件名列表进行批量重命名。下面这个改良版就能派上用场。
rem 读取配置文件
Dim config
config = "conf.txt"
set fso=createobject("scripting.filesystemobject")
set a=createobject("scripting.dictionary")
set file=fso.opentextfile(config)
do while file.atendofstream<>true
m=m+1
a.add m,file.readline
src = a(m)
RenameSubPage src
loop
file.close
Set fso =Nothing
msgbox "操作已完成" ,4096,"文件重命名"
Sub RenameSubPage(strURL)
Dim path
For i=19 To 100
path = Replace(strURL , ".html", "_"& i & ".html")
If fso.fileexists(path) =True Then
target = path & ".tmp"
fso.movefile path , target
Else
' do nothing
End If
Next
End Sub
这个版本的思路略有不同。它的配置文件 conf.txt 里不是关键字,而是具体的文件路径,比如:
D:abc.html
D:def.html
脚本会对配置中的每个文件路径进行“拓展重命名”:假设原始文件是 D:abc.html,它会依次检查是否存在 D:abc_19.html 到 D:abc_100.html 这些衍生文件,凡存在的,统一加上 .tmp 后缀。
从实际应用角度来看,第一个脚本更适合内容审计或文档扫描的场景——比如你需要找出所有包含特定敏感词的文档并隔离它们。而第二个脚本则更适合文件名批量处理的场景,比如网页静态文件的批量备份。
值得注意的是,脚本中重命名操作都是不可逆的。虽然只是改了扩展名,但在大规模操作前,还是建议先在小范围内测试一下,确认逻辑符合预期。毕竟,数据无小事。
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
正版软件
正版软件
正版软件
正版软件
正版软件
1
2
3
7
8