Missing operand error in batch statement with variable - batch-file

I'm getting a syntax error on the timeout command supposedly because of a "missing operand". But I don't see any operand missing.
rem description for the user to read
echo Insert time in minutes:
rem setting default (1 hour)
set /a timeto=3600
rem asking user for input (integer)
set /p timeto=
rem converting minutes to seconds
set /a timeto=%timeto%*60
rem command based on the inputted value
timeout /t %timeto% /nobreak

Invalid syntax. Commenting commands inline is not possible in cmd CLI nor a batch scripts:
==>set /a timeto=3600 ::setting default time (1 hour)
Missing operator.
==>set /a timeto=3600
3600
==>
Note that %timeTo%*60 exceeds timeout /T valid range -1 to 99999 for default 3600*60.
If your code snippet appears enclosed in () parentheses, then use Delayed Expansion.
#ECHO ON >NUL
#SETLOCAL EnableExtensions EnableDelayedExpansion
if 1==1 (
rem description for the user to read
echo Insert time in minutes:
rem setting default time (1 hour = 60 minues)
set /a timeTo=60
rem asking user for input (integer)
set /p timeTo=
rem converting minutes to seconds - erroneous
set /a timeTo=%timeTo%*60
rem converting minutes to seconds - right approach
set /a timeTo=!timeTo!*60
rem command based on the inputted value
echo timeout /t !timeTo! /nobreak
)
Output: note that set /a timeTo=%timeTo%*60 line causes the Missing operand error as results to erroneous set /a timeTo=*60 in the parse time.
==>D:\bat\SO\32410773.bat
==>if 1 == 1 (
rem description for the user to read
echo Insert time in minutes:
rem setting default time (1 hour = 60 minues)
set /a timeTo=60
rem asking user for input (integer)
set /p timeTo=
rem converting minutes to seconds - erroneous
set /a timeTo=*60
rem converting minutes to seconds - right approach
set /a timeTo=!timeTo!*60
rem command based on the inputted value
echo timeout /t !timeTo! /nobreak
)
Insert time in minutes:
Missing operand.
timeout /t 3600 /nobreak
==>

Batch does not support inline comments.All comments need to be in separate line:
::description for the user to read
echo Insert time in minutes:
::setting default time (1 hour)
set /a timeto=3600
::asking user for input (integer)
set /p timeto=
::converting minutes to seconds
set /a timeto=%timeto%*60
::command based on the inputted value
timeout /t %timeto% /nobreak

Related

Delayed Expansion Inside Loop with Arithmetic Operations on Time Values

I am relatively new to batch files, and I have been trying to get the following timing routine (inspired from Arithmetic operations with HH:MM:SS times in batch file) to work:
set "startTime=%time: =0%"
#rem Removing milliseconds information
set startTime=%startTime:~0,-3%
setlocal EnableDelayedExpansion
for %%i in (1 2 3 4 5) do (
timeout /T 3 /NOBREAK
set endTime=!time: =0!
set endTime=!endTime:~0,-3!
set /A "ss=(((1%endTime::=-100)*60+1%-100)-(((1%startTime::=-100)*60+1%-100)"
set /A "hh=ss/3600+100,ss%%=3600,mm=ss/60+100,ss=ss%%60+100"
#rem Get format in HH:MM:SS (maybe H:MM:SS after midnight.. not tested yet)
echo Start time: %startTime%
echo End time: !endTime!
#rem Issue here: Always get the same output despite delayedExpansion active
echo Diff time: !ss!
echo.
#rem Issue here: Not getting expected values
echo Elapsed time: !hh:~1!:!mm:~1!:!ss:~1!...
echo.
)
endlocal
I am not quite sure why the output value for the time difference is always the same despite the delayed expansion. Also, the formatted time does not give me the correct values.
Thanks for your help!
First, thanks for the helpful comments which helped me find a solution to my question. In case it might help someone else, I am posting the solution here.
As mentioned in the comments, the key was to determine the formatting used on my operating system.
The code that works for me is below, inspired from https://www.py4u.net/discuss/2286184:
#rem Check that the format of %TIME% is HH:MM:SS.CS for example 23:59:59.99
echo "%TIME%"
set STARTTIME=%TIME%
#rem convert start time to centiseconds
set /A STARTTIME=(1%STARTTIME:~0,2%-100)*360000 + (1%STARTTIME:~3,2%-100)*6000 + (1%STARTTIME:~6,2%-100)*100 + (1%STARTTIME:~9,2%-100)
setlocal EnableDelayedExpansion
for %%i in (1 2 3 4 5) do (
timeout /T 3 /NOBREAK
set ENDTIME=!TIME!
#rem convert ENDTIME to centiseconds
#rem I found that double-quotes with set /A and numbers are needed inside the for loop
set /A "ENDTIME=(1!ENDTIME:~0,2!-100)*360000 + (1!ENDTIME:~3,2!-100)*6000 + (1!ENDTIME:~6,2!-100)*100 + (1!ENDTIME:~9,2!-100)"
#rem calculating the duratyion is easy
set /A DURATION=!ENDTIME!-%STARTTIME%
#rem Adjust for cases where timings over multiple days
if !ENDTIME! LSS %STARTTIME% set set /A DURATION=%STARTTIME%-!ENDTIME!
#rem now break the centiseconds down to hours, minutes, seconds and the remaining centiseconds
set /A "DURATIONH=!DURATION! / 360000"
set /A "DURATIONM=(!DURATION! - !DURATIONH!*360000) / 6000"
set /A "DURATIONS=(!DURATION! - !DURATIONH!*360000 - !DURATIONM!*6000) / 100"
set /A "DURATIONHS=(!DURATION! - !DURATIONH!*360000 - !DURATIONM!*6000 - !DURATIONS!*100)"
#rem Enforce double-digit format
if !DURATIONH! LSS 10 set DURATIONH=0!DURATIONH!
if !DURATIONM! LSS 10 set DURATIONM=0!DURATIONM!
if !DURATIONS! LSS 10 set DURATIONS=0!DURATIONS!
if !DURATIONHS! LSS 10 set DURATIONHS=0!DURATIONHS!
#rem Outputing timings
echo STARTTIME: %STARTTIME% centiseconds
echo ENDTIME: !ENDTIME! centiseconds
echo DURATION: !DURATION! in centiseconds
echo Elapsed time: !DURATIONH!:!DURATIONM!:!DURATIONS!.!DURATIONHS!
)
endlocal

How to add some seconds to the current PC time?

I want to add some seconds to the current PC time. But I am a beginner in batch script coding and don't know how to modify the current time by adding some seconds.
This is the code I have so far:
Set "tijd=%time%"
echo %tijd%
echo %time%-%tijd%
pause
But the third command line does not output the expected result of current time increased by some seconds.
Here is a commented batch script to get current time without date and add 30 seconds to this time.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Get current time independent on region (country).
for /F "tokens=4-6 delims=/: " %%I in ('%SystemRoot%\System32\robocopy.exe "%SystemDrive%\|" . /NJH') do set "CurHour=%%I" & set "CurMinute=%%J" & set "CurSecond=%%K" & goto AddSeconds
rem The second value to add must be less than 60 for this code!
rem The code below adds 30 seconds to current time.
rem As second 08 and second 09 would be interpreted as invalid octal
rem numbers and so interpreted with value 0 in an arithmetic expression,
rem there is inserted the character 1 left to the second value to change
rem the second range to 100 to 159 (or 160 on leap second) and adding 30
rem is done by subtracting 70. The same method is used for the minutes 08
rem and 09 and the hours 08 and 09 on incrementing them by one if that is
rem necessary at all.
:AddSeconds
set "NewHour=%CurHour%"
set "NewMinute=%CurMinute%"
set /A NewSecond=1%CurSecond% - 70
if %NewSecond% LSS 60 goto TimeOutput
set /A NewSecond-=60
set /A NewMinute=1%NewMinute% - 99
if %NewMinute% LSS 60 goto TimeOutput
set /A NewMinute-=60
set /A NewHour=1%NewHour% - 99
if %NewHour% LSS 24 goto TimeOutput
set /A NewHour-=24
rem Before the new time is output, the second, minute and hour values are
rem formatted to have always two digits by first inserting 0 at beginning
rem and next use only the last two characters of the second, minute and
rem hour strings assigned to the three environment variables.
:TimeOutput
set "NewSecond=0%NewSecond%"
set "NewSecond=%NewSecond:~-2%"
set "NewMinute=0%NewMinute%"
set "NewMinute=%NewMinute:~-2%"
set "NewHour=0%NewHour%"
set "NewHour=%NewHour:~-2%"
echo Current time: %CurHour%:%CurMinute%:%CurSecond%
echo The new time: %NewHour%:%NewMinute%:%NewSecond%
endlocal
For the command line to get current time read either this answer written by Compo or third part of my answer on same question.
The Windows command processor cmd.exe has no support for time calculations. For that reason it is necessary on using just internal commands of cmd.exe to add 30 seconds to current time with using several arithmetic expressions and IF conditions.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
endlocal /?
for /?
goto /?
if /?
rem /?
robocopy /?
set /?
setlocal /?
See also:
Single line with multiple commands using Windows batch file for an explanation of & operator.
Microsoft documentation for the Windows Commands
SS64.com - A-Z index of Windows CMD commands

