[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
这两天刚好移植一个项目要处理所有文件内容,写了个脚本。改了下变成搞excel的,你可以试试
  1. Dim XLSFolder, TXTFolder, fso, stm, xl
  2. ' 设置项目源文件所在的工作路径
  3. XLSFolder = "D:\my\tables"
  4. ' 目标文件夹,必须是已存在的
  5. TXTFolder = "D:\my\tables_trans"
  6. Set fso = CreateObject("Scripting.FileSystemObject")
  7. Set stm = CreateObject("ADODB.Stream")
  8.     stm.Mode = 3
  9.     stm.Type = 2
  10.     stm.Charset = "unicode"
  11.    
  12. Set xl = CreateObject("Excel.Application")
  13.     xl.Visible = False
  14.    
  15. ProcessAllFiles XLSFolder
  16. xl.Quit
  17. WScript.Echo "处理结束。"
  18. '* 遍历文件夹
  19. '******************************
  20. Function ProcessAllFiles(folderspec)        
  21.     Dim fd, fs, f, sfds, sfd
  22.     Set fd = fso.GetFolder(folderspec)
  23.     Set fs = fd.Files
  24.     For Each f in fs
  25.         If UCase(Right(f.Path, 4)) = ".XLS" Then
  26.             ProcessOneFile f.Path
  27.         End If
  28.     Next
  29.     Set sfds = fd.SubFolders
  30.     For Each sfd in sfds
  31.         ProcessAllFiles sfd.Path
  32.     Next
  33. End Function
  34. '* 处理一个文件,反悔错误代码
  35. '********************************
  36. Function ProcessOneFile(filespec)
  37.     On Error Resume Next
  38.     Dim iResult, newPath
  39.     iResult = 0
  40.     newPath = GenerateNewPath(filespec, XLSFolder, TXTFolder)
  41.     ' 处理一个文件
  42.     '-------- start ----------
  43.     Dim wb, ur, i, j, strAll
  44.     ' 打开此文件,不更新链接,只读
  45.     Set wb = xl.Workbooks.Open(filespec, 0, True)
  46.     Set ur  = wb.WorkSheets(1).UsedRange
  47.     For i = 1 To ur.Rows.Count
  48.         For j = 1 To ur.Columns.Count
  49.             If j > 1 Then
  50.                 strAll = strAll & vbTab
  51.             ElseIf i > 1 Then
  52.                 strAll = strAll & vbCrLf
  53.             End If
  54.             strAll = strAll & ur.Cells(i, j).Text
  55.         Next
  56.     Next
  57.     wb.Close
  58.     stm.Open
  59.     stm.WriteText strAll
  60.     stm.SaveToFile newPath & ".txt"
  61.     stm.Close
  62.     '---------  end  ---------
  63.     If Err.Number <> 0 Then
  64.         iResult = Err.Number
  65.         Err.Clear
  66.     End If
  67.     On Error Goto 0
  68. End Function
  69. '* 生成一个结构相同的新路径
  70. '**********************************
  71. Function GenerateNewPath(dpnx, dp1, dp2)
  72.     Dim absDP1, absDP2, starPos, pNames, dpnx2, i
  73.     absDP1 = fso.GetFolder(dp1).Path
  74.     absDP2 = fso.GetFolder(dp2).Path
  75.     pNames = Split(dpnx, "\")
  76.     starPos = UBound(Split(absDP1, "\")) + 1
  77.     For i = starPos To UBound(pNames) - 1
  78.         absDP2 = fso.BuildPath(absDP2, pNames(i))
  79.         If Not fso.FolderExists(absDP2) Then fso.CreateFolder absDP2
  80.     Next
  81.     dpnx2 = fso.BuildPath(absDP2, pNames(UBound(pNames)))
  82.     GenerateNewPath = dpnx2
  83. End Function
复制代码

TOP

返回列表