vbs 文件操作集合代碼
下面是我寫(xiě)的一個(gè) vbs 文件:
rename.vbs
rename.vbs
'關(guān)鍵字配置文件地址
Const config = "E:\cleandata\key.txt"
'要檢查的文件夾
Const dir = "D:\Log\html\"
'日志保存路徑
Const LogDir = "E:\cleandata\Log\"
'全局對(duì)象
set fso=createobject("scripting.filesystemobject")
Dim keywordList(10000)
Rem : =========== 啟動(dòng)主程序
Dim starttime , Endtime
starttime = Now
Call main()
endtime = Now
Set fso = Nothing
msgbox "恭喜!操作已完成。時(shí)間從:" & starttime & " 到 " & endtime ,4096,"文件重命名"
Rem : =========== 主程序
Sub main()
wscript.echo "開(kāi)始。。。" & 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) '遞歸調(diào)用
Next
End Sub
Rem : =========== 比較配置文件,判斷是否包含關(guān)鍵字
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
' 掛起允許,防止在任務(wù)管理器里產(chǎn)生過(guò)多的 cmd.exe 進(jìn)程 ,如果有多個(gè)進(jìn)程,請(qǐng)用 taskkill /f /im cmd.exe 關(guān)閉
' Set objShell = Nothing
Wscript.Sleep 5
End Subkey.txt 文件的內(nèi)容:
關(guān)鍵字一
關(guān)鍵字一
即一行一個(gè)關(guān)鍵字 。
這是 VBS 版批量重命名 的一個(gè)改良版。
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 文件內(nèi)容如下:
D:\a\b\c.html
D:\d\e\f.html
到此這篇關(guān)于vbs 文件操作集合代碼的文章就介紹到這了,更多相關(guān)vbs 文件操作內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
可以將文件轉(zhuǎn)換為vbs語(yǔ)句的vbs代碼
可以將文件轉(zhuǎn)換為vbs語(yǔ)句的vbs代碼...2007-03-03
vbscript 注冊(cè)表腳本書(shū)寫(xiě)
用vbscript實(shí)現(xiàn)的修改注冊(cè)表的腳本,建議大家看看2008-06-06
VBScript教程 第十四課在VBScript中使用對(duì)象
VBScript教程 第十四課在VBScript中使用對(duì)象...2006-11-11

