2

I have created batch file which include code of batch file as well as code of VBScript. Now I am trying to pass variable value from batch file to VBScript but it does not work.

echo This is batch
set /p Name="Enter Your Name: "
:x=msgbox("You have Entered '" & name & "'" ,0, "Your Title Here")
findstr "^:" "%~sf0">temp.vbs & cscript //nologo temp.vbs & del temp.vbs
echo This is batch again

Following output I am getting:

c:\Users\vshah\Desktop>echo This is batch
This is batch

c:\Users\vshah\Desktop>set /p Name="Enter Your Name: "
Enter Your Name: Vinkesh

c:\Users\vshah\Desktop>findstr "^:" "c:\Users\vshah\Desktop\Print.bat"  1>temp.vbs  & cscript //nologo temp.vbs   & del temp.vbs

c:\Users\vshah\Desktop>echo This is batch again
This is batch again

c:\Users\vshah\Desktop>

In Message Box I am getting message only -- You have Entered "
Not getting variable output

Kindly help me to pass variable from batch code to VBScript code and use them

Thank you very much in advance...

2 Answers 2

1

Simply change this:

:x=msgbox("You have Entered '" & name & "'" ,0, "Your Title Here")
findstr "^:" "%~sf0">temp.vbs & cscript //nologo temp.vbs & del temp.vbs

into this:

echo x=msgbox("You have Entered '%name%'" ,0, "Your Title Here")>temp.vbs
cscript //nologo temp.vbs & del temp.vbs

Note that you're not really passing a variable this way, but creating a temporary script that contains the literal value of that variable. If you want the VBScript to actually use a variable you need to change the code to something like this:

echo name=WScript.Arguments.Unnamed(0)>temp.vbs
echo x=msgbox("You have Entered '" ^& name ^& "'" ,0, "Your Title Here")>>temp.vbs
cscript //nologo temp.vbs "%name%"
del temp.vbs
Sign up to request clarification or add additional context in comments.

Comments

1

This generates the correct msgbox:

@echo off
setlocal enabledelayedexpansion
echo This is batch
set /p Name="Enter Your Name: "
:x=msgbox("You have Entered '__name__'" ,0, "Your Title Here")
findstr "^:" "%~sf0"> %TEMP%\str
set /p vbl=<%TEMP%\str
del %TEMP%\str >NUL
set vbl=%vbl:__name__=!name!%
rem remove the first colon
echo %vbl:~1% 1>temp.vbs  & cscript //nologo temp.vbs   & del temp.vbs

I have used a template (__name__) along with enabledelayedexpansion to be able to replace a value by a variable in a variable. I had to create another temporary file too, that I delete afterwards.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.