Board logo

标题: [求助]接受命令行参数的VBS发生错误,请指正 [打印本页]

作者: qinbuer     时间: 2007-9-4 01:39    标题: [求助]接受命令行参数的VBS发生错误,请指正

写了一个接受命令行参数的VBS,用于读取文本的某一行,比如输入cscript

test.vbs test.txt 1即读取test.txt的第一行字符串,但是不知道哪里出错了,请各

位达人帮忙看下:

If WScript.Arguments.Count=0 Then
  WScript.Echo "Sorry,参数错误."
  WScript.Quit
End If

Dim arr()
Dim objFSO,objFile,WSH
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WSH=WScript.Arguments
Set objFile = objFSO.OpenTextFile(WSH(0), 1)
Do Until objFile.AtEndOfStream
  For i=1 To WSH.Count-1
    If objFile.Line = WSH(i) Then
      theline=objFile.ReadLine
      WScript.Echo theline
    End If
  Next
  objFile.SkipLine
Loop
objFile.Close
作者: slore     时间: 2007-9-4 12:48
If WScript.Arguments.Count = 0 Then
   
WScript.Echo "Sorry,参数错误."
    WScript.Quit
End If

Dim
objFSO,objFile,WSArg
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WSArg = WScript.Arguments
Set objFile = objFSO.OpenTextFile(WSArg(0), 1)
Do Until objFile.AtEndOfStream
    If objFile.Line = Int(WSArg(1)) Then
        
WScript.Echo objFile.ReadLine
        Exit Do
    End If
   
objFile.SkipLine
Loop
objFile.Close

Set
objFile = Nothing
Set
WSArg = Nothing
Set
objFSO = Nothing
作者: qinbuer     时间: 2007-9-4 16:47
又是slore兄的热心指点,实在非常感谢。^_^
作者: qinbuer     时间: 2007-9-4 16:52
不好意思,有一问题没有指明,就是我的命令行参数不只一个,其数目不能确定,比

如cscript test.vbs test.txt 1 2 3即读取test.txt文本的1、2、3行,而兄的代码只能

接受除了指定文本之外的一个参数。
作者: slore     时间: 2007-9-4 17:09
If WScript.Arguments.Count < 2 Then
   
WScript.Echo "Sorry,参数错误."
    WScript.Quit
End If

Dim
objFSO,objFile,WSArg,NotSkip
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WSArg = WScript.Arguments
Set objFile = objFSO.OpenTextFile(WSArg(0), 1)
Do Until objFile.AtEndOfStream
    NotSkip = False
    For
i = 1 To WSArg.Count - 1
        If objFile.Line = Int(WSArg(i)) Then
            
WScript.Echo objFile.ReadLine
            
NotSkip = True
            Exit For
        End If
    Next
    If
NotSkip = False Then objFile.SkipLine
Loop
objFile.Close

Set
objFile = Nothing
Set
WSArg = Nothing
Set
objFSO = Nothing
作者: slore     时间: 2007-9-4 17:13
行数按大小顺序写参数……虽然可以有随便写的。。。不过还是就这样了不改了
作者: qinbuer     时间: 2007-9-5 12:04
不胜感激!