Batch: How can i skip it if there is no input? - batch-file

so in this code
:chat
cls
findstr /v "sdlkfjsdlkfs98dfu9sd8f6ysd954" \\Cap\FileServer\Recive\chatroom.chatfile
echo.
echo ----------------------------------------------------------
echo.
color 0b
goto chat1
:chat1
ping localhost -n 3 >nul
set /p text=Text:
echo %name% : %text% >>\\Cap\FileServer\Recive\chatroom.chatfile
goto chat
So i was wondering if i can make it not wait for the Text: and go on to refreshing the chat file if there is no input.

You cannot do this with Batch as once you have prompted the user, the execution will wait until input is received.
Perhaps as a workaround, you can "refresh" when empty input is received:
:chat
cls
findstr /v "sdlkfjsdlkfs98dfu9sd8f6ysd954" \\Cap\FileServer\Recive\chatroom.chatfile
echo.
echo ----------------------------------------------------------
echo.
color 0b
goto chat1
:chat1
REM Reset any existing text value.
set "text="
ping localhost -n 3 >nul
set /p text=Text:
REM Check for input.
IF NOT "%text%"=="" (
REM Input was given. Write it to the file.
echo %name% : %text% >>\\Cap\FileServer\Recive\chatroom.chatfile
)
goto chat
So in the above, if the user just presses Enter at the prompt, nothing will be written to the chatfile and the loop will start over.

Related

I want to have multiple echo and echo. below a pause and below a set /p variable= befor pressing any key in a batch file

