I am trying to do a command line replica compeletely out of boredom and I have an issue. Here is my code:
#echo off
set /a secret=1
color B
title Subnet Access v1.0
echo ____________________________________________________________
echo / I
echo I Subnet Access v1.0 I
echo I____________________________________________________________/
echo.
echo.
timeout 3 > NUL
echo sys rev init
timeout 1 >NUL
echo loading keyframes
echo please wait
timeout 5 >NUL
echo.
echo.
echo Checking for alerts
timeout 6 >NUL
echo alert 0-21-77
timeout 1 >NUL
echo.
set /p pid=enter pid: || set pid=empty
IF "%pid%"=="1123321231" (
echo.
set /a "secret+=1"
cls
echo Secrets %secret%/10 found.
timeout 3 >NUL
cls
echo.
echo pid confirmed
timeout 1 >NUL
echo checking with server...
timeout 5 >NUL
cls
color C
echo COULD NOT IDENTIFY
timeout 1 >NUL
cls
echo.
timeout 1 >NUL
cls
echo COULD NOT IDENTIFY
timeout 1 >NUL
cls
echo.
timeout 1 >NUL
cls
echo COULD NOT IDENTIFY
timeout 1 >NUL
cls
echo.
timeout 1 >NUL
goto :unidentify
)
IF NOT "%pid%"=="1123321231" GOTO :unidentify
:unidentify
cls
echo.
echo unauthorized access
timeout 3 >NUL
echo level drop
timeout 1 >NUL
echo dropping...
timeout 3 >NUL
echo level now 0
timeout 4 >NUL
echo sys exit
timeout 1 >NUL
cls
cls
set su=0
color 7
title sys/rev/console-override
echo Subnet Console v0.1
echo Made by Murtaugh
echo.
:sub01 ::PROBLEM STARTS HERE
IF %su%==0 (
set /p commandMur=sys/dev/: #^> || set commandMur=emptycom
)
IF %su%==1 (
color B
set /p commandMur=sys/dev/: $^> || set commandMur=emptycom
)
IF %commandMur%==help (
::help segment
echo.
echo Help v0.1 Alpha by Murtaugh
echo.
echo Commands are listed below:
echo dir - Lists your directory's contents.
echo help - Shows this page.
echo connect - Connects you to an IP address.
echo.
goto :sub01
)
IF "%commandMur%"=="exit" (
goto :eof
)
IF "%commandMur%"=="cls" (
cls
goto :sub01
)
IF "%commandMur%"=="su" IF "%su%"=="0" (
cls
echo.
set /a "secret+=1"
echo Secrets %secret%/10 found.
set su=1
goto :sub01
)
IF "%commandMur%"=="su" IF "%su%"=="1" (
goto :sub01
)
IF "%commandMur%"=="emptycom" (
goto :sub01
)
IF NOT "%commandMur%"=="exit" IF NOT "%commandMur%"=="help" IF NOT "%commandMur%"=="cls" IF NOT "%commandMur%"=="su" (
echo "%commandMur%": Bad command or file name
echo.
goto :sub01
) ::PROBLEM ENDS HERE
But when I input something with spaces it says test==exit was unexpected at this time. and just close. Is there anyway I can fix this? Thanks.
EDIT: Tried "%prompt%"=="test" thing, doesn't work.
test==exit was unexpected ... is a typical syntax error with if (set doesn't give this type of error).
Most likely you have a if %command%==exit ...-command the next line. To avoid the syntax error, write if "%command%"=="exit" ...
im not really a great programmer, but try adding "QUOTATION TAGS" when you type text with spaces, beacuse batch sometimes get confused about those spaces
Related
I'm running a batch script that checks the status of a server, and if it's up, it goes ahead and reboots another server.
I put a few PAUSE commands in there because I want to see where it's falling over. It seems do the first ping, and then fail on the IF statement with:
The syntax of the command is incorrect.
Please can anyone tell me where I'm going wrong?
#echo off
setlocal enableextensions enabledelayedexpansion
set ipaddr=10.2.17.24
:loop
set state=down
for /f "tokens=5,7" %%a in ('ping -n 1 !ipaddr!') do (
if "x%%a"=="xReceived" if "x%%b"=="x1," set state=up
)
echo.Link is !state!
ping -n 6 10.2.17.24 >nul: 2>nul:
PAUSE
IF "%state%" == "down"
GOTO:loop
else
(
echo.DB server back up
PAUSE
echo.Continuing to web server reboot
PAUSE
set Webipaddr=10.2.17.36
PAUSE
:loop
set Webstate=down
for /f "tokens=5,7" %%a in ('ping -n 1 !Webipaddr!') do (
if "x%%a"=="xReceived" if "x%%b"=="x1," set Webstate=up
)
echo.Link is !Webstate!
ping -n 6 10.2.17.36 >nul: 2>nul:
PAUSE
IF "%Webstate%" == "down"
GOTO:loop
else
echo "Both servers back up."
endlocal
On an if statements, if you want to write commands with newlines, you need to use parenthesis, like this:
IF 1==1 (
FOO
) ELSE (
BAR
)
So We need to:
add parenthesis to first IF (before ELSE)
close the ELSE of the first IF (you seem to be missing a ')')
the last IF must be fixed to this form: IF 1==1 (FOO) ELSE (BAR)
The code, fixed:
#setlocal enableextensions enabledelayedexpansion
#echo off
set ipaddr=10.2.17.24
:loop
set state=down
for /f "tokens=5,7" %%a in ('ping -n 1 !ipaddr!') do (
if "x%%a"=="xReceived" if "x%%b"=="x1," set state=up
)
echo.Link is !state!
ping -n 6 10.2.17.24 >nul: 2>nul:
PAUSE
IF "%state%" == "down" (
GOTO:loop
) else (
echo.DB server back up
PAUSE
echo.Continuing to web server reboot
PAUSE
set Webipaddr=10.2.17.36
PAUSE
:loop
set Webstate=down
for /f "tokens=5,7" %%a in ('ping -n 1 !Webipaddr!') do (
if "x%%a"=="xReceived" if "x%%b"=="x1," set Webstate=up
)
echo.Link is !Webstate!
ping -n 6 10.2.17.36 >nul: 2>nul:
)
PAUSE
IF "%Webstate%" == "down" (GOTO:loop) else (echo "Both servers back up.")
endlocal
The game code is pretty simple, the code for it is below. Can't figure out why syntax for :WRONG section is well wrong. I've tried mirroring it exactly from the start section which works and is identical. Anyone offer any input?
color 4a
cls
:MAIN
#echo off
echo Welcome to XXXX's Trivia Game!!
pause & echo Ready to start?
set /p input=(Y/N)
if %input%==y goto SECTION1
goto BYE
:BYE
#echo Sorry to see you leave! Have a nice day!
pause
exit
:SECTION1
#echo FIRST QUESTION!!!!
pause
cls & echo At what temperature does rain turn to snow?
echo A. 112 degrees
echo B. -37 degrees
echo C. 32 degrees
echo D. 17,341 degrees
set /p input=Answer?
if %input%==C goto 2
goto WRONG
:SECTION2
:SECTION3
:SECTION4
:SECTION5
:SECTION6
:SECTION7
:SECTION8
:SECTION9
:SECTION10
:WRONG
cls & echo You were wrong, would you like to start again?
set /p input==(Y/N)
if %input%==y goto :SECTION1
goto BYE
pause
:HALFWAY
:WIN
Things I've tried:
Removing all sections between :SECTION1 and :WRONG
Renaming :WRONG and the call for it in :SECTION1
Removing :HALFWAY and :WIN
Copying from :MAIN (working correctly) to :WRONG (still no dice)
The following command has an extra = that causes a syntax error:
set /p input==(Y/N)
The prompt for set /p cannot begin with =. Remove the unwanted char as follows, and the command will work.
set /p input=(Y/N)
You are reusing the input variable, and set /p will preserve any pre-existing value if the user simply presses <Enter>. You should clear the value before prompting for input.
set "input="
set /p input=(Y/N)
There are lots of additional areas where your code could (should) be improved, but I will let you discover them at your own pace.
A basic yes/no choice command example for you:
#Choice /M "Ready to start?"
#Echo=%%ERRORLEVEL%% is %ERRORLEVEL%
#Timeout 5
Run it and see what happens when you enter different options.
Fixed your game :)
Try the below code...
Edit: Did some code improvements and cleanup, hope you'd like it...
#echo off
color 4a
cls
:MAIN
cls
echo.
echo --------------------------------------
echo Welcome to New-B-Admin's Trivia Game!!
echo --------------------------------------
echo.
set /p input="Ready to start playing ? Answer (Y/N): "
if /i "%input%"=="y" goto SECTION1
if /i "%input%"=="n" goto BYE
if not "%input%"=="Place_Holder_Value_Dont_Remove" goto MAIN
:SECTION1
cls
echo ------------------
echo Question Number 1
echo ------------------
echo.
echo At what temperature does rain turn to snow?
echo.
echo A. 112 degrees
echo B. -37 degrees
echo C. 32 degrees
echo D. 17,341 degrees
echo.
set /p input="Your answer?: "
if /i "%input%"=="a" goto WRONG
if /i "%input%"=="b" goto WRONG
if /i "%input%"=="c" (
call :CORRECT
goto SECTION2
)
if /i "%input%"=="d" goto WRONG
::the below if statment prevents the user from just hitting Enter Key or any non relevant letters or numbers.
if not "%input%"=="Place_Holder_Value_Dont_Remove" goto SECTION1 rem rename this goto statment to the same name as the label.
:SECTION2
cls
echo ------------------
echo Question Number 2
echo ------------------
echo.
echo What year was the two dollar bill last printed in the United States?
echo.
echo A. 1997
echo B. 2001
echo C. 2003
echo D. 2007
echo.
set /p input="Your answer?: "
if /i "%input%"=="a" goto WRONG
if /i "%input%"=="b" goto WRONG
if /i "%input%"=="c" (
call :CORRECT
goto SECTION3
)
if /i "%input%"=="d" goto WRONG
:: the below if statment prevents the user from just hitting Enter Key or any non relevant letters or numbers.
if not "%input%"=="Place_Holder_Value_Dont_Remove" goto SECTION2 rem rename this goto statment to the same name as the label.
:SECTION3
cls
echo ------------------
echo Question Number 3
echo ------------------
echo.
echo How many super bowls have the Denver Broncos won?
echo.
echo A. 5
echo B. 10
echo C. 7
echo D. 3
echo.
set /p input="Your answer?: "
if /i "%input%"=="a" goto WRONG
if /i "%input%"=="b" goto WRONG
if /i "%input%"=="d" (
call :CORRECT
goto SECTION4
)
if /i "%input%"=="c" goto WRONG
:: the below if statment prevents the user from just hitting Enter Key or any non relevant letters or numbers.
if not "%input%"=="Place_Holder_Value_Dont_Remove" goto SECTION3 rem rename this goto statment to the same name as the label.
:SECTION4
cls
echo ------------------
echo Question Number 4
echo ------------------
echo.
echo What was the name of the hourse from The Lone Ranger Movie that he saved from an enraged buffalo?
echo.
echo A. Silver
echo B. Yellow
echo C. White
echo D. Buck
echo.
set /p input="Your answer?: "
if /i "%input%"=="c" goto WRONG
if /i "%input%"=="b" goto WRONG
if /i "%input%"=="a" (
call :CORRECT
goto SECTION5
)
if /i "%input%"=="d" goto WRONG
:: the below if statment prevents the user from just hitting Enter Key or any non relevant letters or numbers.
if not "%input%"=="Place_Holder_Value_Dont_Remove" goto SECTION4 rem rename this goto statment to the same name as the label.
:SECTION5
:SECTION6
:SECTION7
:SECTION8
:SECTION9
:SECTION10
:END
cls
echo.
echo --------------------
echo No more questions!!!
echo --------------------
echo.
set /p input="Would you like to restart the game ? Answer (Y/N): "
if /i "%input%"=="y" goto SECTION1
if /i "%input%"=="n" goto BYE
if not "%input%"=="Place_Holder_Value_Dont_Remove" goto END
:WRONG
cls
echo.
set /p input="You were wrong, would you like to start again?(Y/N): "
if /i "%input%"=="y" goto SECTION1
if /i "%input%"=="n" goto BYE
if not "%input%"=="Place_Holder_Value_Dont_Remove" goto WRONG
:CORRECT
cls
set input=0
echo.
echo Correct!!! Great progress :)
echo Press enter to continue to next question...
pause >nul
goto :EOF
:HALFWAY
:WIN
:BYE
cls
echo.
echo ----------------------------------------
echo Sorry to see you leave! Have a nice day!
echo ----------------------------------------
pause >nul
exit
I recently played the game "Dictator" and wanted to recreate it in a batch script. I just began creating it when I ran into an issue. I have made programs which allow the user to save their progress and load it. I copied this code from a different program and put it in mine. Saving does seem to work but when i edit the save file (.cmd) and edit one of the saved variables, once I load that file nothing seems to happen. For example I saved one file called key.cmd and edited it to make the "pol" variable equivalent to 100. Once I loaded that file it did say loaded but the pol variable was still set to the default (10). I dont understand as I have made a folder called save and key.cmd file is inside it. I have used my save and load code multiple times in the past and have never had any issues. Please help! Here is my code:
:Setup
#echo off
title Dictator
color 0a
:Save Variables
set pol=10
set bui=10
set low=10
set cor=10
set peo=10
set cri=10
:Main
cls
echo 1 - Save
echo 2 - Load
echo 3 - Police - %pol%
echo 4 - Buissnes Men - %bui%
echo 5 - Lower Government - %low%
echo 6 - Corruption - %cor%
echo 7 - People - %peo%
echo 8 - Criminals - %cri%
choice /c 12345678 /n /m ">>> "
if %errorlevel% equ 1 goto Save
if %errorlevel% equ 2 goto load
:Save
cls
set /p pin="PIN: "
(
echo set pol=%pol%
echo set bui=%bui%
echo set low=%low%
echo set cor=%cor%
echo set peo=%peo%
echo set cri=%cri%
) >> saves\%pin%.cmd
echo SAVED
pause >nul
goto main
:Load
cls
set /p pin="PIN: "
if exist saves\%pin%.cmd (
call saves\%pin%.cmd
echo %pol%
echo LOADED
pause >nul
) else (
echo INCORRECT PIN
pause >nul
)
goto main
At :Load you need to allow spaces in the file name. Most users won't know of this weakness, but in order to allow spaces (and a few other 'special' characters) to be read in the user input without breaking it, you need to add setlocal enableextensions enabledelayedexpansion to the beginning of your batch file and make the set /p pin="PIN: " statement into set /p "pin=PIN: " and replace the %'s with !'s where it calls for %pin%.
In the end, your code should look like:
:Setup
setlocal enableextensions enabledelayedexpansion
#echo off
title Dictator
color 0a
:Save Variables
set pol=10
set bui=10
set low=10
set cor=10
set peo=10
set cri=10
:Main
cls
echo 1 - Save
echo 2 - Load
echo 3 - Police - %pol%
echo 4 - Buissnes Men - %bui%
echo 5 - Lower Government - %low%
echo 6 - Corruption - %cor%
echo 7 - People - %peo%
echo 8 - Criminals - %cri%
choice /c 12345678 /n /m ">>> "
if %errorlevel% equ 1 goto Save
if %errorlevel% equ 2 goto load
:Save
cls
set /p "pin=PIN: "
(
echo set pol=%pol%
echo set bui=%bui%
echo set low=%low%
echo set cor=%cor%
echo set peo=%peo%
echo set cri=%cri%
) >> saves\!pin!.cmd
echo SAVED
pause >nul
goto main
:Load
cls
set /p "pin=PIN: "
if exist saves\!pin!.cmd (
call saves\!pin!.cmd
echo %pol%
echo LOADED
pause >nul
) else (
echo INCORRECT PIN
pause >nul
)
goto main
Here is my code, I don't want the user to have to press enter to make a choice. I tried a couple of things but nothing I tried worked.
#echo off
:debut mon menu a moi
cls
echo 1.Calculatrice
echo 2.Task manager
echo 3.msconfig
echo 4.Notepad
echo 5.ipconfig
echo 6.quit
echo.
set /p /c choix=" Choisir votre application : "
if "%choix%"=="1" goto menu1
if "%choix%"=="2" goto menu2
if "%choix%"=="3" goto menu3
if "%choix%"=="4" goto menu4
if "%choix%"=="5" goto menu5
if "%choix%"=="6" goto menu6
I didn't include the menu since it's not relevant
#echo off
cls
echo 1.Calculatrice
echo 2.Task manager
echo 3.msconfig
echo 4.Notepad
echo 5.ipconfig
echo 6.quit
echo.
choice /C 123456 /M "Choisir votre application : "
goto menu%errorlevel%
:menu1
echo Calculatrice
. . .
:menu6
:menu0
echo Quit
The choice command returns an errorlevel value between 1 and 6 (or 0 if the user press Ctrl-C), so goto menu%errorlevel% command direclty transfer to the desired label without need of any additional checking. For further details, type: choice /?
#echo off
setlocal enableextensions disabledelayedexpansion
rem OPTION 1 - Use choice command (if available)
echo ---------------------------------------------------
rem Ask for the options
choice /n /c 1234560 /m "Choisir votre application : "
rem Each option sets a errorlevel in the same order they are included.
rem It is necessary to test in decreasing order
if errorlevel 7 (
echo selected 0
) else if errorlevel 6 (
echo selected 6
) else if errorlevel 5 (
echo selected 5
) else if errorlevel 4 (
echo selected 4
) else if errorlevel 3 (
echo selected 3
) else if errorlevel 2 (
echo selected 2
) else if errorlevel 1 (
echo selected 1
)
rem Alternatively, this sintax can be used
rem In this case the order in the tests is not relevant
if "%errorlevel%"=="1" echo selected 1
if "%errorlevel%"=="2" echo selected 2
if "%errorlevel%"=="3" echo selected 3
if "%errorlevel%"=="4" echo selected 4
if "%errorlevel%"=="5" echo selected 5
if "%errorlevel%"=="6" echo selected 6
if "%errorlevel%"=="7" echo selected 0
rem OPTION 2 - Use xcopy to retrieve the key press
echo ---------------------------------------------------
rem Show the prompt
<nul set /p ".=Choisir votre application : "
rem Retrieve the user input
set "choix=" & for /f "delims=" %%a in ('xcopy /l /w "%~f0" "%~f0" 2^>nul') do if not defined choix set "choix=%%a"
set "choix=%choix:~-1%"
rem Echo user input
echo %choix%
I have 2 batch files I want to merge from 2 screens to 1 screen.
currently there is 2 screens:
1 screen is the chat room
1 screen is the message input screen.
I want to merge these into 1 batch file on the screen so it requests users input on the chat room screen
is that possible?
My code I want to merge is here:
----------------------------
chat.bat
----------------------------
#echo off
:StartUp
set rr=2
mode con cols=40 lines=8
if exist tmp.bat del /f /s /q tmp.bat
set hd=%cd%
title Remote Chat
color 0f
cls
echo [Remote Chat]
echo.
echo Made by Jordan Begg
echo 31/12/2013
set t1=%time:~6,2%
set /a t2=%t1%+2
if %t2% geq 60 set /a t2=%t2% - 59
:ExpLoop
if %time:~6,2% geq %t2% goto Go
goto ExpLoop
:Go
set cs=ChatSettings.bat
if exist %cs% (
call %cs%
goto Main
) else (
goto SetUp
)
:SetUp
cls
mkdir "C:\chat"
echo [Setup]
echo.
set /p un=Username:
if not defined un goto SetUp
:SetUp2
cls
echo [Setup]
echo.
echo Use this:
echo C:\chat
if defined sd echo Current: %sd%
echo.
set /p sd=Server folder:
if not defined sd goto SetUp2
set sd=%sd%
if not exist %sd% (
set sd=
echo Folder doesn't exist!
pause >nul
goto SetUp2
)
if exist %cs% del /f /s /q %cs%
echo set un=%un%>>%cs%
echo set sd=%sd%>>%cs%
echo set rr=4>>%cs%
goto Main
:Main
cd %hd%
cls
echo [Remote Chat]
echo.
echo 1 - Join
echo 2 - Create
echo 4 - Exit
echo.
set /p c=
if %c%==1 goto Join
if %c%==2 goto create1
if %c%==4 goto Exit
goto main
:Exit
exit
:create1
cls
echo Please enter a valid asset ID (i.e as0123456)
set /p ass=Asset:
ping -n 1 %ass% > c:\ping.tmp
type c:\ping.tmp | FIND "Approximate round trip" >NUL
IF ERRORLEVEL 1 GOTO NOPING
goto Create
:noping
echo Asset cannot be reached
echo.
goto create1
:Create
cd %sd%
set rn=
cls
echo [Create]
echo.
echo created
echo.
set rn=%ass%
echo %rn%
pause
if not defined rn goto Create
set rf=%sd%\%rn%.crm
if exist %rf% goto Create
cls
echo [Create]
echo.
echo Preparing room, please wait.
set group=Admin
echo [%time:~0,5%] %un% (admin) connected.>>%rf%
title Chat [%rn%]
cd %hd%
echo set rn=%rn%>>tmp.bat
echo set rf=%rf%>>tmp.bat
echo set group=%group%>>tmp.bat
start ChatMessage.bat
start ChatDisconnect.bat
cd %sd%
mode con cols=40 lines=16
:Admin
cls
cd %hd% >nul
if not exist tmp.bat (
set msg=Room closed successfully.
goto End
)
cd %sd% >nul
if not exist %rf% (
set msg=Connection terminated.
goto End
)
type %rf%
ping localhost -n 4 >nul
goto Admin
set t=%time:~7,1%+%rr%
if %t% GEQ 10 set /a t=%t% - 10
:AdminL
if %time:~7,1%==%t% goto Admin
:End
if exist tmp.bat del /f /s /q tmp.bat
mode con cols=40 lines=8
echo [Chat]
echo.
echo %msg%
set msg=
taskkill /f /fi "WINDOWTITLE eq Message" >nul
taskkill /f /fi "WINDOWTITLE eq Disconnect" >nul
title Chat
set group=
set rn=
set t1=%time:~6,2%
set /a t2=%t1%+2
if %t2% geq 60 set /a t2=%t2% - 5
:EndLoop
if %time:~6,2% geq %t2% goto Main
goto EndLoop
:Join
mode con cols=40 lines=16
cd %sd%
set rn=
cls
echo [Join]
echo.
if not exist %sd%\*crm (
echo No rooms available.
pause >nul
mode con cols=40 lines=8
goto Main
)
dir /b /o:n %sd%\*.crm
echo.
echo Format (no .crm at end): Room
set /p rn=Room's name:
if not defined rn goto Join
set rf=%sd%\%rn%.crm
if not exist %rf% goto Join
cls
echo [Join]
echo.
echo Joining room, please wait.
set group=User
echo [%time:~0,5%] %un% connected.>>%rf%
title Chat [%rn%]
cd %hd%
echo set rn=%rn%>>tmp.bat
echo set rf=%rf%>>tmp.bat
echo set group=%group%>>tmp.bat
start ChatMessage.bat
start ChatDisconnect.bat
cd %sd%
:User
cls
cd %hd% >nul
if not exist tmp.bat (
set msg=Disconnected successfully.
goto End
)
cd %sd% >nul
if not exist %rf% (
set msg=Connection terminated.
goto End
)
type %rf%
ping localhost -n 4 >nul
goto User
.
-----------------------
chatmessage.bat
------------------------
#echo off
if not exist tmp.bat (
exit
) else (
call tmp.bat
call ChatSettings.bat
)
title Message
mode con cols=30 lines=2
cd %sd%
:func
cls
set /p msg=Message:
if %msg:~0,3%==/me echo [%time:~0,5%] %un% %msg:~4%>>%rf%&& goto func
if not defined msg goto func
echo [%time:~0,5%] %un%: %msg%>>%rf%
goto func