Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using variables from VBScript in batch

I want to pass variables from VBScript to batch but it wouldn't work.

My VBScript:

Dim shell  
Set shell = CreateObject("WScript.Shell")
strnaam = InputBox ("naam")

and my batch:

@echo off
cls
echo %strnaam%
pause

I want the variable strnaam from my VBScript to my batch.

like image 398
Klaas Weerstand Avatar asked Jul 21 '26 19:07

Klaas Weerstand


2 Answers

The most obvious way would be to run the vbscript as a For /F command and save its returned output as a variable:

@Echo Off

:NaamBox
Set "naam="
(Echo WScript.Echo InputBox("Naam:"^))>"%TEMP%\naam.vbs"
For /F Delims^=^ EOL^= %%A In ('CScript //NoLogo "%TEMP%\naam.vbs"')Do Set "naam=%%A"
If Not Defined naam GoTo NaamBox
Del "%TEMP%\naam.vbs"

Echo Uw naam is %naam%
Pause

If you don't like the idea of writing, running, then deleting the file, you could also embed your VBScript within the batch file:

<!-- :
@Echo Off
Echo Typ gelieve uw naam in de popup doos en OK te selecteren
For /F Delims^=^ EOL^= %%A In ('CScript //NoLogo "%~f0?.wsf"')Do Set "naam=%%A"
If Defined naam Echo Uw naam is %naam%&Pause
Exit /B
-->
<Job><Script Language="VBScript">
    WScript.Echo InputBox("Naam:")
</Script></Job>
like image 193
Compo Avatar answered Jul 23 '26 12:07

Compo


You can pass the variables only via environment variables:

  1. The create_variable.vbs file:
Dim WshShell, WshEnv
Set WshShell = CreateObject("WScript.Shell")
Set WshEnv = WshShell.Environment("USER") ' can be either SYSTEM, USER, VOLATILE OR PROCESS
' current value
WScript.Echo "Process: NAAM=" & WshEnv.Item("NAAM")
WshEnv("NAAM") = "This text will appear in batch"
WScript.Echo "Process: NAAM=" & WshEnv.Item("NAAM")
Set WshEnv = Nothing
Set WshShell = Nothing
  1. Then the batch file show_vbs_variable.bat (you have to open a new cmd.exe to have the new variable there! If you need more infor here is a topic on SO that covers it.:
@echo off
cls
echo %naam%
pause
  1. vbs script for clearing up the variable clearing_variable.vbs:
Dim WshShell, WshEnv
Set WshShell = CreateObject("WScript.Shell")
Set WshEnv = WshShell.Environment("USER") ' can be either SYSTEM, USER, VOLATILE OR PROCESS
' current value
WScript.Echo "Process: NAAM=" & WshEnv.Item("NAAM")
'Deleting the env variable
WshEnv.Remove("NAAM")
WScript.Echo "Process: NAAM=" & WshEnv.Item("NAAM")
Set WshEnv = Nothing
Set WshShell = Nothing
like image 21
tukan Avatar answered Jul 23 '26 13:07

tukan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!