我正在尝试为我的朋友做“游戏”,但是当我尝试从保存文件加载时,它会完成一半的工作
代码
set penize=0
set penizesekunda=0
for /f "EOL=: tokens=* Delims=0" %%a in (penize.txt) do (
set penize=%%a
)
for /f "EOL=: tokens=* Delims=1" %%b in (penize.txt) do (
set penizesekunda=%%b
)
echo Penize %penize% penize za sekundu %penizesekunda%
抱歉用我的语言(penize = money,penizesekunda =每秒钱数)
输入为200 200但应该是500 200。
如果您想要完整代码:
@echo off
title Hra (Imui)
color 6a
echo Ahoj %computername% cas %time% datum %date%
echo.
echo.
echo Vitej ve hre
echo.
echo.
echo.
echo Tvym cilem bude ovladnout cely VESMIR
echo.
timeout>nul /t 3 /nobreak
echo Nacitani!
set penize=0
set penizesekunda=0
for /f "EOL=: tokens=* Delims=0" %%a in (penize.txt) do (
set penize=%%a
)
for /f "EOL=: tokens=* Delims=1" %%b in (penize.txt) do (
set penizesekunda=%%b
)
if errorlevel 1 do (
echo Pozor nebyla nalezena zadna ulozena hra nebo doslo k chybe startovani! (hry se nachazeji v souboru penize.txt)
timeout>nul /t 3 /nobreak
cls
goto Start
)
:Start
cls
set /p "Password=Zadej heslo > "
if %Password%== Admin goto RMV
if NOT %Password%== Imui goto FAIL
if %Password%== Imui goto HesloSpravne
goto Start
:Fail
cls
echo Spatne heslo!
echo Zadej heslo prosim (zbyvaji 2 pokusy)
set /p "Password=Zadej heslo > "
if %Password%== Admin goto RMVCzech
if NOT %Password%== Imui goto FailTwo
if %Password%== Imui goto HesloSpravne
goto Fail
:FailDva
cls
echo Spatne heslo!
color ac
echo Pozor! Mas uz jenom jeden pokus!
echo Prosim zadej heslo!
set /p "Password=Zadej heslo > "
if %Password%== Admin goto RMV
if NOT %Password%== Imui goto RMV
if %Password%== Imui goto HesloSpravne
goto FailTwo
:RMV
echo Smazavam ti tvoji hru
@ECHO ON
del "Hra.exe" /P /Q /F
echo Smazavani dokonceno!
Pause>nul
exit
:HesloSpravne
cls
echo Heslo bylo spravne
goto HlavniMenu
:HlavniMenu
echo Vitej
echo %penizesekunda% %penize%
Pause>nul
penize.txt的内容:
500
200
答案 0 :(得分:4)
另一个(更有效的)选项是使用SET / P来读取每一行:
<penize.txt (
set "penize="
set /p "penize="
set "penizesekunda="
set /p "penizesekunda="
)
清除每个变量的简单SET语句只是在文件中的行恰好为空或缺少的情况下,在这种情况下,SET / P将保留任何预先存在的变量值。
使用FOR循环可以缩短代码,特别是如果你还有更多行要阅读:
<penize.txt ( for %%V in (penize penizesekunda) do (
set "%%V="
set /p "%%V="
))
请注意,您可以在单独的行中列出变量,这有助于提高可读性:
<penize.txt (
for %%V in (
penize
penizesekunda
) do (
set "%%V="
set /p "%%V="
)
)
答案 1 :(得分:0)
以下是一些示例代码:
REM Get first line of a file
set /p penize=<penize.txt
REM Get second line of a file
for /f "skip=1" %%a in (penize.txt) do set penizesekunda=%%a