0
votes

I Want To Get The Current Folder Path From where the vbs file is running,and store it into a variable then pass the variable to a batch file.

My VBS is:

dim fso: set fso = CreateObject("Scripting.FileSystemObject")
CD = fso.GetAbsolutePathName(".")
dim WshShell
set WshShell=Wscript.CreateObject("Wscript.shell")
WshShell.run "a.bat " & CD

My BAT Code is:

@echo off
echo get path is='%1'
pause
exit

The VbS is working but problem Is if the folder is like "c:\Program Files\" the batch file echo only "C:\Program"

How Can I Get The Full Path that passed from the vbs.

3

3 Answers

2
votes

I think you need to add a:

CD = """" & CD & """"

Which will help the string, when interpreted by the batch file, be interpreted as a whole. (You will be passing "C:\Program Files\" rather than C:\Program Files\ which will be interpreted as a.bat "C:\Program" "Files\")

For example:

'Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")
CD = """" & fso.GetAbsolutePathName(".") & """"
'Dim WshShell
'Set WshShell=Wscript.CreateObject("Wscript.shell")
'WshShell.run "a.bat " & CD

or:

'Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")
'CD = fso.GetAbsolutePathName(".")
CD = """" & CD & """"
'Dim WshShell
'Set WshShell=Wscript.CreateObject("Wscript.shell")
'WshShell.run "a.bat " & CD

or as Ekkehard.Horner and MC ND have pointed out:

'Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")
'CD = fso.GetAbsolutePathName(".")
'Dim WshShell
'Set WshShell=Wscript.CreateObject("Wscript.shell")
WshShell.run "a.bat " & """" & CD & """"

As MC ND has used, you could substitute """" for char(34) if you think it is easier to read.

To further improve readability as Ekkehard.Horner has suggested, you could create a function:

Function qq(s)
    qq = """" & s & """"
End Function

And use as:

'Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")
'CD = fso.GetAbsolutePathName(".")
'Dim WshShell
'Set WshShell=Wscript.CreateObject("Wscript.shell")
WshShell.run "a.bat " & qq(CD)
1
votes

You need to (double) quote the path

WshShell.run "a.bat " & """" & CD & """"

Otherwise each space separated part of CD will be passed as one argument.

You could use a function like

Function qq(s)
  qq = """" & s & """"
End Function

to avoid the noise in more complex concatenations.

WshShell.run "a.bat " & qq(CD)
1
votes
WshShell.run "a.bat " & Chr(34) & CD & Chr(34)
WshShell.run "a.bat """ & CD & """"

A string with a space in it, when passed to a batch file as argument, is received as two strings. So, it is necessary to enclose it in quotes to pass it as only one argument.

Then in batch file you will get the path with the quotes. So from batch file

echo %1

will show the quoted path and

echo %~1

will show the path without quotes