【问题标题】:Double quotes in VBScript argumentVBScript 参数中的双引号
【发布时间】:2010-11-16 08:46:30
【问题描述】:

正如我自己阅读和体验的那样,VBScript 会从参数中删除所有双引号。有谁知道解决这个问题的方法?如何将双引号传递到脚本中?

【问题讨论】:

  • 你的意思是在 Windows Script Host 中吗?

标签: vbscript


【解决方案1】:

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

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)) 转换为双引号。

【讨论】:

    【解决方案2】:

    此脚本将按原样获取命令行,将双引号和所有内容添加到名为 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
    

    【讨论】:

      【解决方案3】:

      您可以像在https://stackoverflow.com/a/13212628/1752986 中一样获取当前 PID,而不是检查包含 WScript.ScriptName 的命令行

      【讨论】:

        【解决方案4】:

        编辑:误解了这个问题,所以这里有新的答案:

        我认为你不能以任何方式做到这一点。但是,解决方法可能是使用 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"

        【讨论】:

        • 我认为@Dani 的意思是作为脚本的参数,即cscript.exe test.vbs "parameter"
        • 他的意思是:C:\&gt; test.vbs "argument".
        • @Ruel @oracle 谢谢,我的错。
        • 那行得通。现在剩下的就是获取正在运行的 VBS 进程的 PID :)
        • 如果你有兴趣,它是如何工作的: Set objSWbemServices = GetObject ("WinMgmts:Root\Cimv2") Set colProcess = objSWbemServices.ExecQuery _ ("Select * From Win32_Process") For Each objProcess在 colProcess If InStr (objProcess.CommandLine, WScript.ScriptName) 0 Then WScript.Echo objProcess.Name, _ objProcess.ProcessId, _ objProcess.CommandLine End If Next
        【解决方案5】:

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

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

        chr(34) 代表双引号。

        【讨论】:

        • 不幸的是,这只会导致字符串文字 chr(34) 显示为参数(或参数的一部分)。例如,如果唯一的参数是 chr(34)+foo+chr(34),那么解析后的参数就是那个。
        • @John L Veazey - 这正是 OP 从他的问题中想要的,对吧?我不明白你的意思。对不起。
        【解决方案6】:

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

        【讨论】:

          【解决方案7】:

          这里的答案借鉴/结合了其他一些,并根据脚本的 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
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-03-24
            • 2013-06-22
            • 2018-08-03
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多