This question already has answers here:
Example of delayed expansion in batch file
(5 answers)
Closed 4 years ago.
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?
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
Related
im currently making something along the lines of a sentence generator, it uses text files which have a list of words in them, for example celebrity.txt has a list of celebrities and this script both shuffles the text file into newcelebrity.txt and takes the first one from that list so its different every time, and ive run into a problem, i want it to be one line and that you can call a variable in the sentance youre typing, not break it down as it is right now, is there a way to have it "this $celebrity is really great" as of now, it works like this: https://gyazo.com/9ae8583ed5457709bd1c1dc9cc0cc106 and outputs as this https://gyazo.com/1a5a90f1fbf80faa73d71791a8c1c761, i dont mind the quotation marks at all, its just the way you input it.
Is there any way to make it work like i want it to or is this a limitation of batch files?
set /p message=Unity:
set /p input=The variable:
set /p after=Unity:
:gen
setlocal
cd ..
cd rcs
echo doing background work, please wait a few seconds :)
for /f "delims=" %%a in (%input%.txt) do call set "$$%%random%%=%%a"
(for /f "tokens=1,* delims==" %%a in ('set $$') do echo(%%b)>new%input%.txt
endlocal
cls
set "File=C:\Users\%USERNAME%\Desktop\gg\rcs\new%input%.txt"
set /a count=0
echo background work done :)
timeout /t 1 >nul
SETLOCAL enabledelayedexpansion
for /F "tokens=* delims=" %%a in ('Type "%File%"') do (
Set /a count+=1
Set "output[!count!]=%%a"
)
For /L %%i in (1,1,%Count%) Do (
Call :Action "!output[%%i]!"
pause
)
Exit
::*******************************************************
:Action
cls
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo %message% %1 %after%
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo if you want to go back and change your inputs type back
echo if you want to continue generating, type gen
echo.
set /p instruction=
goto %instruction%
::*******************************************************
To get rid of the quotation marks, just use %~1 instead of %1.
I'm assuming, you want to input something like 'this $celeb is great' instead of the first three set /p lines (as your output is already in one line).
I used some fixed variables to keep it short - of course you would fetch them from the user and the text file instead, as you already did.
#echo off
setlocal EnableDelayedExpansion
set "count=3"
set "output[3]=John"
set "sentence=this $celeb is great."
REM above would be your 'set /p' instead
call :action "!output[%count%]!"
goto :of
:action
echo !sentence:$celeb=%~1!
(just to be clear: $celeb is not a variable - it's just a string, that we replace later, so from user's perspective, it behaves like a variable)
I created my first batch file ever a couple of weeks ago to use livestreamer more comfortably.
Basically I can either type in the name of Twitch streamer or I can start something from another plattform.
What I am trying to do is is save my input as an option for the next time.
Let's say I went to the twitch stream of "shroud". When the stream ends I would like to put shroud as an option in the beginning of the script:
:start
title twitchings
color 0a
echo Select [#] or enter Stream
echo.
echo 1. non twitch
echo 2. shroud
set /p select=?
I assume it would be easiest to store that list in a txt file and then load it upon running the batch file, but my basically non-existant knowledge in coding hinders me from creating it.
Does anyone know a quick help, I would also love to know if there is a website where I can learn this stuff without googling for hours :)
This is my updated script for those who are searching for the same issue.
#ECHO off
:start
title twitchings
color 0a
echo Select [#] or enter Stream
echo.
echo 1. non twitch
setlocal enableextensions enabledelayedexpansion
set "file=C:\Users\[...]\savedstreams.txt"
set /A i=1
for /F "usebackq delims=" %%a in ("%file%") do (
set /a i += 1
echo !i! . %%a
)
set /p select=?
set /a varCheck=%select%
if %varCheck% == %select% (goto :isnumber) else (goto :isstream)
exit /B
:isnumber
set "lineNr=%select%"
set /a lineNr-=1+1
for /f "usebackq delims=" %%a in (`more +%lineNr% savedstreams.txt`) DO (
set "stream=%%a"
goto :leave
)
:leave
set "stream=%stream:*:=%"
echo stream: %stream%
goto qual
:isstream
set "stream=%select%"
echo %stream% >>savedstreams.txt
goto qual
:TpyeInSource
echo enter URL
echo.
set /p select2=?
livestreamer %select2%
GOTO end
:qual
livestreamer http://twitch.tv/%stream% 1080p60 || livestreamer http://twitch.tv/%stream% best
GOTO end
:end
#CHOICE /C:rqn /M "[R]etry, [Q]uit or [N]ew"
IF ERRORLEVEL 3 GOTO start
IF ERRORLEVEL 2 GOTO quit
IF ERRORLEVEL 1 GOTO qual
GOTO quit
:quit
echo "bye."
#PAUSE
Thank you guys!
You can use >> to put the output of a command in text file. For example:
echo hi everyone >>textfile.txt
will add "hi everyone" to the last line of the text file.
You can also use > to add it to the first line instead.
(This is my first post here, so bear with me)
Can you show the last user-input in a batch file? I'm gonna try to keep it simple here.
#echo off
:menu
echo Type 1 to proceed.
set /p example=
if "%example%" == "1" GOTO :proceed
GOTO :error
:proceed
pause
:error
cls
echo You wrote (last user input), that's not correct.
timeout 30
GOTO :menu
I know that I could replace the (last user input) with %example%, but then I'd have to make custom error messages for every category, and there are about 50 of them. It'd be easier with a last input command.
By the way, I've taught myself everything that I know about batch, so my example probably has major issues right now, but it works somehow.
You could centralize all user input into a function (user_input)
:menu1
echo Type 1 to proceed.
call :userInput example
if "%example%" == "1" GOTO :proceed
GOTO :error
:menu2
echo Type 42 to proceed.
call :userInput answer
if "%answer%" == "42" GOTO :proceed
GOTO :error
:userInput
set /p LAST_INPUT=
set "%1=%LAST_INPUT%"
exit /b
:proceed
pause
:error
cls
echo You wrote "%LAST_INPUT%", that's not correct.
timeout 30
GOTO :menu
I don't know how to do it without temp file. TO get the things written int the console you need the doskey /history (this will skip the running of the script itself):
#echo off
setlocal enableDelayedExpansion
set "last="
set "but_last="
doskey /history > log.txt
for /f "tokens=* delims=" %%# in (log.txt) do (
set "but_last=!last!"
set "last=%%#"
)
echo "%but_last%"
del /s /q log.txt >nul 2>nul
I'm new to batch and am practicing by making a calculator. I've made it but its not working. So I want to check with more experienced people to see whats wrong.
#echo off
set MATH=Equation:
set /a result=%MATH%
echo %result
You need to do set /p instead of just set (set /p means user input)
if you were to do that it would look like this
#echo off
title CALCULATOR
:start
cls
set /p MATH=Equation:
set /a result=%MATH%
echo %result%
pause
goto :start
This question already has an answer here:
Variables are not behaving as expected
(1 answer)
Closed last year.
i have written this batch file code but it has this error i appreciate u if answer me .
the error is that when i choose 2 it gone to ask me to enter desire site but when i write the address it has an error says ECHO IS OFF
HELP ME WHAT SHOULD I DO...
#echo off
title trace
:main
echo 1)TRACE GOOGLE
echo 2)TRACE YOUR SITE
set /p choice= Enter your choice:
echo %choice%
if %choice%==1 (
tracert www.google.com
goto main
pause >nul
)
if %choice%==2 (
set /p s=Enter your desired site:
echo %s%
pause >nul
)
pause >nul
It's not an error message, it's what happens when you run echo without arguments. In other words, you see this because %s% winds up being empty.
To fix your problem use enabledelayedexpansion as in a block % will expand to the value prior to the block:
#echo off
setlocal enabledelayedexpansion
title trace
:main
echo 1)TRACE GOOGLE
echo 2)TRACE YOUR SITE
set /p choice= Enter your choice:
echo %choice%
if %choice%==1 (
tracert www.google.com
goto main
pause >nul
)
if %choice%==2 (
set /p s=Enter your desired site:
echo !s!
pause >nul
)
pause >nul
Which should work
When you have #echo off at the begening, and you echo a empty var, would get this message. Try to check your echoed vars, ensure them not empty.