gpt4 book ai didi

csv - VBScript遍历文件夹中的所有文件

转载 作者:行者123 更新时间:2023-12-04 01:38:14 25 4
gpt4 key购买 nike

我有在单个文件上执行该过程的代码,任何人都可以更改此脚本,以便它遍历目录“ H:\ Letter Display \ Letters”中文件类型为“ .LTR”的所有文件并将其保存:

 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


任何帮助将非常感激!

谢谢

最佳答案

也许这会清除一切。 (或者让您更加困惑,)

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

关于csv - VBScript遍历文件夹中的所有文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16665748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com