Not able to launch bat file form VBScript if path contains a space

馋奶兔 提交于 2019-12-01 04:19:49

问题


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

D:\My Folder\batchfiles\myFileToRun.bat

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

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

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.

Edit: 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.


回答1:


try like this:

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



回答2:


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.

So, this command

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

At the moment will be passed as;

D:\My Folder\batchfiles\myFileToRun.bat

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

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

error.

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

which will be passed as;

"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

would cause;

Expected end of statement

because it isn't a correctly terminated string.




回答3:


You can use this function to add double quotes into variables :

Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function

So, the code becomes easy for reading :

Dim WshShell, strCurrentDirectory  
Set WshShell = CreateObject("WScript.Shell")  
strCurrentDirectory = WshShell.CurrentDirectory  
wscript.echo DblQuote(strCurrentDirectory & "\My Folder\myFileToRun.bat")
WshShell.Run DblQuote(strCurrentDirectory & "\My Folder\myFileToRun.bat"),0 
Set WshShell = Nothing
'********************************************************************
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
'********************************************************************


来源:https://stackoverflow.com/questions/37254375/not-able-to-launch-bat-file-form-vbscript-if-path-contains-a-space

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!