How to check numbers from a variable with an if in CMD (.bat file)

I know how to check time in command prompt, but I want to check if it's greater or equal to a number, I hope the example below is clear.
#echo off
for /f "tokens=*" %%a in ('time /t') do (
set t=%%a
)
set "dt=%t%"
if "%dt%" <= "12:00" (
echo it is morning
pause
)
if "%dt%" >= "12:00" (
echo It is night
pause
)
timeout /t 10 /nobreak>nul
I have tried 1200 and 12:00, is there a way to get the first 2 characters for a variable? I think this would be the easiest way.
to select the first 2 characters of the variable dt, use
set "selection=%dt:~0,2%"
The 0 is the start position and 2 the length.
See set /? |more from the prompt for documentation
BUT
the if command has very specific requirements for comparison operators - and > and < are use to redirect files.
See if /? from the prompt for documentation.
There are several ways to solve this problem. I think this is the simplest one:
#echo off
set "t=%time::=%"
set /A "dt=t"
if %dt% leq 120000 (
echo it is morning
pause
)
if %dt% geq 190000 (
echo It is night
pause
)
The set "t=%time::=%" command get the %time% dynamic variable, that have a "HH:MM:SS.CC" format, and assign "HHMMSS.CC" to t variable. Then, set /A "dt=t" command get the number until first dot: "dt=HHMMSS". After that, just compare such a value with no quotes.

Loop the command continuously (without pausing) for "X" seconds

