VBScript 将用户重定向到标签

VBScript redirects user to label

我最近开始使用 VBScript 编码,我有一个名为 Game.bat 的批处理文件位于 C:\Velocity DK\My Game.

文件夹中

在我的游戏中,用户可以登录应用程序。基本脚本工作得很好,但问题就在这里。当用户向批处理文件输入错误信息时,我有一个 .vbs 文件打开。但是,我无法找到一种方法使 VBS 文件重定向到我希望用户根据用户按下的按钮继续操作的标签。

批处理文件看起来有点像这样:

@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

我的 VBS 文件如下所示:

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

简而言之,我想知道如何做到这一点,以便当用户按下“是”按钮时,他会被带回批处理文件的 :RETRY 标签,否则,他将带到 :FORCE_QUIT 标签。我该怎么做?

你可以使用参数/参数来调用你的file.bat从你的 file.vbs 做这个?...

基本上,对于一些副本,VBSBAT 文件,以处理 argument/parameter 并根据这些值进行操作..

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" equ "%%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

对不起我的有限英语!

选择

我会用 CHOICE 命令处理这个问题:

CHOICE /M "Do. you want to retry?"
IF %ERRORLEVEL% EQU 1 GOTO RETRY
IF %ERRORLEVEL% EQU 2 GOTO FORCE_QUIT

这样就完全不需要VBS了

错误级别

但是,如果您的 VBS 将包含难以或不可能在批处理文件中完成的代码,您可以让 .vbs 文件 return 有一个错误代码。 START /WAIT 命令会将代码传递给 %ERRORLEVEL% 环境变量:

returnvalue = MsgBox("Do. you want to retry?",4,"My Game")
if returnvalue = 7 then
    WScript.Quit 2
else
    WScript.Quit 1
end if

然后在批处理文件中使用以下内容:

START /WAIT "" "C:\Velocity DK\My Game\Invalid.vbs"
IF %ERRORLEVEL% EQU 1 GOTO RETRY
IF %ERRORLEVEL% EQU 2 GOTO FORCE_QUIT

进一步阅读