現在正在工作。由於某些文件名由中文字符或一些空文件組成(由於我未能完成此操作而無法正確地從vbs關閉文件創建),所以出現了一些錯誤,但我添加了日誌以便可以看到最後處理的文件。我不知道如何讓它只顯示產生錯誤的文件,但也可以。在多個文本文件中循環並替換字符
Const msoFileDialogOpen = 4
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objWord = CreateObject("Word.Application")
Set WshShell = CreateObject("WScript.Shell")
Set myLog = objFSO.OpenTextFile("C:\my.log", ForWriting, True)
strInitialPath = WshShell.ExpandEnvironmentStrings("E:\Filme\")
objWord.ChangeFileOpenDirectory(strInitialPath)
Sub Modify(f)
myLog.WriteLine f
txt = f.OpenAsTextStream.ReadAll
txt = Replace(txt, "ã", "a")
txt = Replace(txt, "â", "a")
f.OpenAsTextStream(2).Write txt
End Sub
Sub Recurse(fldr)
For Each sf In fldr.SubFolders
Recurse sf
Next
For Each f In fldr.Files
ext = LCase(objFSO.GetExtensionName(f.Name))
If ext = "srt" Or ext = "sub" Or ext = "txt" Then Modify f
REM WScript.Echo f
Next
End Sub
With objWord.FileDialog(msoFileDialogOpen)
.Title = "Select the folder to process"
If .Show = -1 Then
For Each item in .SelectedItems
Recurse objFSO.GetFolder(item)
Next
Else
End If
myLog.Close
End With
你是什麼意思的「設置爲4對所有文件」嗎? ['MsoFileDialogType'](http://msdn.microsoft.com/en-us/library/aa432520%28v=office.12%29.aspx)4意味着你可以選擇文件夾,而不是文件。 – 2014-10-20 18:08:18
是的,我想選擇一個文件夾,腳本將打開其中的所有srt,sub和txt文件,進行替換並保存文件。對不起,不清楚。我編輯了我的帖子。 – 2014-10-22 13:22:33
@FloRentin - 'Set txt = f.OpenAsTeStStream.ReadAll' PHOOEY! – 2014-11-12 15:05:11