- 帖子
- 643
- 积分
- 1270
- 技术
- 1
- 捐助
- 0
- 注册时间
- 2012-7-1
|
[问题求助] 【已解决】如何给VBS加一句循环代码使之能够处理多个文件?
本帖最后由 思想之翼 于 2015-2-26 18:48 编辑
下列VBS代码仅对一个文本S1.txt进行操作,结果输出为Z1.txt。若有1000个文本,就要制作1000个类似的VBS,请教大师:如何加一句循环代码,使之能够依次对S1.txt S2.txt S3.txt...S1000.txt操作(S1.txt S2.txt S3.txt...S1000.txt数据格式一样),结果依次输出为Z1.txt Z2.txt Z3.txt...Z1000.txt恳望得到大家的帮助!
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("S1.txt")
m = 1 : Max = 0
ReDim arIn(m-1) : ReDim arOut(m-1)
Do Until file.AtEndOfStream
strLine = RegEx(file.ReadLine)
If strLine <> "" Then
...
End If
Loop
For i = 0 to m-1
For j = CLng(n/2) to 0 step -1
...
Next
strOut = Left(strOut,Len(strOut)-1) & vbCrLf
Next
fso.OpenTextFile("Z1.txt",2,True).Write strOut
CreateObject("Wscript.Shell")
Function RegEx(strLine)
Set re = New RegExp
re.Pattern = "\s+"
re.Global = True
RegEx = Trim(re.Replace(strLine," "))
End Function
解决方案:
Set fso = CreateObject("Scripting.FileSystemObject")
For f = 1 to 300
Set file = fso.OpenTextFile(f & ".txt")
m = 21 : Max = 0 'm=总列数,Max=输出最大行数
ReDim arIn(m-1) : ReDim arOut(m-1)
Do Until file.AtEndOfStream
strLine = RegEx(file.ReadLine)
If strLine <> "" Then
ReDim PreServe ar(m-1,n)
For i = 0 to m-1
arIn(i) = arIn(i) & "$" & Split(strLine)(i) & " "
ar(i,n) = arIn(i)
Next
n = n + 1
End If
Loop
For i = 0 to m-1
For j = CLng(n/2) to 0 step -1
ar1 = Split(ar(i,n-1),ar(i,j))
If UBound(ar1) > 1 Then
For k = 1 to UBound(ar1) - 1
a = UBound(Split(ar1(k))) - 1
b = UBound(Split(ar(i,j))) - 1
If a >= 0 Then
arOut(i) = arOut(i)&Mid(Split(ar1(k))(a),2)&" "
Else
arOut(i) = arOut(i)&Mid(Split(ar(i,j))(b),2)&" "
End If
Next
Exit For
End If
Next
If UBound(Split(arOut(i))) > Max Then Max = UBound(Split(arOut(i)))
Next
For i = 0 to Max - 1
For j = 0 to m-1
If UBound(Split(arOut(j))) >= i Then
strOut = strOut & Split(arOut(j))(i) & vbTab
Else strOut = strOut & vbTab
End If
Next
strOut = Left(strOut,Len(strOut)-1) & vbCrLf
Next
fso.OpenTextFile("z" & f & ".txt",2,True).Write strOut
strOut = "" : n = "0"
Next
CreateObject("Wscript.Shell")
Function RegEx(strLine)
Set re = New RegExp
re.Pattern = "\s+"
re.Global = True
RegEx = Trim(re.Replace(strLine," "))
End Function |
-
1
评分人数
-
|