User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Visual Basic 4 / 5 / 6 section within the Software Development category of DaniWeb, a massive community of 455,988 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,790 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums
Views: 4914 | Replies: 6
Reply
Join Date: Dec 2007
Posts: 5
Reputation: SOW is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
SOW SOW is offline Offline
Newbie Poster

Question Passing VBscript varibales to batch file..

  #1  
Dec 6th, 2007
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2007
Posts: 47
Reputation: VIeditorlover is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
VIeditorlover's Avatar
VIeditorlover VIeditorlover is offline Offline
Light Poster

Re: Passing VBscript varibales to batch file..

  #2  
Dec 6th, 2007
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
Last edited by VIeditorlover : Dec 6th, 2007 at 4:55 am.
Reply With Quote  
Join Date: Dec 2007
Posts: 5
Reputation: SOW is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
SOW SOW is offline Offline
Newbie Poster

Re: Passing VBscript varibales to batch file..

  #3  
Dec 7th, 2007
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??
Reply With Quote  
Join Date: Jun 2007
Posts: 3
Reputation: gng is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
gng gng is offline Offline
Newbie Poster

Solution Re: Passing VBscript varibales to batch file..

  #4  
Dec 8th, 2007
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.

Originally Posted by SOW View Post
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??
Reply With Quote  
Join Date: Dec 2007
Posts: 5
Reputation: SOW is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
SOW SOW is offline Offline
Newbie Poster

Re: Passing VBscript varibales to batch file..

  #5  
Dec 10th, 2007
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!
Reply With Quote  
Join Date: Dec 2007
Posts: 5
Reputation: SOW is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
SOW SOW is offline Offline
Newbie Poster

Re: Passing VBscript varibales to batch file..

  #6  
Dec 19th, 2007
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?
Reply With Quote  
Join Date: Jun 2007
Posts: 3
Reputation: gng is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
gng gng is offline Offline
Newbie Poster

Re: Passing VBscript varibales to batch file..

  #7  
Jan 9th, 2008
Originally Posted by SOW View Post
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Visual Basic 4 / 5 / 6 Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the Visual Basic 4 / 5 / 6 Forum

All times are GMT -4. The time now is 9:32 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC