2009/05/27

Create a Batch File Chat Room


It is possible to build a full featured chat room using only the Window's batch programming language. It may not be pretty, but it gets the job done.

The idea is simple. Everyone writes to the same file. Collect input from the user and write it to that file. Then read that file to the screen to see what anyone else has said. Here is my code:

@echo off
rem: Matthew Merchant ScriptingMadness.blogspot.com
rem ###### TITLE SCREEN ############
title Batch Chat Room
color f0
echo.
echo Welcome to Office Chat Room.
echo.
echo Commands:
echo type CLS to clear the chat log.
echo type EXIT to quit the chat room.
echo just hit enter to update screen.
echo.
set /p username=What is your name?
echo %username% has joined. >> log.dat
cls
goto CHATLOOP
REM ############################




REM ########## CHAT LOOP ##########
set lastmessage=""

:CHATLOOP
cls
type log.dat
echo.
echo.
set /p message=Say:

rem set message checker value
set thismessage="%message%"

rem check for exit
if %thismessage% == "EXIT" echo %username% has left the chat room. >> log.dat
if %thismessage% == "EXIT" exit
rem -----------------

rem check for clear
if %thismessage% == "CLS" echo %username% has cleared the chat log. > log.dat
if %thismessage% == "CLS" set message=Empty
if %thismessage% == "CLS" set lastmessage="Empty"
if %thismessage% == "CLS" goto CHATLOOP
rem ------------------

rem check for same message as last time
if %thismessage% == %lastmessage% goto CHATLOOP

rem if all checks out, go ahead and post this message
echo %username%: %message% >> log.dat

rem save value for next test
set lastmessage="%message%"

goto CHATLOOP
REM ################################



Pretty basic stuff. The hard part was implementing my two features. The first feature was CLS. This command clears the chat log by writing to it with the > symbol. This is helpful when the chat log gets too big because it slows the program down. The second feature is the EXIT command. This gracefully shuts the batch chat program down after sending a exit message to the other users.


Wish List:

I would have liked this program to update in real time. This would be possible if two batch files were used. One could be used for input, and another could be used for displaying the chat log. I can't figure out a way to do it with one batch file.

It might also be neat to create an HTA chat room that works in a similar way. This would present the user with a nicer interface, and it could also be updated in real time.

4 comments:

thejaboy said...

maybe adding a refresh command
somthing like "if %thismessage% == refresh goto chatroom" or somthing like this

lefex27 said...

how can i make others join that rom?

Davide Franchioni said...

come faccio a fate chattare due pc devo mettere due file bat 1 in ogni computer

Temp said...

Join PSCA Co PXT Development team, we are working on a project similar to that only it is internet friendly, maybe using TELNET. http://psca.co/

PXT is PythonXT Running code, PythonXT was a project made by me along time ago. click Contact PSCA Co. and email me. we could work on this project. yes i do know batch very well.



ScriptingMadness.Blogspot.com © 2009 || Privacy Policy