2013-05-21 187 views
3

我有代碼在單個文件上執行該過程,任何人都可以更改此腳本,以便通過目錄「H:\ Letter Display \ Letters」中的所有文件循環文件類型「.LTR」並保存所有:VBScript循環瀏覽文件夾中的所有文件

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 

任何幫助,將不勝感激!

感謝

+0

我已經回答了這個問題[here](http://stackoverflow.com/a/16648253/1630171)。 –

+0

是的,但我有點困惑如何將其實現到代碼的其餘部分, –

回答

7

也許這會清除一切。 (或迷惑你更多)

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 
+0

感謝您的所有幫助,它似乎不喜歡這一行:sFile = File.path它作爲對象不支持此屬性或方法'File.path'即將到來 –

+1

現在,您可以嘗試一下了。 –

+0

非常感謝!真的幫了! –

2

您當前的腳本主要執行以下操作:

Set objFile = objFSO.OpenTextFile("...", ForReading) 
Do Until objFile.AtEndOfStream 
    strLine = objFile.ReadLine 
    'do stuff with strLine and append to strText 
Loop 
objFile.Close 

Set objFile = objFSO.OpenTextFile("...", ForWriting) 
objFile.Write strText 
objFile.Close 

用於處理你只需要添加相應的圍繞外環,並調整一些指令給定文件夾中的所有文件:

For Each f In objFSO.GetFolder("C:\some\folder").Files 
    Set objFile = f.OpenAsTextStream 
    Do Until objFile.AtEndOfStream 
    strLine = objFile.ReadLine 
    'do stuff with strLine and append to strText 
    Loop 
    objFile.Close 

    Set objFile = f.OpenAsTextStream(ForWriting) 
    objFile.Write strText 
    objFile.Close 
Next
+0

@AnsgerWiechers - +1使用.OpenAsTextStream。 –

+0

OpenAsTextStream的+1同上。 – 2013-12-03 20:18:29

0

會是怎樣更好的是做一個遞歸函數去成低於您的主文件夾中的所有文件夾和搜索這些以及..只要和想法:)

相關問題