通过一个文件夹中的所有文件的VBScript循环 [英] VBScript to loop through all files in a folder

查看:85
本文介绍了通过一个文件夹中的所有文件的VBScript循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有code开展过程上的一个文件,任何人都可以改变这个脚本,它遍历目录中的所有文件H:\\字母显示\\家书的文件类型 .LTR 的,拯救他们所有的:

I have the code to carry out the process on a single file, could anyone alter this script so it loops through all files in the directory "H:\Letter Display\Letters" with the file type ".LTR" and saves them all:

 Const ForReading = 1
 Const ForWriting = 2

 Set objFSO = CreateObject("Scripting.FileSystemObject")
 Set objFile = objFSO.OpenTextFile("H:\Letter Display\Letters\LTRPRT__00000008720000000001NI-K-RMND.LTR",    ForReading)


 str1000 = "1000"
 str1100 = "1100"
 str1200 = "1200"
 str9990 = "9990"

 arrCommas1 = Array(14,31,41,59,70,81,101,111,124,138)
 arrCommas2 = Array(14,31,41,55,79,144,209,274,409,563,589,608,623)
 arrCommas3 = ArraY  (14,32,41,73,83,97,106,156,167,184,188,195,207,260,273,332,368,431,461,472,593,617,666,772,810,834,848,894,898)
 arrCommas4 = Array(14,31,41)

 Do Until objFile.AtEndOfStream
   strLine = objFile.ReadLine

   If Left(strLine, 4) = str1000 then
     intLength = Len(strLine)
     For Each strComma in arrCommas1
       strLine = Left(strLine, strComma - 1) + "," _
         + Mid(strLine, strComma, intLength)
     Next
   End If

   If Left(strLine, 4) = str1100 then
     intLength = Len(strLine)
     For Each strComma in arrCommas2
       strLine = Left(strLine, strComma - 1) + "," _
         + Mid(strLine, strComma, intLength)
     Next
   End If

  If Left(strLine, 4) = str1200 then
     intLength = Len(strLine)
     For Each strComma in arrCommas3
       strLine = Left(strLine, strComma - 1) + "," _
         + Mid(strLine, strComma, intLength)
     Next
   End If

  If Left(strLine, 4) = str9990 then
     intLength = Len(strLine)
     For Each strComma in arrCommas4
       strLine = Left(strLine, strComma - 1) + "," _
         + Mid(strLine, strComma, intLength)
     Next
   End If

   strText = strText & strLine & vbCrLf
 Loop


 objFile.Close

 Set objFile = objFSO.OpenTextFile("H:\Letter Display\Letters\LTRPRT__00000008720000000001NI-K-RMND.LTR",  ForWriting)
 objFile.Write strText
 objFile.Close

任何帮助将是非常美联社preciated!

Any help would be much appreciated!

感谢

推荐答案

也许这将清楚的事情了。 (或混淆你更多,)

Maybe this will clear things up. (Or confuse you more, )

Const ForReading = 1
Const ForWriting = 2

sFolder = "H:\Letter Display\Letters\"
Set oFSO = CreateObject("Scripting.FileSystemObject")

For Each oFile In oFSO.GetFolder(sFolder).Files
  If UCase(oFSO.GetExtensionName(oFile.Name)) = "LTR" Then
    ProcessFiles oFSO, oFile
  End if
Next

Set oFSO = Nothing


Sub ProcessFiles(FSO, File)

Set oFile2 = FSO.OpenTextFile(File.path, ForReading)

 str1000 = "1000"
 str1100 = "1100"
 str1200 = "1200"
 str9990 = "9990"

 arrCommas1 = Array(14,31,41,59,70,81,101,111,124,138)
 arrCommas2 = Array(14,31,41,55,79,144,209,274,409,563,589,608,623)
 arrCommas3 = ArraY  (14,32,41,73,83,97,106,156,167,184,188,195,207,260,273,332,368,431,461,472,593,617,666,772,810,834,848,894,898)
 arrCommas4 = Array(14,31,41)

     Do Until oFile2.AtEndOfStream
       strLine = oFile2.ReadLine

       If Left(strLine, 4) = str1000 then
         intLength = Len(strLine)
         For Each strComma in arrCommas1
           strLine = Left(strLine, strComma - 1) + "," _
             + Mid(strLine, strComma, intLength)
         Next
       End If

       If Left(strLine, 4) = str1100 then
         intLength = Len(strLine)
         For Each strComma in arrCommas2
           strLine = Left(strLine, strComma - 1) + "," _
             + Mid(strLine, strComma, intLength)
         Next
       End If

      If Left(strLine, 4) = str1200 then
         intLength = Len(strLine)
         For Each strComma in arrCommas3
           strLine = Left(strLine, strComma - 1) + "," _
             + Mid(strLine, strComma, intLength)
         Next
       End If

      If Left(strLine, 4) = str9990 then
         intLength = Len(strLine)
         For Each strComma in arrCommas4
           strLine = Left(strLine, strComma - 1) + "," _
             + Mid(strLine, strComma, intLength)
         Next
       End If

       strText = strText & strLine & vbCrLf
     Loop

     sFile = File.path
     oFile2.close
     set oFile2 = Nothing

     Set File = FSO.OpenTextFile(sFile ,  ForWriting)
     File.Write strText
     File.Close
     Set File = Nothing

 end sub

这篇关于通过一个文件夹中的所有文件的VBScript循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