calling vbscript from another vbscript file passing arguments

前端 未结 3 1563
耶瑟儿~
耶瑟儿~ 2020-12-12 00:08

I am using the below script to call another script .The issue is I have to pass the arguments which I retrieve by WScript.Arguments to the second script that I am calling .c

相关标签:
3条回答
  • 2020-12-12 00:53

    You need to build your argument list with proper quoting of the arguments. You also need to differentiate between named and unnamed arguments. At the very minimum, all arguments with spaces in them must be put between double quotes. It doesn't hurt, though, to simply quote all arguments, so you could do something like this:

    Function qq(str)
      qq = Chr(34) & str & Chr(34)
    End Function
    
    arglist = ""
    With WScript.Arguments
      For Each arg In .Named
        arglist = arglist & " /" & arg & ":" & qq(.Named(arg))
      Next
      For Each arg In .Unnamed
        arglist = arglist & " " & qq(arg)
      Next
    End With
    
    CreateObject("WScript.Shell").Run "TestScript.vbs " & Trim(arglist), 0, True
    
    0 讨论(0)
  • 2020-12-12 00:53

    Use:

    objShell.Run "TestScript.vbs arg1 arg2"
    

    If one of the arguments contains spaces then you will need to embed these in quotes, probably like this:

    objShell.Run "TestScript.vbs arg1 arg2 ""this is three"""
    

    or it may accept apostrophes (I haven't tried this recently).

    0 讨论(0)
  • 2020-12-12 01:01

    I found the answers a little confusing, so here is mine, which in my mind shows it more simply. The other answer aren't wrong, just different (slightly).

    in test.vbs file:

    Set shell = CreateObject("WScript.Shell")
    shell.CurrentDirectory = "C:\some\path\"
    x = "testing"
    shell.Run "test1.vbs " & x 
    

    in C:\some\path\test1.vbs file:

    x = WScript.Arguments.Item(0) 
    msgbox x
    

    resulting message box from test.vbs file, passed to test1.vbs file:

    testing
    
    0 讨论(0)
提交回复
热议问题