I want the line of code below to work or something that will do the same and i want the echo and echo. to show befor i press any key
set /p variable=
echo.
echo test
echo.
echo test
::ect
and I want the line of code below to work or something that will do the same and i want the echo and echo. to show befor i press any key
pause
echo.
echo test
echo.
echo test
::ect
I tried this (I have not tried it with pause)
#echo off
for /f %%A in ('echo prompt $E^| cmd') do set "esc=%%A"
cls
echo *other text*%ESC%[2A
set /p variable=
echo %ESC%[1B
I was only able to have one echo or one echo. after set /p variable=, but I expected to be able to have multiple echo below set /p variable= and multiple echo. below set /p variable=
If i try to have more then one line of text then it takes the line of text above set /p variable= and moves it down below set /p variable= and combines it with the text i want below set /p variable=
So i want the set /p variable= and pause to pause after all the echo and echo.
I want it to look like for example this when i open the .bat file
Type the number of your choosing:
*other text*
Or this
Press any key to continue . . .
*other text*
sorry for posting the same thing so many times i promis that this is the last time
You are using ANSI escape codes. Just use them at the right place and with the proper parameters (numbers):
#echo off
for /f %%A in ('echo prompt $E^| cmd') do set "esc=%%A"
cls
echo/
echo/
echo *other text*
echo *additional text*
echo *some more text*
echo %ESC%[5A
set /p "variable=Enter var: "
echo %ESC%[2B
echo Thank you for your input.
Output on screen (where Hello is the manual input:
Enter var: Hello
*other text*
*additional text*
*some more text*
Thank you for your input.
You need enough space "above" set /p. It doesn't work at the very first line (after CLS) because you can't move the cursor to "line zero", so you need (at least) two (empty) lines (echo/).

trying to make an "OS" in batch

#echo off
:load
rem imitation of loading the os
color 70
ver
title boot
echo please wait...
ping localhost -n 3 >nul
cls
systeminfo
rem in here user types name he wants to be his account name
:login
title login
cls
date
cls
echo welcome to windows 71
echo before we bigen please type your name
set /P_name=here:
if %name%=admin goto admin
if not goto ms
rem ms=menu start
:ms
echo %time%
echo hello %name%
echo type HELP for list to commands
set /P_command=here:
if %command%=help goto help
if %command%=exit goto exit
if %command%=calendar goto cal
if not goto wc
rem wc=wrong command
:admin
echo hello %name% to the admin panel
echo type HELP for list to commands
set /P_command=here:
if %command%=help goto help
if %command%=exit goto exit
if %command%=calendar goto cal
So the problem is that it crashes after the :LOGIN part and I don't know what to do!
I'm trying to make an OS batch (something like MS-DOS), but it crashes after the "login" part.
I tried everything I could think of and it didn't work, also I want to make a save file so users can set a password for their "account".
As mentioned in above comments, you need to correctly use your variables, you can however use choice instead of set /p for your commands.
#echo off
:load
rem imitation of loading the os
color 70
ver
title boot
echo please wait...
timeout /t 3 >nul
cls
systeminfo
rem in here user types name he wants to be his account name
:login
title login
cls
date /t
timeout /t 2>nul
cls
echo welcome to windows 71
echo Before we begin please type your name
set /P "_name=here:"
if /i "_%name%"=="admin" goto admin
rem ms=menu start
:ms
echo %time%
echo hello %_name%
echo type HELP for list to commands
CHOICE /C HEC /M "Press H for Help, E to exit C for Calender."
goto opt%errorlevel%
:admin
echo hello %_name% to the admin panel
echo type HELP for list to commands
CHOICE /C HEC /M "Press H for Help, E to exit C for Calender."
goto opt%errorlevel%
:opt1
echo Help stuff goes here
goto :eof
:opt2
exit
:opt3
echo Calenders stuff goes here
Some things to note. You do not require to goto ms is the user is not admin as the statement for not being admin will not be met, we will automatically faal through into the ms label.
Notice where the problems were in your code. i.e if %name%=admin should be if "%_name%"=="admin" with double equal sign and the underscore in the name. It is also double quoted to ensure that we do a match without unwanted whitespace. Lastly /I option to catch ADMIN in any case.
See if /?, choice /? from command line for more help around these functions.
Okay this code is quite wrong.
I fixed it.
#echo off
:load
rem imitation of loading the os
color 70
title boot
echo please wait...
ping localhost -n 3 >nul
cls
rem in here user types name he wants to be his account name
:login
title login
cls
echo Welcome to Microsoft Windows 7!
echo Before we begin, please type your name.
set /p name=here:
if "%name%"=="admin" goto admin
if not "%name%"=="admin" goto ms
rem ms=menu start
:ms
echo %time%
echo Hello %name%
echo Type HELP for list to commands
set /p command=here:
if "%command%"=="help" goto help
if "%command%"=="exit" goto exit
if "%command%"=="calendar" goto cal
goto ms
rem wc=wrong command
:admin
CLS
echo hello %name% to the admin panel
echo type HELP for list to commands
set /P command=here:
if "%command%"=="help" goto help
if "%command%"=="exit" goto exit
if "%command%"=="calendar" goto cal
GOTO :ADMIN
Okay but you don't need to start systeminfo and all that.

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:

Set /P not capturing input in IF statement

Everything in this batch script works fine, but when I enter the IF statement, for some reason set /p id= doesn't actually capture anything. In fact shortly after it will echo:
You chose session %id%.
but that will return a blank, as though nothing was entered for ID.
Any advice would be greatly appreciated.
#echo off
echo Please be sure CMD is being run as an administrator.
echo.
:loop
set /p targetpc="Which PC would you like to query for users (Hostname or IP)?: "
echo.
echo Querying %targetpc%...
echo.
quser /server:%targetpc%
echo.
set /p choice="Would you like to log a user off of %targetpc%? [Y/N]: "
echo.
IF /I "%choice%" EQU "Y" (
echo Enter user's session ID:
set /p id=
echo.
echo You chose session %id%.
echo.
logoff %id% /server:%targetpc% /V
echo.
echo Done!
echo.
goto loop
)
IF /I "%choice%" EQU "N" (
goto loop
)
You are using the %id% value within the same block where it is set. This being the case, you need to use delayed expansion.
Add these lines to the top and bottom of your script, respectively:
SETLOCAL EnableDelayedExpansion
<Your script>
ENDLOCAL
Now use delayed expansion notation with your block (! around variables instead of %):
IF /I "%choice%" EQU "Y" (
echo Enter user's session ID:
set /p id=
echo.
echo You chose session !id!.
echo.
logoff !id! /server:%targetpc% /V
echo.
REM Note, replacing with a period here.
echo Done.
echo.
goto loop
)
There are tons of other questions on this site regarding delayed expansion, so a quick search within the batch-file tag should yield lots of additional info if you need it.

Class: ECHO is off from batbox

I wanted to make a little game using batbox. I wanted it to display which class you used but it didn't work. It showed "Class: ECHO is off." And I don't know what's wrong. Here's the code I used:
:charectarselect
cd gamedata
color 0f
cls
batbox /g 0 0
echo Select class:
echo.
echo %mage%
echo %worrior%
set /p classes=Class:
echo %classes%>pclass.gm
goto login
:login
color 0f
cd gamedata
set /p pclass=<pclass.gm
set /p user=<username.gm
set /p pass=<password.gm
if "%pclass%" == "1" echo Mage>pclass.gm
cls
title Login
echo ======================LOGIN======================
set /p login=Username :
if %login% == %user% goto password
goto login
:password
cls
echo ======================LOGIN======================
set /p passw=Password :
if %passw% == %pass% goto game
goto password
:game
cls
batbox /c 0xc9 /d "Health: %health%"
batbox /g 11 0 /c 0x9c /d "Strength: %strength%"
batbox /g 23 0 /c 0x9a /d "Class: %pclass%"
pause >nul
The problem is caused by you entering 1 for class. Specifically, it's caused by the line echo %classes%>pclass.gm
When you entered 1 in character selection, it expands that command to echo 1>pclass.gm. Unfortunately, 1> is used to redirect output from STDOUT to a file, and so batch is interpreting this as a command to send the output of echo to pclass.gm. By itself, echo displays whether echo is on or off.
You can get around this by adding parentheses to the echo line:
(echo %classes%)>pclass.gm

Resources