简体   繁体   English

VBS如何使用带空格的字符串变量调用cmd.exe

[英]VBS How to call cmd.exe using a string variable with spaces

I need to call the following: 我需要调用以下内容:

set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "cmd /c copy /y C:\input\" & WScript.Arguments(0) & " C:\output", 0

where the input argument may be "File Name.txt". 其中输入参数可以是“File Name.txt”。 I have seen countless examples of people doing the same thing using double quotes for a hard coded file location, but nothing using an input argument or variable. 我已经看到无数人使用双引号为硬编码文件位置做同样的事情,但没有使用输入参数或变量。 What syntax is required so that the command line receives: 需要什么语法,以便命令行接收:

copy /y "C:\input\File Name.txt" C:\output

and not 并不是

copy /y C:\input\File Name.txt C:\output

for an arbitrary file name? 对于任意文件名?

Embed the needed quotes (escaped via doubling) in the surrounding literals: 在周围的文字中嵌入所需的引号(通过加倍转义):

WshShell.Run "cmd /c copy /y ""C:\input\" & WScript.Arguments(0) & """ C:\output", 0

background , further reading 背景进一步阅读

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM