-1

I made a a database script to store giftcardcodes in. However, in my script the variables somehow don't contain anything. When I removed things that didnt matter in this question, I realised it worked.

WORKING:

::making variables
@echo off
title Please insert your code here:
cls
set /p code=Please insert your code here:
cls
title Who is the code from?
set /p client=Who is the code from?
cls
title For what did you get the money?
set /p what=For what did you get the money?
cls
title What's the amount of money saved on the code?
set /p amount=What's the amount of money saved on the code?
cls
::write to txt
@echo Code: %code%; Who: %client%; What: %what%; Money [in Euro]:%amount%>> %CD%\assets\database.txt
@echo off
color 0c
@echo Successfully written to database!
pause
exit

NOT WORKING:

:PASSWORDPROMPT
@ECHO off
setlocal EnableDelayedExpansion
@echo off
cls
@echo off
title Insert Password:
@echo off
set /p Passwort=Insert your Password here:
if %Passwort%==mypassword goto YES
if not %Passwort%==mypassword goto NO

:YES
if exist %CD%\assets\database.txt (
    goto EXISTS
) else (
    md %CD%\assets\
    copy /b NUL %CD%\assets\database.txt
    attrib +h %CD%\assets
)
:EXISTS
@echo off
title ENTER OR READ?
@echo off
cls
set /p readwrite=Do you want to ENTER a code to the database or READ the database? [ENTER/READ]
if /i %readwrite%==ENTER (
@echo off
title Please insert your code here:
cls
set /p code=Please insert your code here:
cls
title Who is the code from?
set /p client=Who is the code from?
cls
title For what did you get the money?
set /p what=For what did you get the money?
cls
title What's the amount of money saved on the code?
set /p amount=What's the amount of money saved on the code?
cls
@echo Code: %code%; Who: %client%; What: %what%; Money [in Euro]:%amount%>> %CD%\assets\database.txt
@echo off
color 0c
@echo Successfully written to database!
@echo off
color 0b
<nul set /p "=Press a key to Close..."
pause >nul
title Closing...
@echo off                           
for /L %%A in (3,-1,0) do ( 
cls
echo Closing in %%A Seconds.
ping localhost -n 2 >nul 
cls 
)

) else (
cls
@echo off
color
cls
@echo off
@echo ________________________________________________________
@echo YOUR CODES:
@echo ________________________________________________________
color
more %CD%\assets\database.txt
@echo ________________________________________________________
<nul set /p "=Press a key to Close..."
pause >nul
)

exit

:NO
cls
color 0c
@echo WRONG PASSWORD!
@echo off
timeout 1 /nobreak >null
@echo off                        
for /L %%A in (3,-1,0) do ( 
cls
echo Reload in %%A seconds...
ping localhost -n 2 >nul 
)
cls 
goto PASSWORDPROMPT

Does someone know what makes the difference and how I could fix the code?

aschipfl
  • 33,626
  • 12
  • 54
  • 99

1 Answers1

0

You are experiencing the delayed-expansion issue. It seems that you have already enabled delayed expansion.

However, to preserve compatibility, we have to change the variables to be updated at runtime like so:

%var% -> !var!

Please note that it only applies to some variable. The following examples are invalid: !!~A, !1


For your situation, you should modify your code as so:

@echo Code: !code!; Who: !client!; What: !what!; Money [in Euro]:!amount!>> %CD%\assets\database.txt