I am trying to make a countdown timer but the timer doesn't count up or count down. Please Help ! it just shows +1.
:Plus
echo-------------------------------------------------------------------------
echo Please enter starting number...
set /p time=%time%
echo Please enter ending number...
set /p etime=%etime%
echo-------------------------------------------------------------------------
echo.%time%
set time=%time%
:loop1
set /p time=%time%+1
if %time%==%etime% goto timesup1
echo.%time%
ping localhost -n 2 > nul
goto loop1
:timesup1
echo.%etime%
echo Time is Up!
goto Opt
(The Minus Option just shows the number typed then '-1')
:Minus
echo ------------------------------------------------------------------------
echo Please enter starting number...
set /p time=%time%
echo Please enter ending number...
set /p etime=%etime%
echo-------------------------------------------------------------------------
echo %time%
set time=%time%
:loop2
set /p time=%time%-1
if %time%==%etime% goto timesup2
echo %time%
ping localhost -n 2 > nul
goto loop2
:timesup2
echo %etime%
echo Time is Up!
goto Opt
your problem is here:
set /p time=%time%+1
you need /a, not /p to do calculations (see set /?)
Also you shouldn't set %time%, because it's a system variable used by windows. (use another name, like sTime(analogue to eTime)
Related
When i execute my program and put in something like 1280000000*20 it gives me -169803776 when it should be 25600000000, when i then try 25600000000/20 it gives me -1698037766 which is different. This shows there is something wrong with my calculator because the laws of maths say when a*b=c, b/c=a which isnt true in this situation. The calculations are also incorrect.
I've tried putting the same code into python (different language) and it works fine so i think its something to do with batch itself.
echo off
cls
:start
cls
echo Welcome to the calculator
echo.
echo Choose one of the of the following
echo.
echo 1. Multiplication
echo 2. Division
echo.
set /p choose=Choose a number:
IF %choose%==1 (
goto multiply
) ELSE IF %choose%==2 (
goto divide
) ELSE (
goto error
)
:error
cls
echo THAT ISNT AN OPTION!!
echo.
pause
goto start
:multiply
cls
echo Pick 2 numbers to multiply
echo.
set /p a=Number 1:
set /p b=Number 2:
set /a c=%a%*%b%
cls
echo %a% x %b% = %c%
pause
goto start
:divide
cls
echo Pick 2 numbers to divide
echo.
set /p a=Number 1:
set /p b=Number 2:
set /a c=%a%/%b%
cls
echo %a% / %b% = %c%
pause
goto start
I expect the output of 1280000000*20=25600000000, but the actual output is -169803776. This number changes every time to a negative for some reason.
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:
I am currently coding (in batch) a mini tycoon game. However,
I'm having some difficulties when executing this command:
:cantbuy
echo Insufficient Funds!
pause
goto shop
:buy1
set /p money<"m2df.dll"
set /p cps<"m3df.dll"
if %money% GTR 100 goto canbuy1
goto cantbuy
:canbuy1
set /a "newmoney=%money%-100"
set /a "newcps=%cps%+2"
set cps=%newcps%
set money=%newmoney%
echo %money%>m2df.dll
echo %cps%>m3df.dll
echo Bought!
pause
goto shop
The program seems to exit straight after I input "1"
Any ideas how to fix this?
Update:
Found It Out. I decided to put a pause after everything to see what was wrong, and the syntax was bad and it went straight to another command crashing the script. here's the fixed script:
:cantbuy
echo Insufficient Funds!
pause
goto shop
:buy1
set /p money=<"m2df.dll"
set /p cps=<"m3df.dll"
if %money% GEQ 100 goto canbuy1
goto cantbuy
:canbuy1
set /a "newmoney=%money%-100"
set money=%newmoney%
set /a "newcps=%cps%+2"
set cps=%newcps%
echo %cps% >m3df.dll
echo %money% >m2df.dll
echo Bought!
pause
goto shop
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.
I want to tell a user to input just how many minutes he wants before shutting down the computer and I don't know how can I avoid any letter in the user input. I just want to allow a number otherwise go to finish
#ECHO OFF
pushd %systemroot%
echo *{Enter the number of minutes required before Turn off the computer then press (enter)}
set/p "x="
if %x% EQU %x% set/a z= 60*%x%
echo %z%
shutdown -s -t %z%
echo finish
Here is another method:
#ECHO OFF
pushd "%systemroot%"
:loop
"set x="
set /p "x=Enter the number of minutes required before Turn off the computer then press (enter) "
rem this allows the user to press enter to quit without doing anything
if not defined x goto :EOF
rem check for numerals only - else loop to start again
set "num="
for /f "delims=0123456789" %%a in ("%x%") do set "num=%%a"
if defined num goto :loop
set /a z=60*%x%
echo %z%
shutdown -s -t %z%
echo finish
#ECHO OFF
SETLOCAL
pushd %systemroot%
echo *{Enter the number of minutes required before Turn off the computer then press (enter)}
set/p "x="
SET "notnumber=9%x%"
FOR /l %%z IN (0,1,9) DO CALL SET "notnumber=%%notnumber:%%z=%%"
IF DEFINED notnumber ECHO entry was NOT a number&GOTO :EOF
set/a z= 60*%x% 2>NUL
IF ERRORLEVEL 1 ECHO invalid number entered&GOTO :EOF
echo %z%
ECHO shutdown -s -t %z%
echo finish
GOTO :eof
Note that batch regards a number that starts 0 as OCTAL, so entering 09 is invalid (because it's not octal) and entering 013 is valid, but will give a timeout of 11 minutes because 13 octal is 11 decimal.
Note also that the shutdown command is simply echoed to avoid shutting down during testing.