Gần đây tôi gặp một yêu cầu ứng dụng, yêu cầu đổi tên các tệp HTML trong thư mục chỉ định nếu trong nội dung chứa một số từ khóa nhất định.
Dưới đây là một tệp VBS tôi đã viết:
rename.vbs
‘Địa chỉ tệp cấu hình từ khóa
Const config = “E:cleandatakey.txt”
‘Thư mục cần kiểm tra
Const dir = “D:Loghtml”
‘Đường dẫn lưu log
Const LogDir = “E:cleandataLog”
‘Đối tượng toàn cục
set fso=createobject(“scripting.filesystemobject”)
Dim keywordList(10000)
‘=========== Khởi động chương trình chính
Dim starttime , Endtime
starttime = Now
Call main()
endtime = Now
Set fso = Nothing
msgbox “Chúc mừng! Thao tác đã hoàn tất. Thời gian từ: ” & starttime & ” đến ” & endtime, 4096, “Đổi tên tệp”
‘=========== Chương trình chính
Sub main()
wscript.echo “Bắt đầu… ” & Now
Call GetKeyWord()
Call getFiles(dir)
End Sub
‘=========== Đọc tệp cấu hình
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)
If Len(Trim(word)) > 0 Then
KeywordList(m) = word
End If
Loop
file.close
Set file = Nothing
End Sub
‘=========== Lấy danh sách tệp
Sub getFiles(path)
Set folder = fso.GetFolder(path)
Set subfolder = folder.subfolders
Set file = folder.files
For Each s_file In file
checkWord s_file.path
Next
For Each s_subfolder In subfolder
getFiles(s_subfolder.path) ‘Gọi đệ quy
Next
End Sub
‘=========== So sánh với từ khóa trong cấu hình
Sub checkWord(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 & ” đã khớp với từ khóa: ” & word
RenameSubPage path
Exit For
End If
Next
End Sub
‘=========== Đổi tên tệp
Sub RenameSubPage(path)
If fso.fileexists(path) = True Then
Dim target , ext
ext = “.bak”
target = path & ext
fso.movefile path , target
WriteLog target
End If
End Sub
‘=========== Ghi log
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
Wscript.Sleep 5
End Sub
Nội dung tệp
key.txt:Từ khóa một
Từ khóa hai
Từ khóa hai
(Mỗi dòng một từ khóa.)
Đây là một phiên bản cải tiến của chương trình đổi tên hàng loạt bằng VBS.
Phiên bản đơn giản khác:
‘Đọc tệp cấu hình
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 “Thao tác đã hoàn tất” ,4096,”Đổi tên tệp”
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
End If
Next
End Sub
Chú thích:
Nội dung tệp
conf.txt:D:abc.html
D:def.html
D:def.html
Hy vọng bài viết chia sẻ về mã thao tác tệp VBS này sẽ hữu ích cho bạn. Cảm ơn bạn đã ủng hộ。
📌 Bài viết này được đóng góp bởi người dùng và bản quyền thuộc về người dùng đã xây dựng bài viết. Bản quyền thuộc về tác giả gốc và chỉ dùng cho mục đích học tập và giao tiếp. Nếu có bất kỳ vi phạm nào, vui lòng liên hệ với chúng tôi để xóa nó.
