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