Why does the batch file crash? - 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

Related

How do I create an update function in Batch?

I would like to create an update function that works just after the user is prompted to type into the "ChatBox" so that other users on my school network can type and all other users can see it without having to restart the program or typing space to reload the .txt file
here is the code I have written so far;
:enter
cls
type cblog.txt
echo.
set /p text=
echo %text% >> cblog.txt
goto enter
I like this topic, so I wrote a fully working prototype:
#echo off
setlocal
if "%~1" equ "" echo You must give your username as parameter & goto :EOF
set "user=%~1"
set "now=%time%"
echo %now%: User %user% entered the chat room>> msgQueue.txt
call :chatRoom 3< msgQueue.txt
goto :EOF
:chatRoom
rem Omit previous messages
:omitMsg
set /P "msg=" <&3
if "%msg:~0,12%" neq "%now%:" goto omitMsg
echo %msg%
echo/
echo Press S to send a message or eXit to end
echo/
:msgLoop
rem Check for new messages received
:showMsg
set "msg="
set /P "msg=" <&3
if not defined msg goto send
echo %msg%
goto :showMsg
rem Check to send a new message
:send
ver > NUL
choice /C SNX /N /T 3 /D N > NUL
if errorlevel 3 goto end
if errorlevel 2 goto msgLoop
rem Send a message
echo -----------------------------------
set /P "msg=Message: "
echo -----------------------------------
echo %user%: %msg% >> msgQueue.txt
goto msgLoop
:end
echo %time%: User %user% leaved the chat room>> msgQueue.txt
The response time may be adjusted in /T 3 parameter of choice command: shorter times makes the chat more responsive, but it consume more CPU time.
Below is an image that show a test with four users in the Chat Room:

Batch Script: Using GOTO on IF statement

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.

Batch file closes after a goto command

Here's the code
#echo off
color 0a
:user_login
set /p username=What is your username?
if %username% == admin (
cls
goto admin_request_password
)
echo Incorrect username please try again.
cls
goto user_login
:admin_request_password
set /p Password=What is your Encryption Key?:
if %Password% == ecca1924889236 (
pause
cls
goto admin_menu
pause
)
echo Incorrect password
pause
cls
goto user_login
:admin_menu
echo Type acct_info for account info.
echo Type site_key for website file manager.
echo type ip_info for your ip info.
echo Type close to close the Net-Sec portal
goto admin_menu_process
:admin_menu_process
set /a choice=Waiting for Request:
if %choice% == acct_info (
cls
goto acct_info ) echo Incorrect term cls goto admin_menu
if %choice% == site_key (
goto site_key )echo Incorrect term
cls
goto admin_menu
if %choice% == ip_info (
goto ip_info ) echo Incorrect term
cls
goto admin_menu
if %choice% == close (
goto close ) echo Incorrect term
cls
goto admin_menu
:ip_info
ipconfig
pause
cls
goto admin_menu
:acct_info
echo Your username is: admin
echo your password is: ecca1924889236
pause
cls
goto admin_menu
:site_info
The username is: net-sec
The password is: netsec127
pause
cls
goto admin_menu
:close
cls
Suggestions:
Do not set a variable to %username% as that is already an existing system variable.
Enclose both strings with double quotes in a string comparison.
Use the choice command when you have a known set of choices.
Try not to include unnecessary labels.
Here is a rewrite of your code to hopefully help you in your scripting exploits:
#Echo Off
Color 0A
:getName
ClS
Set/P "uname=What is your username? "
If /I Not "%uname%"=="admin" (Echo Incorrect username please try again.
>Nul Timeout 3
GoTo getName)
:getPass
ClS
Set/P "password=What is your encryption key? "
If Not "%password%"=="ecca1924889236" (Echo Incorrect password please try again.
>Nul Timeout 3
GoTo getPass)
:menu
ClS
Echo [1] Account information.
Echo [2] Website File Manager.
Echo [3] IP information.
Echo [4] Close the Net-Sec portal.
Choice /C 1234 /M "Enter your request"
If ErrorLevel 4 GoTo close
If ErrorLevel 3 GoTo ip_info
If ErrorLevel 2 GoTo site_info
If ErrorLevel 1 GoTo acct_info
GoTo :EOF
:acct_info
Echo Your username is: admin
Echo your password is: ecca1924889236
Timeout 3
GoTo menu
:site_info
Echo The username is: net-sec
Echo The password is: netsec127
Timeout 3
GoTo menu
:ip_info
IPConfig
Timeout -1
GoTo menu
:close
ClS
Echo Closing...
Timeout 3

Repeat part of program while other part stays the same

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

Batch opening wrong file type

So here's my ENTIRE code:
#echo off
cls
color fc
:Start
cls
echo Welcome to -{GAMELOADER}-
set/p u=Username:
if %u%==username goto Correct1
if not %u%==username goto Incorrect
:Incorrect
cls
echo You Have Entered Incorrect Pass And/Or Username!
set/p t=Try Again? (Y/N)
if %t%==Y goto Start
if %t%==y goto Start
if %t%==N goto Quit
if %t%==n goto Quit
:Correct1
set/p p=Password:
if %p%==password goto Open
if not %p%==password goto Incorrect
:Open
cls
echo Games:
echo ------------------------
echo [1]Kerbal Space Program
echo ------------------------
set/p g=Choice:
if %g%== 1 goto KSPEnd
:KSPEnd
start "" "C:\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program\KSP.exe"
cls
goto Quit
:Quit
cls
echo Goodbye
Timeout 1
But the code opens the .exe AND a .txt file with exactly the same name. I can't rename the files. So basically i'm asking how to open a specific file type.
Thanks
Instead of starting C:\....\KSP.exe, first go to the right directory, then start KSP:
cd "C:\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program"
KSP.exe
Ok I've got two things for you. Firstly I'll give you you desired solution.
Treat it like an operatable program
rem To start Kerbal Space Program:
set Path=C:\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program;%Path%
start KSP
Thats it. Really.
Secondly:
Use the Choice command
you keep on using set /p where choice would be much better.
Just to be convenient I redid you code with everything I would do. Have fun!
Code :
#echo off
cls
color fc
title -{GAMELOADER}-
:Start
echo Welcome to -{GAMELOADER}-
set/p u=Username:
if %u%==username goto Correct1
if not %u%==username goto Incorrect
set Er=Userid
goto :Crash
:Incorrect
cls
echo You Have Entered Incorrect Pass And/Or Username!
choice /c yn /m "Try Again?"
if %errorlevel%==1 goto Start
if %errorlevel%==2 goto Quit
set Er=Loop-End_of_stream
goto :Crash
:Correct1
set/p p=Password:
if %p%==password goto Open
if not %p%==password goto Incorrect
set Er=Passid
goto :Crash
:Open
cls
echo Games:
echo ------------------------
echo [1]Kerbal Space Program
echo ------------------------
echo.
Choice /c 1 /m "Game: "
if %errorlevel%==1 goto KSPEnd
set Er=Gameid
goto :Crash
:KSPEnd
set Path=C:\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program;%Path%
start KSP
goto Quit
set Er=End_of_file___UNKNOWN
goto :Crash
:Quit
cls
echo Goodbye
Timeout 1
Exit
:Crash
Rem Always useful :)
Echo Program has crashed Error: %Er%
Pause
Exit
Hope that helped. Mona

Resources