Currently I am working on a batch file to execute a command continuously for X no. of seconds, and after elapse of X, I wish to redirect it using goto function.
This is the closest I have got to what I intend the code to do.
:MainMenu
set yourno=
set /p yourno=Input your number:
set /p looptime=Input time:
goto PerformCalc
:PerformCalc
set /a yourno= %yourno%+1
timeout 1
set /a looptime=%looptime%-1
goto CheckLoop
:CheckLoop
if %looptime% equ 0 goto FinishScreen
goto PerformCalc
:FinishScreen
echo Congratulations Your no. is %yourno%
echo Operations completed in %looptime% seconds
pause
Now the problem with the above code is if I set the loop time for 10 seconds, it will perform only 10 operations. But that is not something I intend to do. I realize that I have inputted timeout 1 in my code and reducing it by 1 everytime 1 second passes. However this is because I am unable to find a way to continuously loop the code for 10 seconds, without using timeout 1 thus pausing it to perform only 10 operations.
If you still do not get what I mean, here is the logical flow the code should perform (say for eg. the user inputs looptime as 10 seconds
For Time=10seconds
Do {set /a yourno= %yourno%+1 continuously}
After Time of 10 seconds has elapsed redirect to :FinishScreen
Thankyou for your help.
#echo off
setlocal
rem Get end time
set seconds=10
for /F "tokens=1-4 delims=:.," %%a in ("%time: =0%") do set /A "endTime=1%%a%%b%%c%%d+seconds*100"
echo Start: %time%
echo Working %seconds% seconds, please wait...
set yourNo=0
:loop
set /A yourNo+=1
rem Check if end time had been reached
for /F "tokens=1-4 delims=:.," %%a in ("%time: =0%") do if 1%%a%%b%%c%%d lss %endTime% goto loop
echo End: %time%
echo This program could complete %yourNo% loops in 10 seconds
This method may fail if the processing time pass over midnight, but a simple adjustment can solve this point, if needed...

I need to press a specific key and have my script " GO TO" another part in the script

I have a batch script that I desperately need help with. I use a batch script to grab files from folders and move them. The script sweeps the folders every 30 sec but you can "press a key to continue" also. I need to change it so I can press a specific key, doesn't matter what key, and have it GO TO the top location of my script to reset.
Below is a striped down version of what I currently have.
#echo off
set /p username=ENTER LOGIN:
echo.
:pc
echo 1 -- Flats
echo 2 -- Simple Tabletop
set /P rmFunc="Enter a choice: "
for %%I in (1 x) do if #%rmFunc%==#%%I goto run%%I
:run1
set pc=300
goto begin
:run2
set pc=400
goto begin
:begin
set studio=TT
set computer=Handbag_1
set setnumber=
set studiosetnumber=%studio%_%setnumber%
set delay_swch=5
set delay_loop=30
:bottom
echo logged in as: %username%
timeout /t %delay_loop%
GOTO BOTTOM
It is not possible to determine what key was pressed when using TIMEOUT, so that command is of no help.
You could use the CHOICE command with the /T (timeout) and /D (default) options instead. You can no longer simply press any key. Instead you can only press one key to restart, and another specific key to continue, or wait till the default value (continue) is returned. The biggest drawback is it no longer has a visual countdown.
echo Press C to Continue, or R to Restart
choice /c cr /d c /t 30 /n /m "The process will automatically continue in 30 seconds "
if errorlevel 2 goto pc
goto bottom
You could display a countdown timer by adding a third undocumented default option and setting the timeout to 1 second. If the default is returned, then it decrements the counter. Once the counter reaches 0 then the desired time has elapsed and you treat it as Continue.
I use the SET /P trick to display the prompt with a without a newline. I end with a carriage return so that the next prompt displays from the beginning of the same line.
The countdown timing will be a bit off, but it should be plenty good enough.
The carriage return variable must be defined and delayed expansion must be enabled near the top (before any loop label).
:: Define CR to hold a carriage return (0x0D)
for /f %%A in ('copy /Z "%~dpf0" nul') do set "CR=%%A"
:: Delayed expansion must be enabled to use the CR variable
setlocal enableDelayedExpansion
The timer code then becomes
set wait=15
:waitLoop
<nul set /p ="Press C to Continue, or R to Restart. Auto continue in %wait% seconds. !CR!"
choice /c 0cr /t 1 /d 0 >nul
if errorlevel 3 echo(&goto pc
if errorlevel 2 echo(&goto bottom
set /a wait-=1
if %wait% equ 0 echo(&goto bottom
goto :waitLoop
timeout does not return errorlevel if interrupted, so perhaps one way to solve this is to time timeout
#ECHO Off
SETLOCAL
SET /a "starttime=1%time:~-5,2%%time:~-2%"
timeout /t 30
SET /a "endtime=1%time:~-5,2%%time:~-2%"
IF %endtime% lss %starttime% SET /a endtime +=6000
SET /a elapsed=endtime - starttime
ECHO %elapsed%
GOTO :EOF
elapsed should contain the resultant time in tens-of-milliseconds. It's unlikely to be exactly 3000 for a 'no keypress' situation, but perhaps if %elapsed% gtr 2980 echo no keypress would suffice.
Note that I use a time format of hh:mm:ss.hh so if yours is different, you'd need to adjust the time substringing.
Since the target here is a timeout of about 30 seconds, the calculations can be performed on the seconds digits only. The values are accumulated as 1ss(.)hh which ensures that there is not a leading zero so set /a will not attempt to enter octal mode. Since it's entirely possible that the end time will be in the next minute, the adding of 6000 compensates by adding-in the minute (6000 tens-of-milliseconds)
Since it seems to be necessary to build the entire batch,
#echo off
set /p username=ENTER LOGIN:
echo.
:pc
echo 1 -- Flats
echo 2 -- Simple Tabletop
set /P rmFunc="Enter a choice: "
:
for %%I in (1 x) do if #%rmFunc%==#%%I goto run%%I
:run1
set pc=300
goto begin
:run2
set pc=400
goto begin
:begin
set studio=TT
set computer=Handbag_1
set setnumber=
set studiosetnumber=%studio%_%setnumber%
set delay_swch=5
set delay_loop=30
:bottom
echo logged in as: %username%
SET /a "starttime=1%time:~-5,2%%time:~-2%"
timeout /t %delay_loop%
SET /a "endtime=1%time:~-5,2%%time:~-2%"
IF %endtime% lss %starttime% SET /a endtime +=6000
SET /a elapsed=endtime - starttime
if %elapsed% leq 2980 goto ???
GOTO BOTTOM
And - well there I'd have to leave it. You are setting studio and setnumber to constants within your code. Are these somehow related to "Process Class?" If you want to re-enter this data, then you'd need ??? above to be begin - but you'd need to set /p those values (which you haven't shown.) As for the file sweep - there's nowhere in your code that shows this filesweep happening. The modifications I have indicated will allow you to press any key to interrupt the 30-second bottom loop and re-enter the data.

Resources