I made a chat program and I want it to auto refresh so i can get rid of the refresh option I have on it. the only problem is I would need to have everything from :A all to way to Echo -----.... to repeat itself while everything below does not. And I do not want it to be split into 2 files.
:A
cls
Echo Server: %S%
Echo Press Q to Quit Or Press R to refresh chat log.
echo ---------------------------------------------------------
type %S%.txt
echo ---------------------------------------------------------
set /p M=Enter Your Message:
if "%M%"=="Q" goto B
if "%M%"=="q" goto B
if "%M%"=="R" goto A
if "%M%"=="r" goto A
echo %U%: %M% >>%S%.txt
goto A
Note
if /I Do a case Insensitive string comparison;
:eof This predefined label will exit the current routine;
call :label Jump to a label (subroutine) in the current batch script.
Next code snippet could help.
:A
call :repeatHeader
set /p "M=Enter Your Message: "
if /I "%M%"=="Q" goto :B
if /I "%M%"=="R" (
call :refreshLog
goto :A
)
echo %U%: %M% >>%S%.txt
goto :A
:repeatHeader
Echo Server: %S%
Echo Press Q to Quit Or Press R to refresh chat log.
goto :eof
:refreshLog
cls
echo ---------------------------------------------------------
type %S%.txt
echo ---------------------------------------------------------
goto :eof
:B
Related
For my AP Physics 2 project we have to create something that can calculate the equations for resistors and capacitors, I'm having problems with having the variables actually add up to a complete number rather than just being 2+4+4 as an output.
Below is my code so far:
#echo off
echo Do you want Resitors, or Capacitors.
echo Type R or C when prompted.
set /p input0=Enter:
if %input0% equ R goto R1
if %input0% equ C goto C1
goto startup
echo Press any key to continue.
pause>nul
:R1
cls
echo Do you want Series or Parallel?
echo Type S or P when prompted.
set /p input1=Enter:
if %input1% equ S goto S1
if %input1% equ P goto P1
goto Startup
echo Press any key to continue.
pause>nul
:S1
cls
echo What are your 3 values?
set /p input2=Enter:
echo.
set /p input3=Enter:
echo.
set /p input4=Enter:
echo.
set /p "Answer=%input2%+%input3%+%input4%"
echo %Answer%
goto Startup
echo Press any key to continue.
pause>nul
:P1
cls
echo What are your 3 values?
set /p input5=Enter:
echo.
set /p input6=Enter:
echo.
set /p input7=Enter:
echo.
set /a "Answer=(1/((1/%input6%)+(1/%input5%)+(1/%input7%)))"
echo %Answer%
goto Startup
echo Press any key to continue.
pause>nul
When you get to the user input part, no matter what I type (desk, fire, door) it always goes to fireplace. Is there something wrong with my if syntax?
#echo off
color C
title RG Text Game
echo -------------------------------------------------
echo Welcome to the Game!
echo.
echo -------------------------------------------------
echo.
pause
echo.
echo Blah bah blah story story
echo What do you want to do?
echo Choices: fire/desk/door
set /p choice=
if %choice%=="fire" GOTO fireplace
if %choice%=="desk" GOTO desk
if %choice%=="door" GOTO door
:fireplace
echo.
echo You come to the fireplace.
echo.
pause
:desk
echo.
echo You go to the desk.
echo.
:door
echo.
echo You go to the door.
echo.
Doublequote %choice% or it will not be equal: desk is not equal as "desk".
And exit your Label block with a goto:eof or exit/b.
Use the /i switch with IF so you can also use DESK or DesK
if /i "%choice%"=="fire" GOTO fireplace
if /i "%choice%"=="desk" GOTO desk
if /i "%choice%"=="door" GOTO door
goto:error
:fireplace
echo.
echo You come to the fireplace.
echo.
pause
exit/b
:desk
echo.
echo You go to the desk.
echo.
exit/b
:door
echo.
echo You go to the door.
echo.
exit/b
You need to protect the entry points of each of your target blocks, otherwise when it's done executing the target block, it will "fall into" the next block.
Before any label line (e.g., :fireplace), you'll need a GOTO to make sure that the program flow doesn't "fall into" the next routine:
#echo off
color C
title RG Text Game
echo -------------------------------------------------
echo Welcome to the Game!
echo.
echo -------------------------------------------------
echo.
pause
echo.
echo Blah bah blah story story
echo What do you want to do?
echo Choices: fire/desk/door
set /p choice=
if /I "%choice%" EQU "fire" GOTO fireplace
if /I "%choice%" EQU "desk" GOTO desk
if /I "%choice%" EQU "door" GOTO door
GOTO END
:fireplace
echo.
echo You come to the fireplace.
echo.
pause
GOTO END
:desk
echo.
echo You go to the desk.
echo.
GOTO END
:door
echo.
echo You go to the door.
echo.
:END
Note also the changes to the IF statements. These allow you to handle the case of typing FIRE or Fire instead of just fire.
In addition to the tips from Jeff and SachaDee,
if you name all your labels equal to the choice you could use a loop
For %%A in (fire desk door) Do If /i "%choice%" equ "%%A" Goto %choice%
Or with a llimited number of choices you could use choice.exe and work with a single letter answer (no enter needed then) and evaluate the returned errorlevel.
If you are familiar with the 1983 movie "War Games" you should remember the part where the computer ask him "Shall We Play a Game". I have been trying to recreate that and this is what I have so far. The issue is after I enter the password the Command Prompt window closes out. Can someone help me find my mistake?
#echo off
title Shall We Play a Game?
color 0b
set /a tries=3
set password=Joshua
:top
echo %tries% Tries Remaining
set /p pass=Password:
if %pass%==%password% (
goto correct
)
set /a tries=%tries -1
if %tries%==0 (
goto penalty
)
cls
goto top
:penalty
echo CONNECTION TERMINATED
pause
exit
:correct
goto greeting
:greeting
echo Shall we play a game?
echo y/n
set input=
if %input%=y goto y
if %input%=n goto n
:y
echo How about
echo Chess
echo Tic-Tac-Toe
echo Snake
echo Global Thermonuclear War
if %opt%==Chess goto Chess
if %opt%==Tic-Tac-Toe goto TicTacToe
if %opt%==Snake goto Snake
if %opt%==Global Thermonuclear War goto Global Thermonuclear War
:n
echo Thats too bad! Maybe we should play some other day!
pause
exit
:chess
:tictactoe
echo Are you sure?
echo y/n
set response=
if %response%==y goto tictactoe1
if %response%==n goto tictactoe2
:tictactoe1
echo Go Back?
echo y/n
set feedback=
if %feedback%==y goto greeting
if %feedback%==n goto tictactoe2
:tictactoe2
echo testing
goto tictactoe2
This should be what you're trying to do:
#echo off
title Shall We Play a Game?
color 0b
set /a "tries=3"
set "password=Joshua"
:top
echo %tries% Tries Remaining
set /p "pass=Password: "
if "%pass%"=="%password%" (
goto correct
)
set /a "tries=%tries% -1"
if %tries%==0 (
goto penalty
)
cls
goto top
:penalty
echo CONNECTION TERMINATED
pause
exit
:correct
goto greeting
:greeting
echo Shall we play a game?
echo y/n
set /p "input="
if "%input%"=="y" goto y
if "%input%"=="n" goto n
:y
echo How about
echo Chess
echo Tic-Tac-Toe
echo Snake
echo Global Thermonuclear War
set /P "opt="
if "%opt%"=="Chess" goto Chess
if "%opt%"=="Tic-Tac-Toe" goto TicTacToe
if "%opt%"=="Snake" goto Snake
if "%opt%"=="Global Thermonuclear War" goto GlobalThermonuclearWar
:n
echo Thats too bad! Maybe we should play some other day!
pause
exit
:chess
:tictactoe
echo Are you sure?
echo y/n
set /p response=
if %response%==y goto tictactoe1
if %response%==n goto tictactoe2
:tictactoe1
echo Go Back?
echo y/n
set /p feedback=
if %feedback%==y goto greeting
if %feedback%==n goto tictactoe2
:tictactoe2
echo testing
goto tictactoe2
You forgot /P in a set a couple of times, this is used to get user input. In your set /a tries=%tries -1 you also forgot to put a second % around tries, this should be set /a tries=%tries% -1. Furthermore, you should put "" double quotes around your variables if you're comparing them, this prevents the script from braking if a variable doesn't exist or is empty. You also shouldn't have spaces in your labels, and you should put quotes around your set, like this: set "variable=value", this prevents trailing spaces from getting in your variables
i have a question . It is possible to make a batch menu to accept multiple commands at the same time?
Example : ( my code )
#ECHO OFF
set tries=6
:top
cls
set /a tries=%tries% -1
if %tries%==0 (
goto penalty
)
Echo You have %tries% attempts left.
Echo Please enter your password to proceed
set /p password=
if %password%==Parola ta (
echo Welcome Your Name
ping localhost -n 5 >nul
cls
Echo CONNECTED!
C:
CD\
CLS
:MENU
CLS
ECHO ============= MENU NAME =============
ECHO -------------------------------------
ECHO 1. System Information TXT file
ECHO 2. Selection 2
ECHO 3. Selection 3
ECHO 4. Selection 4
ECHO 5. Selection 5
ECHO 6. Selection 6
ECHO 7. Selection 7
ECHO -------------------------------------
ECHO 8. Selection 8
ECHO -------------------------------------
ECHO 9. Selection 9
ECHO -------------------------------------
ECHO ==========PRESS 'Q' TO QUIT==========
ECHO.
SET INPUT=
SET /P INPUT=Please select a number:
IF /I '%INPUT%'=='1' GOTO Selection1
IF /I '%INPUT%'=='2' GOTO Selection2
IF /I '%INPUT%'=='3' GOTO Selection3
IF /I '%INPUT%'=='4' GOTO Selection4
IF /I '%INPUT%'=='5' GOTO Selection5
IF /I '%INPUT%'=='6' GOTO Selection6
IF /I '%INPUT%'=='7' GOTO Selection7
IF /I '%INPUT%'=='8' GOTO Selection8
IF /I '%INPUT%'=='9' GOTO Selection9
IF /I '%INPUT%'=='Q' GOTO Quit
CLS
ECHO ============INVALID INPUT============
ECHO -------------------------------------
ECHO Please select a number from the Main
echo Menu [1-9] or select 'Q' to quit.
ECHO -------------------------------------
ECHO ======PRESS ANY KEY TO CONTINUE======
PAUSE > NUL
GOTO MENU
:Selection1
systeminfo.exe>systeminfo.txt
ECHO ============INVALID INPUT============
ECHO -------------------------------------
ECHO Please select a number from the Main
echo Menu [1-9] or select 'Q' to quit.
ECHO -------------------------------------
ECHO ======PRESS ANY KEY TO CONTINUE======
PAUSE > NUL
GOTO MENU
:Selection2
Call cleanup.bat
:Selection3
and in here too...
:Selection4
and so on
:Selection5
and so on
:Selection6
and so on
:Selection7
and so on
:Selection8
and so on
:Selection9
and so on
:Quit
CLS
ECHO ==============THANKYOU===============
ECHO -------------------------------------
ECHO ======PRESS ANY KEY TO CONTINUE======
PAUSE>NUL
EXIT
pause
cls
) else (
goto top
)
goto top
How to make the program accept more than 1 command?
For example 1,3,5 to execute in the same time?
or another question, how its possible to undo .exe back to .bat?
there such a program?
Suggestion: make call from goto and concatenate the commands with &:
IF /I '%INPUT%'=='1' GOTO:Selection1
IF /I '%INPUT%'=='2' CALL:Selection2&CALL:Selection2&CALL:Selection4
IF /I '%INPUT%'=='3' CALL:Selection3&CALL:Selection3
IF /I '%INPUT%'=='4' CALL:Selection4&CALL:Selection4
IF /I '%INPUT%'=='5' CALL:Selection5
IF /I '%INPUT%'=='6' CALL:Selection6
IF /I '%INPUT%'=='7' CALL:Selection7
IF /I '%INPUT%'=='8' CALL:Selection8
IF /I '%INPUT%'=='9' CALL:Selection9
IF /I '%INPUT%'=='Q' GOTO:Quit
To get this work you must alse add goto:eof (eof=end of file) after the jump labels, eg.:
:Selection2
Call cleanup.bat
goto:eof
:Selection3
and in here too...
goto:eof
:Selection4
and so on
goto:eof
:Selection5
and so on
goto:eof
...
..
.
goto:eof returns control to the main program.
Is this what you were looking for?
File Menu.cmd
#echo off
setlocal
set quit=false
set /p InputChoices=Enter Choice(s) (A,B,C)
echo %InputChoices%
call :executeChoices %InputChoices%
endlocal
goto :eof
:executeChoices
if [%1]==[] goto :eof
call :Step%1
shift
goto :executeChoices
goto :eof
:StepA
echo Step A
goto :eof
:StepB
echo Step B
goto :eof
:StepC
echo Step C
goto :eof
Works like this:
c:\>Menu.cmd
Enter Choice(s) (A,B,C) b c a
b c a
Step B
Step C
Step A
You can use the start "" command keyword and each command will run in it's own window, but initiated by your menu batch file.
I'm designing a chat for my school LAN network, it types your messages into a .dll file so as to disguise the chat log.
The problem is that all of a sudden, whenever I started typing messages which have a space in them, the batch file crashes. For example if I enter the message as "h h" the batch will crash with the error:
h==exit was unexpected at this time
Heres the script:
#echo off
CLS
COLOR f2
SET user=%username%
SET message=
IF %user%==Josh SET cuser=Miltzi & GOTO :admin
IF %user%==miltzj SET cuser=Miltzi & GOTO :admin
IF %user%==steinj SET cuser=Jarod & GOTO :first
IF %user%==steinda SET cuser=Daniel & GOTO :first
IF %user%==rubine SET cuser=Evan & GOTO :first
IF %user%==sklairn SET cuser=Nathan & GOTO :first
IF %user%==portnoyc SET cuser=Craig & GOTO :first
IF %user%==polakowa SET cuser=Polly & GOTO :first
IF %user%==selbya SET cuser=Alex & GOTO :first
IF %user%==vanderwesthuizenl SET cuser=Lance & GOTO :first
msg * This is a test message! :D
REM the above line is incase a teacher runs the chat remotely from their computer
exit
:CHAT
TITLE Grade 11 IT chat :)
IF NOT EXIST C:\users\Josh\desktop\1.dll echo Chat cleared. >> C:\users\Josh\desktop\1.dll
CLS
type C:\users\Josh\desktop\1.dll
SET /P message=Type message and press enter (Type help to view chat options):
IF ERRORLEVEL 1 GOTO :CHAT
IF %message%==exit GOTO :exit
IF %message%==Exit GOTO :exit
IF %message%==EXIT GOTO :exit
IF %message%=="exit" GOTO :exit
IF %message%==help GOTO :help
IF %message%==Help GOTO :help
IF %message%=="help" GOTO :help
echo %user%: %message% >> C:\users\Josh\desktop\1.dll
GOTO :CHAT
:exit
CLS
echo %user% left the chat. >> C:\users\Josh\desktop\1.dll
exit
:help
CLS
echo Welcome to the help section
echo To exit the chat, please type exit as a message into the chat rather than closing the cmd
box manually.
echo To refresh the chats messages, just press enter without writing any text.
echo Please press enter to go back to the chat :)
pause
GOTO :CHAT
:ACHAT
TITLE Grade 11 IT chat :)
IF NOT EXIST C:\users\Josh\desktop\1.dll echo Chat cleared. >> C:\users\Josh\desktop\1.dll
CLS
type C:\users\Josh\desktop\1.dll
SET /P message=Type message and press enter (Type help to view chat options):
IF ERRORLEVEL 1 GOTO :ACHAT
IF %message%==exit GOTO :exit
IF %message%==Exit GOTO :exit
IF %message%==EXIT GOTO :exit
IF %message%=="exit" GOTO :exit
IF %message%==help GOTO :help
IF %message%==Help GOTO :help
IF %message%=="help" GOTO :help
IF %message%==cls GOTO :CLS
echo %user%: %message% >> C:\users\Josh\desktop\1.dll
GOTO :CHAT
:exit
CLS
echo %user% left the chat. >> C:\users\Josh\desktop\1.dll
exit
:help
CLS
echo Welcome to the help section
echo To exit the chat, please type exit as a message into the chat rather than closing the cmd
box manually.
echo To refresh the chats messages, just press enter without writing any text.
echo Please press enter to go back to the chat :)
pause
GOTO :CHAT
:CLS
del C:\users\Josh\desktop\1.dll
GOTO :ACHAT
:admin
echo %user% joined the chat. >> C:\users\Josh\desktop\1.dll
GOTO :ACHAT
:first
echo %user% joined the chat. >> C:\users\Josh\desktop\1.dll
GOTO :CHAT
exit
Any help would be appreciated!
I guess the error occurs in this line:
IF %message%==exit GOTO :exit
If %message% includes a space, e.g. h h, this line expands into
IF h h==exit GOTO :exit
which is not a valid syntax for the IF operator.
To avoid the error, enclose the operands in quotes:
IF "%message%"=="exit" GOTO :exit
But be aware that this variant is also not reliable and will throw a syntax error if %message% includes the quote character ".
And by the way, you can perform case-insensitive string comparison using the /i switch:
IF /i "%message%" EQU "exit" GOTO :exit