Luiz Vaughan
Luiz Vaughan

Reputation: 675

VBScript and Batch interaction

I am running a batch script and somewhere the user has to access a database.

At this moment, a window made in vbscript would prompt asking the user to type in the login and password. (OK, Cancel buttons)

If the credentials are correct after the OK, the batch would continue according to planA, otherwise the batch would do something else going to planB. If (Cancel), it would return to the batch and the main menu.

THIS IS WHAT I HAVE BEEN STRUGGLING WITH:

@echo off 

:Ini 
echo   [1] Access database 
echo   [2] Main menu 
echo: 
set /p Quest= What do you prefer (1 / 2)?        
if not '%Quest%'=='' set Quest=%Quest:~0,1%  
if '%Quest%'=='1' goto VBS 
if '%Quest%'=='2' goto BATCH 
echo Invalid option, please try again 
cls 
goto Ini 

:BATCH 
echo Heading for main menu ... 
goto Main 

:VBS 
:wscript.echo InputBox("Enter your password","VBScript-Batch") 
findstr "^:" "%~sf0" | findstr /i /v ":Label" >temp.vbs 
for /f "delims=" %%N in ('cscript //nologo temp.vbs') do set pass=%%N  
del temp.vbs 

:Label1 

If %pass%=="okay" echo Valid Password ! & goto PLAN-A
If not %pass%=="okay" echo Invalid Password !! & goto PLAN-B 

:PLAN-A
echo continue from here 

:PLAN-B
echo do something else

(...)

-- How to capture the user information, validate it and go back to the batch for the planA or planB ??

As you see, if we eliminate the "& goto PLAN" stuff the script works. It sends the VBS input "pass" to the batch and the batch echoes "continue from here" or "do something else", from where the rest of your code should continue in the same batch.

However, it is not working ... Any help to make this really work ?

Upvotes: 2

Views: 1624

Answers (1)

dbenham
dbenham

Reputation: 130809

Your primary issue was you didn't set up the file properly to facilitate extracting the VBS from the batch file. Your VBS looks no different than a batch label. You filter out "Label" labels, but you still include lines like :ini, :BATCH, etc. Obviously those will trip up VBS. I solved the problem by prefixing your VBS with ::: and adapting your filter. There is no need to explicitly filter out any labels. I chose 3 colons because a single colon is used for a label, and 2 colons is frequently used for comments. You could have multiple independent VBS scripts embedded within your batch simply by varying the number of preceding colons.

I also restructured the code a small amount, and sprinkled in some EXIT /B statements so that code does not fall through. Also your :MAIN is not defined so I commented out the GOTO and replaced it with EXIT /B.

@echo off
:Ini
echo   [1] Access database
echo   [2] Main menu
echo:
set /p Quest= What do you prefer (1 / 2)?
if not '%Quest%'=='' set Quest=%Quest:~0,1%
if '%Quest%'=='1' goto VBS
if '%Quest%'=='2' goto BATCH
echo Invalid option, please try again
cls
goto Ini

:BATCH
echo Heading for main menu ...
::goto Main
exit /b

:VBS
:::wscript.echo InputBox("Enter your password","VBScript-Batch")
findstr "^:::" "%~sf0" >temp.vbs
for /f "delims=" %%N in ('cscript //nologo temp.vbs') do set pass=%%N
del temp.vbs
If "%pass%"=="okay" (
  echo Valid Password !
    goto PLAN-A
) else (
  echo Invalid Password !!
    goto PLAN-B
)

:PLAN-A
echo continue from here
exit /b

:PLAN-B
echo do something else
exit /b

Upvotes: 4

Related Questions