标题: [问题求助] 【已解决】vbs:加循环语句出错 [打印本页]
作者: 思想之翼 时间: 2013-10-27 17:25 标题: 【已解决】vbs:加循环语句出错
本帖最后由 思想之翼 于 2022-11-2 19:23 编辑
下列VBS代码仅对一个文本1.txt进行操作,结果输出为z1.txt。若有300个文本,就要制作300个类似的VBS,欲使之能够依次对1.txt 2.txt 3.txt...300.txt操作(1.txt 2.txt 3.txt...300.txt数据格式一样),结果依次输出为Z1.txt Z2.txt Z3.txt...Z300.txt 所以我尝试加了一句循环代码,不想出错了
Set fso = CreateObject("Scripting.FileSystemObject")
For x = 1 to 300
Set file = fso.OpenTextFile(x & ".txt")
'原先是这句:Set file = fso.OpenTextFile("1.txt")
m = 1 : 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" & x & ".txt",2,true).Write strOut
'原先是这句: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
Next x
作者: apang 时间: 2013-10-27 18:18
本帖最后由 apang 于 2013-10-27 18:29 编辑
再把 CreateObject("Wscript.Shell") 这一行改成下面两行
strOut = "" : n = ""
Next
作者: 思想之翼 时间: 2013-10-27 18:44
回复 2# apang
谢谢您的帮助
按您指点修改了代码,还显示出错:
z1.txt文本可以输出,之后就出现下面的出错警告。我看了一下,z1.txt比z2.txt多1行数据,比z3.txt多2行数据,...可能z2.txt z3.txt...z300.txt数据行数与z1.txt不一致,导致出错。如何修改?恳望指点。
作者: apang 时间: 2013-10-27 20:25
本帖最后由 apang 于 2013-10-27 20:26 编辑
哦,貌似错了
strOut = "" : n = 0
Next
试试
作者: 思想之翼 时间: 2013-10-28 00:07
回复 4# apang
谢谢您给予的大力帮助!
作者: apang 时间: 2013-10-28 12:53
是这个帖子吧?http://www.bathome.net/viewthread.php?tid=23404&highlight=
1.txt~300.txt列数必须相同,行数可以不相同。- Set fso = CreateObject("Scripting.FileSystemObject")
- m = 2 'm=总列数
- For x = 1 to 300
- Set file = fso.OpenTextFile(x & ".txt")
- fso.OpenTextFile("z" & x & ".txt",2,true).Write GetStr(file)
- file.Close : Set file = Nothing
- Next
-
- Function GetStr(file)
- ReDim arIn(m-1) : ReDim arOut(m-1)
- Max = 0 : n = 0
- 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
-
- GetStr = strOut
- End Function
-
- Function RegEx(strLine)
- Set re = New RegExp
- re.Pattern = "\s+"
- re.Global = True
- RegEx = Trim(re.Replace(strLine," "))
- End Function
复制代码
欢迎光临 批处理之家 (http://bathome.net./) |
Powered by Discuz! 7.2 |