本帖最后由 yu2n 于 2015-1-8 19:20 编辑
回复 6# pan528
批量处理文本和拖入文本?
建议直接做成浏览文件夹的形式,直接对文件夹里面所有的TXT类型文件处理。
选取多个拖放?好像Windows对拖放个数有限制,如果文件路径够长,估计一次处理不了多少文件。
' VBS 批量替换TXT文件中的英文引号为成对中文引号 By Yu2n- ' VBS 批量替换TXT文件中的英文引号为成对中文引号 By Yu2n
- Call CommandMode("VBS 批量替换TXT文件中的英文引号为成对中文引号 By Yu2n")
-
- Main
- Sub Main()
- Dim strFolder, arrPath, strPath, nFileCount, i
- strFolder = BrowseForFolder("请选择要替换的 TXT 文件所在目录:")
- If strFolder = "" Then
- WScript.Echo vbCrLf & " --- 错误:没有选择文件夹。程序即将退出 ..." & vbCrLf
- Exit Sub
- End If
- arrPath = ScanFolder(strFolder)
- ' 统计个数,用于显示进度
- For Each strPath In arrPath
- If LCase(Right(strPath,4))=".txt" Then nFileCount = nFileCount + 1
- Next
- ' 执行替换
- Dim dtStart, objWord : dtStart = Now()
- For Each strPath In arrPath
- If LCase(Right(strPath,4))=".txt" Then
- i = i + 1 : WScript.Echo "[" & i & "/" & nFileCount & "]" & strPath
- Call DoReplace(strPath, strPath) ' 执行替换
- End If
- Next
- WScript.Echo vbCrLf & " --- " & nFileCount & " 个文档完成替换,耗时 " _
- & DateDiff("s",dtStart,Now()) & " 秒。" & vbCrLf
- End Sub
-
- ' 替换英文引号为中文引号(成对)
- Sub DoReplace(ByVal file1, ByVal file2)
- Set fso = CreateObject("Scripting.filesystemobject")
- set oTxt = fso.OpenTextFile(file1, 1, False, -2)
- str1 = oTxt.ReadAll
- For Each objItem In Split(str1, vbCrLf)
- str2= str2 & regEx_Replace("("")(.*?)("")", objItem, "“$2”") & vbCrLf
- Next
- set oTxt = fso.OpenTextFile(file2, 2, True, -2)
- oTxt.Write str2
- oTxt.Close
- End Sub
-
- ' 正则替换文本
- Function regEx_Replace(ByVal sPattern, ByVal str, ByVal strReplace)
- With CreateObject("VBScript.RegExp")
- .Pattern=sPattern : .IgnoreCase=True
- .Global=True : .MultiLine=True
- regEx_Replace = .Replace(str,strReplace)
- End With
- End Function
-
- ' 浏览文件夹
- Function BrowseForFolder(ByVal strTips)
- Dim objFolder
- Set objFolder = CreateObject("Shell.Application").BrowseForFolder (&H0, strTips, &H0010 + &H0001)
- If (Not objFolder Is Nothing) Then BrowseForFolder = objFolder.Self.Path 'objFolder.Items().Item().Path
- End Function
-
- ' 获取文件夹所有文件夹、文件列表(数组)
- Function ScanFolder(ByVal strPath)
- Dim arr() : ReDim Preserve arr(-1)
- Call SCAN_FOLDER(arr, strPath) : ScanFolder = arr
- End Function
- Function SCAN_FOLDER(ByRef arr, ByVal folderSpec)
- On Error Resume Next
- Dim fso, objItems, objFile, objFolder
- Set fso = CreateObject("Scripting.FileSystemObject")
- Set objItems = fso.GetFolder(folderSpec)
- If Right(folderSpec, 1) <> "\" Then folderSpec = folderSpec & "\"
- If (Not fso.FolderExists(folderSpec)) Then Exit Function
- For Each objFile In objItems.Files
- ReDim Preserve arr(UBound(arr) + 1)
- arr(UBound(arr)) = objFile.Path
- Next
- For Each objFolder In objItems.subfolders
- Call SCAN_FOLDER(arr, objFolder.Path)
- Next
- ReDim Preserve arr(UBound(arr) + 1)
- arr(UBound(arr)) = folderSpec
- End Function
-
- ' 以命令提示符环境运行(保留参数)
- Sub CommandMode(ByVal sTitle)
- If (LCase(Right(WScript.FullName,11)) = "wscript.exe") Then
- Dim oArg, sArgs
- For Each oArg In WScript.Arguments
- sArgs = sArgs & " """ & oArg & """"
- Next
- CreateObject("WScript.Shell").Run( _
- "cmd /c Title " & sTitle & " & Cscript.exe //NoLogo """ & _
- WScript.ScriptFullName & """ " & sArgs & " & pause"), 1, False
- Wscript.Quit
- End If
- End Sub
复制代码
|