如果路径包含空格,则无法启动bat文件形式的VBScript [英] Not able to launch bat file form VBScript if path contains a space

查看:106
本文介绍了如果路径包含空格,则无法启动bat文件形式的VBScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我一直试图从路径启动myFileToRun.bat文件.

I have been trying to launch myFileToRun.bat file from a path for example.

D:\My Folder\batchfiles\myFileToRun.bat

下面是我试图通过它运行的VBScript.

Below is the VBScript through which I'm trying to run it.

Dim WshShell, strCurrentDirectory  
Set WshShell = CreateObject("WScript.Shell")  
strCurrentDirectory    = WshShell.CurrentDirectory  
WshShell.Run strCurrentDirectory & "\myFileToRun.bat" , 0  
Set WshShell = Nothing

我无法运行它,因为VBS在系统找不到指定的文件"中出现错误提示 我已经尝试过stackoverflow和其他论坛的其他类似文章中提到的各种方法.他们中的大多数人都说要加双引号.

I'm not able to run it, as VBS is thowing error sayin "System cannot find the file specified" I have tried various methods mentioned in other similar posts in stackoverflow and other forums. Most of them say to add double quotes.

注意:我已经尝试添加双引号,例如,双引号

Note: I have tried adding double quotes, double double quotes for example

1.WshShell.Run """strCurrentDirectory & "\myFileToRun.bat" "", 0
2.WshShell.Run """"strCurrentDirectory & "\myFileToRun.bat"""", 0
3.WshShell.Run """"strCurrentDirectory & \myFileToRun.bat"""", 0

他们都不起作用,因为我猜我在错误的地方给出了报价.变量strCurrentDirectory更改为"strCurrentDirectory",而不是"D:\ My Folder \ batchfiles",因此我的最终路径将是"strCurrentDirectory""\ myFileToRun.bat",这是错误的.我需要将路径设置为"D:\ My Folder \ batchfiles \ myFileToRun.bat"并运行该文件.

None of them worked as I guess I am giving quotes at wrong places. The variable strCurrentDirectory changes to "strCurrentDirectory" instead of "D:\My Folder\batchfiles" and so my final path would be "strCurrentDirectory""\myFileToRun.bat", which is wrong. I need the path to be "D:\My Folder\batchfiles\myFileToRun.bat" and the file to run.

我还不太了解这些加引号的概念. 所以,请您能向我解释这个概念,并帮助我解决此错误.

I haven't really understood the concept of these adding quotes. So can you please explain me this concept and help me in resolving this error.

我已经找到了解决方案.因为必须引用当前目录,所以我使脚本更简单.

I have got the solution. As I had to refer to the current directory, I just made the script simpler.

Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "myFileToRun.bat", 0
Set WshShell = Nothing

谢谢大家的回答.很有帮助.

Thank you all for your answers. It was helpful.

推荐答案

您只需要正确地对字符串中的引号进行转义,规则是每当您要在字符串中显示双引号的情况下.

You just need to escape the quotes in the string correctly, the rule is whenever you want to show a quote in a string double it.

所以,这个命令

WshShell.Run strCurrentDirectory & "\myFileToRun.bat" , 0

此刻将被传递为

D:\My Folder\batchfiles\myFileToRun.bat

与大多数Windows程序一样,在传递Run()时,带有空格的路径在传递时需要用引号引起来,以确保识别出完整路径

As with most Windows programs paths with spaces need to be surrounded with quotes when passed to ensure the full path is recognised, at the moment Run() is seeing

D:\My

,其余作为参数传递给My程序.由于系统无法找到My程序,因此会导致

and the rest as an argument passed to the My program. As the system won't be able to find the My program it will cause the

系统找不到指定的文件

System cannot find the file specified

错误.

要解决此问题,我们需要传递一个带引号的字符串(将要包含的每个文字引号的引号加倍)

To fix this we need to pass a quoted string (doubling the quotes for each literal quote we want to include)

WshShell.Run """" & strCurrentDirectory & "\myFileToRun.bat""", 0

将作为;

"D:\My Folder\batchfiles\myFileToRun.bat"

注意:在与变量串联时,我们仍然需要正确打开和关闭字符串.对于上述情况,我们将字符串""""连接到一个变量上,该变量简单地是"",但带有一个引号和结尾引号来表示文字字符串.

Note: When concatenating with variables we still need to open and close the string correctly. For a situation like above we concatenate the string """" on to a variable, which is simply "" but with a opening and closing quote to denote the literal string.

还需要将变量正确地连接到字符串,否则您将得到怪异的结果,例如您首次尝试对字符串进行转义

Also variables need to be correctly concatenated to the string or you will get weird results, for example your first attempt to escape the string

WshShell.Run """strCurrentDirectory & "\myFileToRun.bat" "", 0

会引起原因;

语句的预期结尾

Expected end of statement

因为它不是正确终止的字符串.

because it isn't a correctly terminated string.

这篇关于如果路径包含空格,则无法启动bat文件形式的VBScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