0

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

4
  • 1
    Without the content of penize.txt there is little we can do to help. Commented Sep 5, 2016 at 16:32
  • Content of penize.txt is : First line 500 Second line 200 Commented Sep 5, 2016 at 16:47
  • does that include :? počítaje v to : Commented Sep 5, 2016 at 16:50
  • What do you mean by that Commented Sep 5, 2016 at 16:53

2 Answers 2

4

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="
  )
)
Sign up to request clarification or add additional context in comments.

1 Comment

clever solution. I liked !
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.