[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
我来测试下,多谢楼上兄弟!

TOP

本帖最后由 yu2n 于 2014-9-1 22:46 编辑

沙发~
我用VBS写了一个,使用Word的另存为功能,直接保存为TXT。
请将原文件夹备份后试试,很期待知道Word转换1W个DOC文档需要多长时间……
  1. 'doc2txt.vbs    By yu2n,  2014.09.01
  2. CommandMode "批量转多文件夹内DOC为TXT,再合并TXT"
  3. Main
  4. Sub Main()
  5.   On Error Resume Next
  6.   ' 选择文件夹
  7.   Dim strFolder, arrPath, strPath, nFileCount, i
  8.   WScript.Echo "请选择 Word 文件路径:"
  9.   strFolder = BrowseForFolder("请选择 Word 文件路径:")
  10.   If strFolder = "" Then Exit Sub
  11.   arrPath = ScanFolder(strFolder)
  12.   ' 统计个数,用于显示进度
  13.   For Each strPath In arrPath
  14.     If LCase(Right(strPath,4))=".doc" Or LCase(Right(strPath,5))=".docx" Then
  15.       nFileCount = nFileCount + 1
  16.     End If
  17.   Next
  18.   ' 执行转换
  19.   Set objWord = Word_Init()
  20.   For Each strPath In arrPath
  21.     If LCase(Right(strPath,4))=".doc" Or LCase(Right(strPath,5))=".docx" Then
  22.       i = i + 1
  23.   ' 显示进度
  24.       WScript.Echo "[" & i & "/" & nFileCount & "]" & strPath
  25.   ' 执行转换
  26.       Doc2Txt objWord, strPath
  27.   ' 追加TXT
  28.       CreatTxtFile strFolder, strPath
  29.     End If
  30.   Next
  31.   ' 退出
  32.   objWord.Quit
  33.   Msgbox "完成!"
  34. End Sub
  35. ' 打开DOC,另存为
  36. Function Doc2Txt(objWord, FilePath)
  37.   On Error Resume Next
  38.   Set fso = CreateObject("Scripting.Filesystemobject")
  39.   If Not fso.FileExists(FilePath) Then Exit Function
  40.   
  41.   Const wdFormatText = 2
  42.   Const Encoding = 1200
  43.   Const wdCRLF = 0
  44.   Set objDoc = objWord.Documents.Open(FilePath)
  45.   objWord.ActiveDocument.SaveAs FilePath & ".txt", wdFormatText, False, "", False, _
  46.         "", False, False, False, False, False, Encoding, False, False, wdCRLF
  47.   objDoc.Close
  48.   If Not Err.Number = 0 Then Doc2Txt = True
  49. End Function
  50. ' 浏览文件夹
  51. Function BrowseForFolder(ByVal strTips)
  52.   Dim objFolder
  53.   Set objFolder = CreateObject("Shell.Application").BrowseForFolder (&H0, strTips, &H0010 + &H0001)
  54.   If (Not objFolder Is Nothing) Then BrowseForFolder = objFolder.Self.Path  'objFolder.Items().Item().Path
  55. End Function
  56. ' 创建 Word 对象
  57. Function Word_Init()
  58.   Set objWord = CreateObject("Word.Application")
  59.   If Not Err.Number = 0 Then
  60.     Msgbox "错误:无法创建 Word 对象,你可能没有安装 Office 。"
  61.     WScript.Quit(999)
  62.   End If
  63.   If Not objWord.Application.Version >= 12.0 Then
  64.     Msgbox "警告:请使用 Office 2007 以上版本。"
  65.   End If
  66.   ' 隐藏运行,屏蔽提示
  67.   objWord.Visible = False
  68.   objWord.DisplayAlerts = False
  69.   Set Word_Init = objWord
  70. End Function
  71. '将转换后的TXT追加到指定文件
  72. Function CreatTxtFile(ByVal strFolderPath, ByVal strFilePath)
  73.     Set fso = CreateObject("Scripting.FileSystemObject")
  74.     If Not fso.FileExists(strFilePath & ".txt") Then Exit Function
  75.     ' 整理路径
  76.     strSubFolderName = Mid(strFilePath, Len(strFolderPath) + 2)
  77.     strSubFolderName = Left(strSubFolderName, InStr(strSubFolderName, "\") - 1)
  78.     strTxtFile = strFolderPath & "\" & strSubFolderName & "\" & strSubFolderName & ".txt"
  79.     ' 打开转换后的TXT文件
  80.     Set rTxt = fso.OpenTextFile(strFilePath & ".txt", 1, False, -1)
  81.     strText = rTxt.ReadAll()
  82.     rTxt.Close
  83.     ' 删除转换后的文件
  84.     fso.DeleteFile strFilePath & ".txt", True
  85.     ' 将转换后的TXT追加到指定文件
  86.     Set wTxt = fso.OpenTextFile(strTxtFile, 8, True, -1)
  87.     wTxt.Write strText
  88.     wTxt.Close
  89. End Function
  90. ' 获取文件夹所有文件夹、文件列表(数组)
  91. Function ScanFolder(ByVal strPath)
  92.     Dim arr()
  93.     ReDim Preserve arr(0)
  94.     Call SCAN_FOLDER(arr, strPath)
  95.     ReDim Preserve arr(UBound(arr) - 1)
  96.     ScanFolder = arr
  97. End Function
  98. Function SCAN_FOLDER(ByRef arr, ByVal folderSpec)
  99.     On Error Resume Next
  100.     Dim fso, objItems, objFile, objFolder
  101.     Set fso = CreateObject("Scripting.FileSystemObject")
  102.     Set objItems = fso.GetFolder(folderSpec)
  103.     If Right(folderSpec, 1) <> "\" Then folderSpec = folderSpec & "\"
  104.     If (Not fso.FolderExists(folderSpec)) Then Exit Function
  105.     For Each objFile In objItems.Files
  106.         arr(UBound(arr)) = objFile.Path
  107.         ReDim Preserve arr(UBound(arr) + 1)
  108.     Next
  109.     For Each objFolder In objItems.subfolders
  110.         Call SCAN_FOLDER(arr, objFolder.Path)
  111.     Next
  112.     arr(UBound(arr)) = folderSpec
  113.     ReDim Preserve arr(UBound(arr) + 1)
  114. End Function
  115. ' 以命令提示符环境运行(保留参数)
  116. Sub CommandMode(ByVal sTitle)
  117.     If (LCase(Right(WScript.FullName,11)) = "wscript.exe") Then
  118.         Dim i, sArgs
  119.         For i = 1 To WScript.Arguments.Count
  120.             sArgs = sArgs & " " & """" & WScript.Arguments(i-1) & """"
  121.         Next
  122.         CreateObject("WScript.Shell").Run( _
  123.             "cmd /c Title " & sTitle & " &Cscript.exe //NoLogo  """ & _
  124.             WScript.ScriptFullName & """ " & sArgs & " &pause"),3
  125.             Wscript.Quit
  126.     End If
  127. End Sub
复制代码
『千江有水千江月』千江有水,月映千江;万里无云,万里青天。    http://yu2n.qiniudn.com/

TOP

返回列表