VBScript参数中的双引号

时间:2010-11-16 08:46:31

标签: vbscript

当我为自己阅读和体验时,VBScript会删除所有双引号和参数。有没有人知道这方面的方法?如何将双引号传递给脚本?

7 个答案:

答案 0 :(得分:11)

如果该参数需要引号,您可以使用命名参数来识别它,然后用双引号括起该值

dim arg
if WScript.Arguments.Named.Exists("a") then
    arg = WScript.Arguments.Named("a")
    arg = chr(34) & arg & chr(34)
end if

并因此使用:

cscript test.vbs /a:"a parameter"

但如果您只想提供引号,这无济于事。单引号,但您也可以使用单引号(或其他字符/字符串)并执行Replace(arg, "'", chr(34))转换为双引号。

答案 1 :(得分:7)

这个脚本将按原样获取命令行,使用双引号和一个名为strLine的变量,并显示它:

Set objSWbemServices = GetObject("WinMgmts:Root\Cimv2") 
Set colProcess = objSWbemServices.ExecQuery("Select * From Win32_Process") 
For Each objProcess In colProcess 
    If InStr (objProcess.CommandLine, WScript.ScriptName) <> 0 Then 
        strLine = Mid(objProcess.CommandLine, InStr(objProcess.CommandLine , WScript.ScriptName) + Len(WScript.ScriptName) + 1)
    End If 
Next
WScript.Echo(strLine)

通过以下方式运行:

cscript scriptname.vbs "option" ""other option"" third option

会导致:

"option" ""other option"" third option

答案 2 :(得分:2)

您可以获取https://stackoverflow.com/a/13212628/1752986

中的当前PID,而不是检查命令行是否包含WScript.ScriptName

答案 3 :(得分:1)

编辑:在这里误解了这个问题的新答案:

我认为你不能以任何方式做到这一点。但是,解决方法可能是使用Win32_Process类的CommandLine属性,这应该可以为您提供完整的命令行。

例如,尝试这个脚本:

Set wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set processes = wmi.ExecQuery("SELECT * FROM Win32_Process")
For Each proc in processes
    If InStr(proc.CommandLine, "double quotes") > 0 Then
        wscript.echo proc.CommandLine
    End IF
Next

参数为:"some long commandline enclosed in double quotes here"

答案 4 :(得分:1)

我不确定这是否有效,但在传递参数时我猜你可以做类似的事情 -

chr(34) + <your argument string> + chr(34)

chr(34)代表双引号。

答案 5 :(得分:0)

不幸的是,我不知道任何传递双引号的转义方法,因为它是一个参数分隔符。我可以建议的是修改你的脚本,或者从那里添加引号。

答案 6 :(得分:0)

这里的答案借鉴/结合了其他一些人,并根据脚本的pid查询流程信息:

Function ArgumentsString()
    Dim nPid : nPid = ThisProcessId()
    For Each oPrc In GetObject("winmgmts:\\.\root\cimv2").ExecQuery(_
        "Select * From Win32_Process Where ProcessId=" & nPid ) 
    Exit For : Next
    ArgumentsString = Mid( oPrc.CommandLine, _
        InStr(oPrc.CommandLine, WScript.ScriptName) + _
        Len(WScript.ScriptName) + 1 )
End Function 

Function ThisProcessId()
    ThisProcessId = 0
    Dim sTFile, oPrc
    With CreateObject("Scripting.FileSystemObject")
        sTFile = .BuildPath(.GetSpecialFolder(2), "sleep.vbs")
        With .OpenTextFile(sTFile, 2, True)
            .Write "WScript.Sleep 1000"
        End With
    End With
    With CreateObject("WScript.Shell").Exec("WScript " & sTFile)
        For Each oPrc In GetObject("winmgmts:\\.\root\cimv2").ExecQuery(_
        "Select * From Win32_Process Where ProcessId=" & .ProcessID)
        Exit For : Next
        ThisProcessId = oPrc.ParentProcessId
    End With
End Function
相关问题