How do you pass parameters containing spaces in vbs

前端 未结 1 1976
名媛妹妹
名媛妹妹 2020-12-21 15:55

I have a batch script:

test.bat

echo first arg is %1
pause

That I want to invoke from a vbscript with admin rights like so:

相关标签:
1条回答
  • 2020-12-21 16:37

    The way to get this working is:

    UAC.ShellExecute "cmd", "/c """"c:\test.bat"" ""has spaces""""", "", "runas", 1
    

    The important thing to note when using runas to invoke admin user rights, is that the working directory will change to c:\windows\system32. Therefore, you need to specify the full path to the batch file so that it can be found.

    I'm still not sure why this only works when passing the bat file as an argument to "cmd", rather than executing it directly.

    All credit due to Alex K so if this helped you, upvote his comments.

    In this case you need to quote the entire set of arguments to cmd, and each of the arguments also. You also need to double up the quotes. It looks a bit mad, but this is the best solution because otherwise it will fail if you have a space in any arguments and the bat file path.

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