I'm trying to echo a variable into an autoexec.cfg file but I'm having some trouble with echoing a string and a variable to a file simultaneously.
I've tried putting quotes around the echo statement like
echo "sv_maxplayers %answer%">%location%
and, while it echos the string correctly, it includes the quotations marks.
("sv_maxplayers 4")
My script is currently
#ECHO OFF
set location=".\Risk of Rain 2_Data\Config\autoexec.cfg"
set /p answer="Please enter a number 1-16:"
if %answer% GTR 0 (
if %answer% LSS 17 (
goto set
)
)
echo Invalid argument.
goto exit
:set
echo sv_maxplayers %answer%>%location%
echo Done!
goto exit
:exit
pause
exit
Running that returns
Please enter a number 1-16:4
sv_maxplayers
Done!
Press any key to continue . . .
in the console and clears autoexec.cfg.
It should output as
sv_maxplayers 4
Any idea what I'm doing wrong?
Found the solution! I was missing some spaces.
echo sv_maxplayers %answer%>%location%
needs to be
echo sv_maxplayers %answer% > %location%
Related
I am writing a basic batch script and I need to acquire user input.
The script appears to crash when I call the user input vars in an echo command.
(This is probably something very basic but I can't see the error...)
#echo off
echo ********************************
echo SHUTDOWN/RESTART
echo ********************************
echo.
set /p answer="Restart (R) or Shutdown(S)? "
set /p time="How soon ? "
echo you have selected %answer% in %time% seconds.
if "%answer%"=="" goto error
if "%time%"=="" goto error
if "%answer%"=="R" (
goto restart
) else (
goto shutdown
)
echo.
echo.
:restart
FOR /L %%AA IN (1,1,%1) DO (
echo Restarting in %%AA ...
)
The FOR command requires a single alphabetic character for its controlled variable. %AAA is invalid.
I try to make a batch file make another batch file with parentheses like this
set brackleft ="("
set brackright =")"
#(
echo #echo off
echo if exist exp.txt %brackleft%
echo goto a
echo %brackright% else %brackleft%
echo echo Bye
echo pause
echo %brackleft%
echo pause
echo :a
echo echo Hi
echo pause
) > Test.bat
But when I right click and edit the new batch file this comes up
#echo off
if exist exp.txt
goto a
else
echo Bye
pause
ECHO is off.
pause
:a
echo Hi
pause
Note:
rem ↓ this space is harmful creates %brackleft % variable instead of %brackleft%
set brackleft ="("
set brackright =")"
rem ↑ this space is harmful as well: creates %brackright % variable
rem ↑
Let's remove that harmful spaces:
set brackleft="("
set brackright=")"
Then, echo %brackright% else %brackleft% would result to ")" else "(".
You need next syntax with escaped both left and right parentheses as ^( and ^); Moreover, ^ caret should be escaped as well (see position of double quotes):
set "brackleft=^("
set "brackright=^)"
I can't add a parentheses
set brackleft ="("
set brackright =")"
You have created a variable with the name "brackleft " and "brackright " (note the trailing spaces).
Any extra spaces around either the variable name or the string, will not be ignored, SET is not forgiving of extra spaces like many other scripting languages.
Source set
Notes:
Remove the spaces before the =.
You also need to remove the "s (otherwise they will be echoed)
You also need to escape the ( and ) in order for the echo to work later.
^^ is used to make a single ^ escape character.
You should use ^^^ to escape the ( or ) inside an existing code block.
Your logic seems to have a mistake. echo %brackleft% should probably be echo %brackright%, otherwise your parentheses don't match up.
So the code becomes:
set brackleft=^^^(
set brackright=^^^)
Corrected batch file:
set brackleft=^^^(
set brackright=^^^)
#(
echo #echo off
echo if exist exp.txt %brackleft%
echo goto a
echo %brackright% else %brackleft%
echo echo Bye
echo pause
echo %brackright%
echo pause
echo :a
echo echo Hi
echo pause
) > Test.bat
Output:
F:\test>type Test.bat
#echo off
if exist exp.txt (
goto a
) else (
echo Bye
pause
)
pause
:a
echo Hi
pause
Further Reading
An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
set - Display, set, or remove CMD environment variables. Changes made with SET will remain only for the duration of the current CMD session.
syntax - Escape Characters, Delimiters and Quotes.
I am new to batch coding and I am having an issue where whenever I select an option, it goes to 1 (which is :MEMBERS) even if I enter 2 or 3. I am unsure why this is happening.
#echo off
cls
color A
:MENU
cls
echo ===============================
echo Mavast Client v0.01
echo ===============================
echo Enter a number from the list and hit enter
echo to run that program.
echo 1. Members
echo 2. Support us
echo 3. Exit
set /p var= Run:
if %var%=="1" goto MEMBERS
if %var%=="2" goto SUPPORT
if %var%=="3" goto EXIT
:MEMBERS
cls
echo Owner/Rifler = C0MpL3T3
echo Admin/Stratcaller = TDGR (The Dutch Grim Reaper)
echo Awper = NONE
echo Lurker = NONE
echo Entry Fragger = NONE
echo press any key to continue
timeout -1 > NUL
goto MENU
:SUPPORT
cls
echo Support us by donating BitCoins or skins
echo to donate skins, just trade them to TDGR or C0MpL3T3.
echo to donate bitcoins, send any amount you want to this address:
echo 1DSRHe7L842MZjwTWYPzgGzbsZ8zU97w9g
echo thank you!
echo press any key to continue
timeout -1 > NUL
goto MENU
:EXIT
cls
exit
try with
if "%var%"=="1" goto MEMBERS
if "%var%"=="2" goto SUPPORT
if "%var%"=="3" goto EXIT
Quotes are also compared in IF conditions.
I'm making a blackjack gambling simuator using .bat files. At one point i get the error "goto was unexpected at this time". Anyone know a solution to this problem? I have included the section of code that is causing the problem.
Echo if %%DealerHand%% GTR 21 set DealerStatus=Bust
Echo if %%DealerHand%%==21 set DealerStatus=Blackjack!
Echo if %%DealerHand%% LSS 21 set DealerStatus=%%DealerHand%%
Echo if %%handP4%% GTR 21 set P4status=Bust
Echo if %%handP4%%==21 set P4status=Blackjack!
Echo if %%handP4%% LSS 21 set P4status=%%handP4%%
Echo cls
Echo Echo Table Number:1
Echo Echo Dealer:David
Echo Echo Money:$%%money%%
Echo Echo Money on table:$%%bet%%
Echo Echo Players:
Echo Echo.
Echo Echo Dealer:Hand:%%DealerStatus%%
Echo Echo.
Echo Echo %acctname%:Hand:%%P4status%%
Echo Echo.
Echo Echo Enter to continue
Echo Pause^>nul
Echo if %%DealerStatus%% == Bust goto winCheck1
Echo if %%DealerStatus%% == Blackjack! goto DealerWins1
Echo if %%DealerStatus%% == %%DealerHand%% goto winCheck1
Echo :winCheck1
Echo if %%P4hand%% == Bust goto DealerWins1
Echo if %%P4hand%% == Blackjack! goto P4wins1
Echo if %%P4hand%% GTR %%DealerHand%% goto P4win1
Echo if %%P4hand%% LSS %%DealerHand%% goto DealerWins1
Echo if %%P4hand%% == %%DealerHand%% goto DealerWins1
*P.S. I'm using this .bat file to generate a new .bat file contianing code. Hence the Echo before every line.
Any help is good help!
Thanks.
Your test for the string "Blackjack!" is going to fail due to the exclamation mark. Try wrapping the test with quotes:
Echo if "%%DealerStatus%%" == "Blackjack!" goto DealerWins1
and
Echo if "%%P4hand%%" == "Blackjack!" goto P4wins1
I suspect however, that there are a few other issues in your code that may be tripping you up. The %P4hand% variable is a number earlier in your code snippet. Are you intending to test %P4status% instead? You may also want to check your use of tag names for consistency as there's P4wins1 as well as P4win1 in use.
The first advice I'll give you is to put ECHO ON at the top of your file (or just remove ECHO OFF if it's there), run it again, and figure out which line has the error. Batch wants to tell you what's wrong, but everybody always suppresses echo even while debugging.
That said, even without knowing which line has the error, my psychic debugging skills tells me that somehow %DealerHand% is the empty string, which causes a syntax error in one of these two lines:
if %%DealerStatus%% == %%DealerHand%% goto winCheck1
if %%P4hand%% == %%DealerHand%% goto DealerWins1
Running that if statement with %DealerHand% undefined or the empty string will give you the error you see. You don't show where DealerHand is set, but if it should not be empty, you'll need to make sure it has a value when you enter this code.
If it is possible that DealerHand has an empty value, then you can guard against the syntax error by wrapping the variable in some characters, so that both sides of the IF always have non-empty strings.
if {%%DealerStatus%%} == {%%DealerHand%%} goto winCheck1
Which evaluates to if {} == {} goto winCheck1 when both variables are null.
I have this code:
loop
echo -- File Created With LuaIDE. Do NOT Remove This Line > LuaCode.lua
set /p Lua=Lua:
%Lua% >> LuaCode.lua
if %Lua% == "LuaIDE.Exit()" ( echo Thanks for using LuaIDE!
timeout /t 3 /nobreak >nul
exit )
goto loop
and what i want it to do is get User-Input and put it into a file and if the user input is "LuaIDE.Exit()" I would like it to say a message for 3 seconds and exit but it just says:
'whatever you type in' is not recognized as an internal or external command,
operable program or batch file.
Why does it do this?
You have a few bugs. Try this:
echo -- File Created With LuaIDE. Do NOT Remove This Line > LuaCode.lua
:loop
set /p Lua=Lua:
echo %Lua% >> LuaCode.lua
if %Lua%==LuaIDE.Exit() goto exit
goto loop
:exit
echo Thanks for using LuaIDE!
ping 1.1.1.1 -n 3 -w 1 >nul
exit
Try this:
#echo off
:loop
echo -- File Created With LuaIDE. Do NOT Remove This Line > LuaCode.lua
set /p Lua=Lua:
echo %Lua%>>LuaCode.lua
if /i "%Lua%" == "LuaIDE.Exit()" echo Thanks for using LuaIDE & timeout /t 3 /nobreak >nul & exit
goto loop
Let me know how it works.
stop complicating a simple task....
the code is as simple as
#echo off
: start
echo.
set /p input= type your text here:
echo %input%>>myfile.extension
timeout /t 5 >null
cls
goto start
replace myfile with name of your file you want to send text.
replace extension with any that you want txt,log , etc
also you can specify path of your file e,g
echo %input%>>path/directory