Search code examples
batch-filevbscript

VBScript redirects user to label


I have recently started coding in VBScript and I have a work where I have a batch file called Game.bat located in the folder C:\Velocity DK\My Game.

In my game, the user gets to login to the app. The basic script works just fine, but here is the problem. I have a .vbs file that opens when the user inputs wrong information to the batch file. Though, I can't find a way to make the VBS file redirect to the label I want the user to be going on depending on which button the user pressed.

The batch file looks a bit similar to this:

@echo off
:RETRY
:MAIN
cls
echo Please input valid information.
echo.
set /p username=Username: 
set /p password=Password: 
if exist %USERPROFILE%\Game\"%username%"\username.sav if exist %USERPROFILE%\Game\"%username%"\username.sav goto game
if not exist %USERPROFILE%\Game\"%username%"\username.sav if not exist %USERPROFILE%\Game\"%username%"\username.sav (
    cls
    start /wait "" C:\"Velocity DK"\"My Game"\Invalid.vbs
)
goto MAIN

:: My Game's code is located here

:FORCE_QUIT
cls
exit /force
goto FORCE_QUIT

My VBS file looks like this:

returnvalue = MsgBox "Do. you want to retry?",4,"My Game"
if returnvalue = 7 then
    ' Some code to redirect to batch file label :FORCE_EXIT
    WScript.Quit
else
    ' Some code to redirect to the batch file label :RETRY
end if

In brief, I would like to know how I could make it so that when the user presses the yes button, he gets taken back to the :RETRY label of the batch file other wise, he will be taken to the :FORCE_QUIT label. How can I do so?


Solution

  • Can you use the argument/parameter to call your file.bat from your file.vbs to do this?...

    Basically, with some cope editions the VBS and BAT file, to handle the argument/parameter and behave from these values..

    The bat code above, will write the vbs file for you and execute the vbs.

    Call from vbs: file.bat :label

    @echo off 
    for %%a in (%*) do if /i "%~1" == "%%~a" goto %%~a
    
    >"%temp%\Q_SO55201139.vbs" ^
        (
         echo/ Option Explicit
         echo/ Dim WshShell,StrArg0, StrArg1, StrArg2, Result
         echo/ Set WshShell = Wscript.CreateObject^("Wscript.Shell"^)
         echo/ StrArg0 = Chr^(34^) ^& "%~f0" ^& Chr^(34^)
         echo/ StrArg1 = Chr^(34^) ^& ":RETRY" ^& Chr^(34^)
         echo/ StrArg2 = Chr^(34^) ^& ":FORCE_QUIT" ^& Chr^(34^)
         echo/ Result = MsgBox^("Do you want to retry?",vbyesno,"My Game"^)
         echo/ if  Result = VbNo then
         echo/     WshShell.Run ^(StrArg0 ^& StrArg2^),1,True
         echo/ else
         echo/     WshShell.Run ^(StrArg0 ^& StrArg1^),1,True
         echo/ end if
        )
        
    :RETRY
    
    :MAIN
    
    cls
    echo Please input valid information.
    echo.
    
    set /p "username=Username: "
    set /p "password=Password: "
    
    if exist "%USERPROFILE%\Game\"%username%"\username.sav" if exist "%USERPROFILE%\Game\"%username%"\username.sav" goto :game
    if not exist "%USERPROFILE%\Game\"%username%"\username.sav" if not exist "%USERPROFILE%\Game\"%username%"\username.sav" (
    
        cls & start "" /w "%Windir%\System32\wScript.exe" //nologo "C:\Velocity DK\My Game\Invalid.vbs"
    
        )
    
    start "" /w "%Windir%\System32\wScript.exe" //nologo "%temp%\Q_SO55201139.vbs" && exit /b
    
    goto MAIN
    
    :: My Game's code is located here
    
    :FORCE_QUIT
    cls & exit /force
    
    goto :FORCE_QUIT
    
    del /q /f "%temp%\Q_SO55201139.vbs" 2>nul >nul
    

    Vbs file code

    Option Explicit
    Dim WshShell,StrArg0, StrArg1, StrArg2, Result
    Set WshShell = Wscript.CreateObject("Wscript.Shell")
    StrArg0 = Chr(34) & "game_cmd_vbs.cmd" & Chr(34)
    StrArg1 = Chr(34) & ":RETRY" & Chr(34)
    StrArg2 = Chr(34) & ":FORCE_QUIT" & Chr(34)
    Result = MsgBox("Do you want to retry?",vbyesno,"My Game")
    if  Result = VbNo then
        WshShell.Run (StrArg0 & StrArg2),1,True
    else
        WshShell.Run (StrArg0 & StrArg1),1,True
    end if
    

    Obs.: You need add fullpath in line 5th: c:\full\path\to\file\game_bat_vbs.cmd

    Sorry my limited English!