RPochyly
RPochyly

Reputation: 11

Batch : Multiple for /f commands

I'm trying to do "game" for my friend but when i try to load from save file it will do half of it's job

Code

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%

sorry it in my language (penize = money, penizesekunda = money per second)

And input is 200 200 but it should be 500 200.

If you want full code :

@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

Content of penize.txt :

500
200

Upvotes: 0

Views: 114

Answers (2)

dbenham
dbenham

Reputation: 130819

Another (more efficient) option is to use SET /P to read each line:

<penize.txt (
  set "penize="
  set /p "penize="
  set "penizesekunda="
  set /p "penizesekunda="
)

The simple SET statements that clear each variable are there just in case the line happens to be empty or missing in the file, in which case SET /P would preserve any pre-existing variable value.

The code can be shortened by using a FOR loop, especially if you have many more lines to read:

<penize.txt ( for %%V in (penize penizesekunda) do (
  set "%%V="
  set /p "%%V="
))

Note that you can list the variables on separate lines, which can help readability:

<penize.txt ( 
  for %%V in (
    penize
    penizesekunda
  ) do (
    set "%%V="
    set /p "%%V="
  )
)

Upvotes: 4

Compo
Compo

Reputation: 38589

Here is some example code for you:

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

Upvotes: 0

Related Questions