Hi,

I have a vbscript which calls a batch file. Now I want my vbscript variables to be passed to batch file. Is this possible? Please assist

Thanks,
SOW

This works, so you can write a com wrapper for it.

Option Explicit

Private Declare Function SetEnvironmentVariable Lib "kernel32" Alias "SetEnvironmentVariableA" (ByVal lpName As String, ByVal lpValue As String) As Long

Private Sub Command1_Click()

    SetEnvironmentVariable "xxx", "yyy"
    Shell "c:\test.bat"
    
    '// rem bat file
    '// echo %xxx%
    '// rem ftp wait for input so window will stay opened
    '// ftp

End Sub

Vieditorlover,

Thanks for that reply. I'm quite new to scripting and I was getting compilation error. Could please explain it in bit detail..

I will tell you the scenario and the example code i'm taking here..

My .vbs file has the following code :

dim a
dim WshShell
a=inputbox("Server Name:")
if (a= "" )then
Msgbox "Please give correct input"
Else
set WshShell=Wscript.Createobject("Wscript.shell")
wshshell.run """C:\qat.bat""" ' Here I want to pass variable a
End if

My .bat file code can as simple as

@echo off
copy ss.txt + tt.txt "%1".txt

How to use your answer in this scenario??

SOW,

Not sure why you are double-quoting your batch file name, unless it actually resides in a directory path which has embedded spaces.

If your directory path does not have embedded spaces, this will work:
wshshell.run "C:\qat.bat " & a
(ensure a space before the last double-quote and then append the variable a.)

If your directory path DOES have embedded spaces (eg, "f dir" below) then try this:
wshshell.run """C:\temp\f dir\doit.bat"" " & a
(ensure you double-up the double-quotes around the fullpath filename to the batch file.
And this will work whether you have spaces in the path or not)

In any case, it appears that your double-quote within your batch file also needs some fixing up...the double-quotes for the target file do NOT need double-spaces, unless the actual filename will have embedded spaces.

If target file will NOT have spaces in the name, this will work:
copy ss.txt + tt.txt %1.txt

If target file WILL have spaces in the name, then there will be more than one arg to the batch, so use %* to get them all, and put double-quotes after the file extension:
copy ss.txt + tt.txt "%*.txt"
(This will work whether you have spaces in the target filename or not)

Hope this helps.

Vieditorlover,

Thanks for that reply. I'm quite new to scripting and I was getting compilation error. Could please explain it in bit detail..

I will tell you the scenario and the example code i'm taking here..

My .vbs file has the following code :

dim a
dim WshShell
a=inputbox("Server Name:")
if (a= "" )then
Msgbox "Please give correct input"
Else
set WshShell=Wscript.Createobject("Wscript.shell")
wshshell.run """C:\qat.bat""" ' Here I want to pass variable a
End if

My .bat file code can as simple as

@echo off
copy ss.txt + tt.txt "%1".txt

How to use your answer in this scenario??

Yes, the directory path was having embedded space for which I used double quotes.. Will work on this soon.. I used '&', but I didnt give space between & and a. Will try your method. Thanks a lot!

That Worked.!! Thanks!
What if there are more than 1 variables in batch file, i.e 2 vb variables to be passed to a batach file?

That Worked.!! Thanks!
What if there are more than 1 variables in batch file, i.e 2 vb variables to be passed to a batach file?

SOW, I'm late in replying. Hope you figured it out already.

It's simple really...all arguments passed to a batch file are separated by spaces.

Use %1 for first argument passed to a batch file
Use %2 for 2nd argument
Use %3 for 3rd argument

Use %* to get ALL arguments in one shot.

Of course, you could also use some IF statements to test if there really is a 1st, 2nd, 3rd, etc argument, before actually trying to use the arguments.

Good luck.

Hello, this thread really helped me, but I'm having another issue along these same lines.
The file I want to use is always different. How can I get the variable to see the same directory path without throwing in the extra quotes?
ScriptFileName = Drawing_Number & ".scr"
ScriptPathandFile = """S:\DsnTools\Drawing Search Tool\UNIT ARRAY\"" " & ScriptFileName

ScriptPathandFile now equals ""S:\DsnTools\Drawing Search Tool\UNIT ARRAY\" DE000000.scr"

Notice the space and quote just before the scr file name.

Thanks in advance for your help.
Mark

SOW,

Not sure why you are double-quoting your batch file name, unless it actually resides in a directory path which has embedded spaces.

If your directory path does not have embedded spaces, this will work:
wshshell.run "C:\qat.bat " & a
(ensure a space before the last double-quote and then append the variable a.)

If your directory path DOES have embedded spaces (eg, "f dir" below) then try this:
wshshell.run """C:\temp\f dir\doit.bat"" " & a
(ensure you double-up the double-quotes around the fullpath filename to the batch file.
And this will work whether you have spaces in the path or not)

In any case, it appears that your double-quote within your batch file also needs some fixing up...the double-quotes for the target file do NOT need double-spaces, unless the actual filename will have embedded spaces.

If target file will NOT have spaces in the name, this will work:
copy ss.txt + tt.txt %1.txt

If target file WILL have spaces in the name, then there will be more than one arg to the batch, so use %* to get them all, and put double-quotes after the file extension:
copy ss.txt + tt.txt "%*.txt"
(This will work whether you have spaces in the target filename or not)

Hope this helps.

This article has been dead for over six months. Start a new discussion instead.