|
beyond125
初级用户
积分 92
发帖 35
注册 2008-3-25
状态 离线
|
『楼 主』:
求助,如何将vbs程序转换成VB语言程序
dim fso, f1, f2, regex, arr
dim str, num1, num2, num3, fn1, fn2
fn1=inputbox("请输入提取文件名","信息","1.loc")
fn2=inputbox("请输入生成文件名","信息","1.csv")
set regex=new regexp
regex.pattern="^"&inputbox("请输入包含字符的行","信息","ELCD")
regex.ignorecase=true
set fso=createobject("scripting.filesystemobject")
set f2=fso.createtextfile(fn2,true)
f2.writeline "Log"&","&"Distance"&","&"4Hz"&","&"4Hz"&","&"Depth"
f2.writeline "#"&","&"(m)"&","&"(mA)"&","&"(dBmA)"&","&"(cm)"
set f1=fso.opentextfile(fn1,1)
do while f1.atendofstream<>true
str=f1.readline
if regex.test(str) then
arr=split(str,",")
if arr(4)>0 then
num1=arr(4)*1000
num2=log(num1)/log(10)*20
num3=arr(2)*100
f2.writeline arr(1)&","&","&num1&","&round(num2,2)&","&num3&","
end if
end if
loop
f2.close
f1.close
msgbox "OK"
set f1=nothing
set f2=nothing
set regex=nothing
set fso=nothing
上面是VBS脚本程序,下面是我改的VB程序,调试时,老出错,请大家指点帮忙。谢谢
Dim fso, f1, f2, f3, f4, regex, arr
Dim str, num1, num2, num3, fn1, fn2, fn3, fn4, fn5, fn6
Private Sub Command1_Click()
CommonDialog1.Filter = "LOC文件(*.loc)|*.loc|所有文件(*.*)|*.*"
CommonDialog1.ShowOpen
fn1 = CommonDialog1.FileName
Text1.Text = fn1
''读取数据源文件用fn1文件
End Sub
Private Sub Command2_Click()
CommonDialog1.Filter = "CSV文件(*.csv)|*.csv|文本文件(*.txt)|txt|所有文件(*.*)|*.*"
CommonDialog1.ShowSave
fn6 = CommonDialog1.FileName
Text2.Text = fn6
''输出数据文件fn6文件
End Sub
Private Sub Command3_Click()
Set fso = CreateObject("scripting.filesystemobject")
Set f6 = fso.createtextfile(fn2, True)
f6.writeline "Log,Distance,4Hz,4Hz,,Depth"
f6.writeline "#,(m),(mA), (dBmA),,(cm)"
Set f1 = fso.opentextfile(fn1, 1)
Do While f1.atendofstream <> True
str = f1.readline
If regex.Test(str) Then
arr = Split(str, ",")
If arr(4) > 0 Then
num1 = arr(4) * 1000
num2 = Log(num1) / Log(10) * 20
num3 = arr(2) * 100
f6.writeline arr(1)&","&","&num1&","&round(num2,2)&","&num3&","
End If
End If
Loop
f6.Close
f1.Close
MsgBox "数据转换OK"
Set f1 = Nothing
Set f6 = Nothing
Set regex = Nothing
Set fso = Nothing
End Sub
Private Sub exitfile_Click()
Unload Me
End
End Sub
Private Sub Form_Load()
Text1.Text = ""
Text2.Text = ""
Set regex = New RegExp
End Sub
Private Sub openfile_Click()
CommonDialog1.Filter = "LOC文件(*.loc)|*.loc|所有文件(*.*)|*.*"
CommonDialog1.ShowOpen
fn1 = CommonDialog1.FileName
Text1.Text = fn1
''读取数据源文件用fn1文件
End Sub
Private Sub Option1_Click()
regex.Pattern = "^ELF"
regex.IgnoreCase = True
End Sub
Private Sub Text1_Change()
Text1.Text = fn1
End Sub
这是我写的VB程序,请大家帮忙看看哪里不对!!!
非常感谢!
|
|
2008-7-2 16:51 |
|
|
chishingchan
银牌会员
积分 1282
发帖 538
注册 2002-11-2
状态 离线
|
『第
2 楼』:
请问楼主需要转换的原因,或许从这个问题上可以帮助你解决。
|
|
2008-7-2 23:42 |
|
|
beyond125
初级用户
积分 92
发帖 35
注册 2008-3-25
状态 离线
|
『第
3 楼』:
以前用脚本是因为,给自己方便工作,现在转是因为想做的更正规一点.
|
|
2008-7-4 20:46 |
|
|
beyond125
初级用户
积分 92
发帖 35
注册 2008-3-25
状态 离线
|
『第
4 楼』:
现在我改成这样,但是怎么Private Sub Command3_Click()子程序出现不了结果了,不知是什么原因,请大家给帮助看看,哪里出错了.
Dim fso, f1, f2, f3, f4, regex, arr
Dim str, fn1, fn2, fn3, fn4, fn5, fn6
Private Sub Command1_Click()
CommonDialog1.Filter = "LOC文件(*.loc)|*.loc|所有文件(*.*)|*.*"
CommonDialog1.ShowOpen
fn1 = CommonDialog1.FileName
Text1.Text = fn1
End Sub
Private Sub Command2_Click()
CommonDialog1.Filter = "CSV文件(*.csv)|*.csv|文本文件(*.txt)|txt|所有文件(*.*)|*.*"
CommonDialog1.ShowSave
fn6 = CommonDialog1.FileName
Text2.Text = fn6
End Sub
Private Sub Command3_Click()
Set fso = CreateObject("scripting.filesystemobject")
Set f6 = fso.createtextfile(fn6, True)
f6.writeline "Log,Distance,4Hz,4Hz,,Depth"
f6.writeline "#,(m),(mA), (dBmA),,(cm)"
Set f1 = fso.opentextfile(fn1, 1)
Do While f1.atendofstream <> True
str = f1.readline
If regex.Test(str) Then
arr = Split(str, ",")
If arr(4) > 0 Then
num1 = arr(4) * 1000
num2 = Log(num1) / Log(10) * 20
num3 = arr(2) * 100
f6.writeline arr(1), , num1, Round(num2, 2), , num3
End If
End If
Loop
f6.Close
f1.Close
MsgBox "数据转换OK"
Set f1 = Nothing
Set f6 = Nothing
Set regex = Nothing
Set fso = Nothing
End Sub
Private Sub exitfile_Click()
Unload Me
End
End Sub
Private Sub Form_Load()
Text1.Text = ""
Text2.Text = ""
Set regex = New RegExp
End Sub
Private Sub openfile_Click()
CommonDialog1.Filter = "LOC文件(*.loc)|*.loc|所有文件(*.*)|*.*"
CommonDialog1.ShowOpen
fn1 = CommonDialog1.FileName
Text1.Text = fn1
End Sub
Private Sub Option1_Click()
regex.Pattern = "^ELF"
regex.IgnoreCase = True
End Sub
Private Sub Text1_Change()
Text1.Text = fn1
End Sub
|
|
2008-7-4 20:48 |
|
|
beyond125
初级用户
积分 92
发帖 35
注册 2008-3-25
状态 离线
|
『第
5 楼』:
对了,现在总是提示f6.writeline arr(1), , num1, Round(num2, 2), , num3这个出错,不知,在VB中这个程序该如可修改,
在VBS中这行是这样的f2.writeline arr(1)&","&","&num1&","&round(num2,2)&","&num3&","
我在VB中改为了f6.writeline arr(1), , num1, Round(num2, 2), , num3
在VB中writeline 的表示和VBS中有什么区别?
|
|
2008-7-4 20:55 |
|
|