制作批处理文件只做一次

时间:2016-11-13 19:03:32

标签: batch-file cmd

我正在制作游戏,我想要一个只出现一次的个性化用户名选项。

以下是一个例子:

@echo off
:onetime
echo please enter a username
echo.
set /p newuser=%newuser%:
echo %newuser%> cfg.txt
goto menu


:menu
for /f "tokens=* delims=" %%x in (cfg.txt) do echo %%x
cls
...

我正在试图弄清楚如何制作:一次发生一次,以便将用户名发送到cfg.txt

任何人都知道怎么做?

2 个答案:

答案 0 :(得分:1)

我已经清理了你的代码并提供了一份完整的工作脚本,并注明了你将游戏代码放在哪里。

虽然此代码不需要延迟扩展,但我已将其放在脚本中,就像在原始脚本中一样。

请注意我选择的变量名称,因为它们在多个位置使用。

我试图用关于它正在做什么的信息来评论代码,如果需要,我会碰巧进一步解释。

如果可以的话,我想在你完成游戏时真正玩游戏。 :)

希望有所帮助。

<强> BPG.cmd

REM Script: BPG.cmd
REM Version: 1.0
REM Description:  Game to play about Monsters.
Rem Notes: Currently Implementing Menu System.

Rem Sets up Variables and checks if the script needs to be re-called.
@(
    SETLOCAL ENABLEDelayedExpansion
    echo off
    SET "eLvl=0"
    SET "ScriptFolder=%~dp0"
    SET "Log=%~dpn0.log"
    SET "ConfigFile=%~dpn0_cfg.txt"
    IF /I "%~1" NEQ "MAX" (
        ENDLOCAL
        ECHO.Game not started Maximized, Opening in a New Window by Running: Start "BPG 1 A Batch of Monsters" /MAX "%~dpnx0" MAX
        Start "BPG 1 A Batch of Monsters" /MAX "%~dpnx0" MAX
        EXIT /b %eLvl%
    )
    COLOR 2
)

REM Calls Main Function.
CALL :Main

REM Ends the script.
(
    ENDLOCAL
    EXIT /b %eLvl%
)

REM Main Function, most of your coding goes here, and this function calls sub functions.
:Main


    Rem Check if Config file exists, if it does not, then call the New User Function.
    IF NOT EXIST "%ConfigFile%" (
        CALL :NewUser
    )

    Rem Load Username from Config file.
    FOR /F "Tokens=*" %%A IN ('Type "%ConfigFile%"') DO (
        SET "_UserName=%%~A"
    )

    Rem Call Menu System
    CALL :Menu

    REM Based off the option chosen Either Start a new Game or Skip to the End.
    REM ECHO.CALL %_Menu_Choice%
    CALL %_Menu_Choice%

GOTO :EOF

:NewUser
    SETLOCAL

    REM Get the Username Input
    SET /P "_User=Please Enter a Username: "

    REM Output the Username to the config file, overwriting all the file contents:
    echo.%_User%>"%ConfigFile%"

    ENDLOCAL
GOTO :EOF

:Menu

    SETLOCAL

    REM Output the Config file contents:
    Type "%ConfigFile%"

    REM Clear the screen
    cls  
    echo  ______   _______   _______ 
    echo I  ___ \ I  ____ I I ____  \
    echo I I   I II I    II I I    \/
    echo I I__/ / I I____II I I      
    echo I  __ I  I  _____I I I ____ 
    echo I I  \ \ I I       I I \_  I
    echo I I___I II I       I I___I I
    echo I______/ I_/       I_______I A BATCH OF MONSTERS
    echo.
    echo.
    echo 1) Begin
    echo.
    echo 2) Exit
    echo.
    set /p "_Choice=%_UserName%, Enter a Number: "

    (
        REM End the local variable Space, and Set return variables based on the choice, or re-draw the menu.
        ENDLOCAL
        IF /I "%_Choice%" EQU "1" (
            REM ECHO.%_Choice% EQU 1
            SET "_Menu_Choice=:Begin_Game"
        ) ELSE (
            IF /I "%_Choice%" EQU "2" (
                REM ECHO.%_Choice% EQU 2
                SET "_Menu_Choice=GOTO :EOF"
            ) ELSE (
                ECHO.%_Choice% Not Valid!
                PAUSE
                GOTO :Menu
            )
        )
    )
goto :EOF

:Begin_Game

    REM All the remaining code for your game should probably go here.

GOTO :EOF

答案 1 :(得分:0)

尝试检查文件是否存在:

@echo off
:onetime
setlocal enableDelayedExpansion
if not exist "cfg.txt" (
 echo please enter a username
 echo.
 set /p newuser=%newuser%:
 echo !newuser!> cfg.txt
 goto menu
)


:menu
for /f "tokens=* delims=" %%x in (cfg.txt) do echo %%x
cls