I am trying to find the biggest of 4 numbers in batch scripting but it is not working.
The GTR command is not getting executed.
From this line it never gets executed if !Counter_Senior! gtr !Max_Age! (
I'm new to batch scripting, I am not sure about alignment and spacing. please help me through.
echo off
setlocal enabledelayedexpansion
set /a Counter_Child=1
set /a Counter_Senior=2
set /a Counter_Older_adult=0
set /a Counter_Young_adult=3
set /a Max_Age=%counter_Child%
echo maximum age is %Max_Age%
if !Counter_Senior! gtr !Max_Age! (
set Max_Age=%Counter_Senior%
if !Counter_Older_adult! gtr !Max_Age! (
set Max_Age=%Counter_Older_adult%
if !%Counter_Young_adult! gtr !Max_Age! (
set Max_Age=%Counter_Young_adult%
time /t
echo Maximum age is %Max_Age%
goto:EOF
)
)
)
if !Counter_Older_adult! gtr !Max_Age! (
set %Max_Age%=%Counter_Older_adult%
if !%Counter_Young_adult! gtr !Max_Age! (
set %Max_Age%=%Counter_Young_adult%
echo Maximum age is %Max_Age%
goto:EOF
)
)
if !%Counter_Young_adult! gtr !Max_Age! (
set %Max_Age%=%Counter_Young_adult%
echo Maximum age is %Max_Age%
goto:EOF
)
echo Maximum age is %Max_Age%
goto:EOF
Your code seems way too complicated. Try this:
echo off
set Counter_Child=1
set Counter_Senior=2
set Counter_Older_adult=0
set Counter_Young_adult=3
set Max_Age=%Counter_Child%
IF %Counter_Senior% GTR %Max_Age% SET Max_Age=%Counter_Senior%
IF %Counter_Older_adult% GTR %Max_Age% SET Max_Age=%Counter_Older_adult%
IF %Counter_Young_adult% GTR %Max_Age% SET Max_Age=%Counter_Young_adult%
echo maximum age is %Max_Age%
If you only want the maximum value of the four Counter variables then you could use this:
For /F "Tokens=2 Delims==" %%A In ('Set Counter_'
) Do If %%A GEq !Max_Age! Set/A Max_Age=%%A
[Edit]And for the scenario suggested by Anders…
For %%A In (
%Counter_Child% %Counter_Senior% %Counter_Older_adult% %Counter_Young_adult%
) Do If %%A GEq !Max_Age! Set/A Max_Age=%%A
There are (at least) two problems with your code:
You are using goto:EOF inside a if and that quits the entire batch file.
In some places you incorrectly used set %Max_Age%=.. instead of set Max_Age=..
Your code is also overcomplicated and can be reduced to something simpler like the answer posted by MichaelS.
If you want to print specific information when a change is made you can use a generic sub procedure:
#echo off
goto Start
:CheckAge
setlocal enableextensions enabledelayedexpansion
set newage=!%1!
if %newage% gtr %Max_Age% (
echo %1 is older than %Max_Age%, the new maximum is %newage%
set Max_Age=%newage%
)
endlocal & set Max_Age=%Max_Age%
goto :EOF
:Start
set Counter_Child=1
set Counter_Senior=2
set Counter_Older_adult=0
set Counter_Young_adult=3
set Max_Age=0
call :CheckAge Counter_Child
call :CheckAge Counter_Senior
call :CheckAge Counter_Older_adult
call :CheckAge Counter_Young_adult
echo Maximum age is %Max_Age%
Related
i have a text file logging timestamps from ffprobe on video durations of some video files, which that text file looks like this:
14.068700
5.043011
84.071967
5.043011
104.058600
5.043011
134.055234
5.056000 ....
I am trying to add these up, since batch files do not allow for floating numbers i chose to use powershell. here is my following code:
set total=0
for /f "tokens=*" %%x in (timestamps.txt) do (
set item=%%x
echo !item!
for /f "delims=." %%i in ('powershell %total% + %item%') DO SET total=%%i
echo %total%
)
but again it seems cause it is a floating number i am unable to do something like
SET /a total=%total% + %total%
so that i can not add that as a variable in this line:
powershell %total% + %item%
I have tried every combo i can think of with no luck, lots of searches and nothing comes back.
any idea how to do this or is there a better way to add up all these in pure batch ?
a pure Powershell solution would have been much simpler, but seeing as you require batch-file with powershell:
#echo off
setlocal enabledelayedexpansion & set nums=
for /f "usebackq delims=" %%x in ("timestamps.txt") do set nums=!nums!%%x+
for /f "delims=" %%i in ('powershell %nums:~0,-1%') do set "total=%%i"
echo Total: %total%
We just append all the numbers with the operator + to a variable, then pass that variable to powershell and get the result.
Note! delayedexpansion is needed because we are setting
or by utilizing powershell to do all the work and simply assign the result to the variable:
#echo off
for /f "delims=" %%i in ('"powershell -command (Get-Content -Path "timestamps.txt" ^| Measure-Object -Sum^).Sum"') do set "total=%%i"
echo Total: %total%
so this is what i came up with, not as elegant as it possibly can be BUT it is working. Wish it was a bit quicker but PowerShell slows things down a bit but definitely worth it!
its crazy to think when doing dos they never thought of using floating numbers, lol
anyways here is code:
set seconds=0
set item=0
for /f "tokens=*" %%t in (%filename%_copy_two%fileextension%) do (
set item=%%t
for /f %%i in ('powershell !item!+!seconds!') do (set seconds=%%i)
)
for /f %%i in ('powershell -NoP "[Math]::Round(!seconds!/ 60,2)"') do (set minutes=%%i)
set HH=00
for /f "tokens=1 delims=." %%r in ('echo %minutes%') do set min=%%r
for /f "tokens=2 delims=." %%p in ('echo %minutes%') do set sec=%%p
IF %sec% GTR 60 (
set /a newSec=!sec!-60
if !newSec! lss 10 set newSec=0!newSec!
set /a newMin=1 + !min!
if !newMin! lss 10 set newMin=0!newMin!
) else (
set /a newMin=!min!
set /a newSec=!sec!
)
IF %newMin% LSS 60 (
if !newMin! lss 10 set newMin=0!newMin!
set MM=!newMin!
if !newSec! lss 10 set newSec=0!newSec!
set SS=!newSec!
GOTO playlistTotal
)
IF %newMin% GEQ 60 IF %newMin% LSS 120 (
set /a MM=!newMin!-60
if !MM! lss 10 set MM=0!MM!
set /a HH=01
set SS=!newSec!
GOTO playlistTotal
)
IF %newMin% GEQ 120 IF %newMin% LSS 180 (
set /a MM=!newMin!-120
if !MM! lss 10 set MM=0!MM!
set /a HH=02
set SS=!newSec!
GOTO playlistTotal
)
IF %newMin% GEQ 120 IF %newMin% LSS 240 (
set /a MM=!newMin!-180
if !MM! lss 10 set MM=0!MM!
set /a HH=03
set SS=!newSec!
GOTO playlistTotal
)
IF !newMin! EQU 240 (
set /a MM=!newMin!-240
if !MM! lss 10 set MM=0!MM!
set /a HH=04
set SS=!newSec!
GOTO playlistTotal
)
IF %newMin% Gtr 240 (
ECHO We do not suggest a single playlist over 4 hours.
echo Please Go back edit your list to be shorter.
ECHO And just append to it
del %filename%_copy%fileextension%
del %filename%_copy_two%fileextension%
GOTO editPlaylist
)
:playlistTotal
del %filename%_copy%fileextension%
del %filename%_copy_two%fileextension%
echo.
Echo Playlist a total duration of = !HH!:!MM!:!SS!
echo.
IF !newMin! EQU 240 (
set /a MM=!newMin!-240
if !MM! lss 10 set MM=0!MM!
set /a HH=04
set SS=!newSec!
GOTO playlistTotal
)
IF %newMin% Gtr 240 (
ECHO We do not suggest a single playlist over 4 hours.
echo Please Go back edit your list to be shorter.
ECHO And just append to it
del %filename%_copy%fileextension%
del %filename%_copy_two%fileextension%
GOTO editPlaylist
)
:playlistTotal
del %filename%_copy%fileextension%
del %filename%_copy_two%fileextension%
echo.
Echo Playlist has a total duration of= !HH!:!MM!:!SS!
echo.
I hope this helps someone out! kind of stinks i cant ask a question for 6 months for this question considering there is NOT solution that I can find that met me OP
And once again I posted my solution after a question which I try to do every time, have a great day!
EDIT: thanks to #Gerhard
I have changed to this, but leaving the code here in case someone needs for a similar issue in a different way.
for /f "delims=" %%i in ('"powershell -command (Get-Content -Path "%filename%_copy_two%fileextension%" ^| Measure-Object -Sum^).Sum"') do set "seconds=%%i"
for /f %%i in ('powershell -NoP "[Math]::Round(!seconds!/ 60,2)"') do (set minutes=%%i)
Then keeping the same as is till someone posts a better solution.
You can achieve this in a very simple and pure Batch file that run much faster than any PS:
#echo off
setlocal
set /A factor=1000000, total=0
for /F "tokens=1,2 delims=." %%a in (timestamps.txt) do set /A total+=%%a*factor+1%%b-factor
echo %total:~0,-6%.%total:~-6%
Of course, this solution is so simple because it assumes that all input numbers have 6 decimals! If this is not the case, then the code should be modified accordingly...
I have a long list and i want to split it up in parts that will wait for an external trigger to go on.
At first I tried a for loop that did 100 rows per run.
But because of differences in workload on the pc the amount of time it takes varies.
So i was wondering, is it possible to loop the FOR statement for a set time.
But could not find any example.
After adding the suggestion stephan made to use a for loop with delayed expansion, it now works.
How to include a maximum run time in the for loop?
'''
#ECHO OFF
SETLOCAL enabledelayedexpansion
CLS
set ENDTIME=!TIME!
set function_endtime=!ENDTIME!
for /F "tokens=1-4 delims=:.," %%a in ("%function_endtime%") do (
set /A "start=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
)
:: add 4 seconds to current time as end time
set /A function_endtime=start+(4*100)
:: Format the result for use in if loop
set /A hh=function_endtime/(60*60*100),rest=function_endtime%%(60*60*100), mm=rest/(60*100), rest%%=60*100, ss=rest/100, cc=rest%%100
if %hh% lss 10 set hh=0%hh%
if %mm% lss 10 set mm=0%mm%
if %ss% lss 10 set ss=0%ss%
if %cc% lss 10 set cc=0%cc%
set function_endtime=%hh%:%mm%:%ss%.%cc%
echo Finish: %function_endtime% now: %time%
FOR /F "tokens=1,2 delims=," %%A IN (C:\test.txt) DO (
echo %%A %%B
set "last_value=%%B"
if "!time!" geq "%function_endtime%" goto :done
)
:done
echo %time%
'''
you can't put code between goto and the :label I guess you completely misinterpreted my comment. Your code should look like:
...
FOR /F "tokens=1,2 delims=," %%A IN (C:\test.txt) DO (
echo %%A %%B
set "last_value=%%B"
if "!time!" geq "%function_endtime%" goto :done
)
:done
...
(without any testing or checking your logic)
I'm fairly new to batch and I've been trying to write some simple sorting programs. This program uses the most basic sorting system, and the code(from what I can see) seems to be error-free. Yet when I run it, a random list is generated, and it seems as if some sorting is going on, then the array is filled with the letter "m". I don't see why this is occurring, so if somebody could point me in the correct direction I would greatly appreciate it.
My code:
#echo off
color b
title sorting
set ar=0
set num=0
set check=0
set checknum=0
set totalnumber=500
set randmax=5000
:array
if %num% LSS %totalnumber% (
set /A a[%num%]=%random% %% %randmax%
set /A num=%num%+1
goto array
)
if %num% EQU %totalnumber% (
goto echo1
)
:echo1
for /F "tokens=2 delims==" %%s in ('set a[') do echo %%s
echo sort initialized
goto sort
)
:sort
set n=0
:sortloop
set /A m=%n%+1
if %n% EQU %totalnumber% (
goto check
)
if %a[%n%]% GTR %a[%m%]% (
set hold=%a[%m%]%
set a[%m%]=%a[%n%]%
set a[%n%]=%hold%
set /A n=%n%+1
goto sortloop
)
if %a[%n%]% LSS %a[%m%]% (
echo a[%n%] check
set /A n=%n%+1
goto sortloop
)
:check
set check=0
set checknum=0
:checkloop
set /A checknumplus=%checknum%+1
if %check% EQU %totalnumber% (
goto complete
)
if %checknum% EQU %totalnumber% (
set n=0
goto sort
)
if %a[%checknum%]% LSS %a[%checknumplus%]% (
set /A check=%check%+1
set /A checknum=%checknum%+1
goto checkloop
)
:complete
for /F "tokens=2 delims==" %%s in ('set a[') do echo %%s
for /F "tokens=2 delims==" %%s in ('set a[') do echo %%s > sortedlist.txt
When you need to use variables inside of variables in batch (most commonly when working with arrays), you need to use delayed expansion.
Right now, your code says set hold=%a[%m%]%. The interpreter is treating this value as the variable %a[% (which doesn't exist, so it using nothing), the literal character m, and the variable %]% (which also doesn't exist and is therefore empty).
To get around this, put setlocal enabledelayedexpansion at the top of your code and then change your set statement to set hold=!a[%m%]! (and do the same thing with the other lines that are using it).
Im trying to create a program in which you will enter 5 numbers randomly and will automatically shows the sorted entered numbers from lowest to highest. Here is my code.
#echo off
:main
set /p num1="Enter 1st :"
set /p num2="Enter 2nd :"
set /p num3="Enter 3rd :"
set /p num4="Enter 4th :"
set /p num5="Enter 5th :"
set /a high=0
set /a low=0
set /a ave=(num1+num2+num3+num4+num5)/5
if %num1% GTR %num2% (set /a high=%num1%) ELSE set /a high =%num2%
if %num3% GTR %high% (set /a high=%num3%)
if %num4% GTR %high% (set /a high=%num4%)
if %num5% GTR %high% (set /a high=%num5%)
if %num1% LSS %num2% (set /a low=%num1%) ELSE set /a low =%num2%
if %num3% LSS %low% (set /a low=%num3%)
if %num4% LSS %low% (set /a low=%num4%)
if %num5% LSS %low% (set /a low=%num5%)
ECHO.
ECHO Average: %ave%
ECHO Highest: %high%
ECHO Lowest: %low%
ECHO Input Numbers: %num1% %num2% %num3% %num4% %num5%
:end
set /p return="Continue?"
if "%return%"=="yes" GOTO main
GOTO exit
:exit
There are several ways to solve this problem. In the "one by one" method each variable must be compared vs. another one; this method is too much complex and is the one you used to read the values. You may also use sort command to sort the numbers (as Rafael tried in his answer); however, the numbers must be padded at left side with zeros in order to be correctly sorted.
When a certain process is repeated over several elements, the usual way to solve such a problem is via an array. You may find descriptions about array concept in several sites, like in Wikipedia. You may read how to use arrays in a Batch file at this post.
The Batch file below make good use of the fact that environment variables are kept automatically sorted; this way, the program just insert the numbers in an array and then take out its elements, so the sort process itself is not required:
#echo off
setlocal EnableDelayedExpansion
set N=5
:main
rem Delete array elements from previous cycle, if any:
for /F "delims==" %%a in ('set array[ 2^>NUL') do set "%%a="
rem Read the numbers; at same time, accumulate they in "ave" variable
rem and create the array with the proper index (with left zeros)
set ave=0
for /L %%i in (1,1,%N%) do (
set /P num="Enter number %%i :"
set /A ave+=num
set index=000000000!num!
set array[!index:~-10!]=!num!
)
set /A ave=ave/N
rem Assemble the output line and get low and high values
set "output="
set "low="
for /F "tokens=2 delims==" %%a in ('set array[') do (
if not defined low set low=%%a
set high=%%a
set output=!output! %%a
)
ECHO/
ECHO Average: %ave%
ECHO Highest: %high%
ECHO Lowest: %low%
ECHO Input Numbers: %output%
set /p return="Continue?"
if "%return%"=="yes" GOTO main
I was looking for an answer to a similar "sort numbers" question, and want to add example of using SORT command as an alternative for completeness. Note that using SORT requires writing a file to disk and seems less efficient:
#echo off
setlocal EnableDelayedExpansion
set "num_1=2" & set "num_2=5" & set "num_3=1" & set "num_4=18" & set "num_5=10"
for /f "tokens=2 delims==" %%i in ('set num_') do (set /a "var=1000000+%%i"
echo !var! >> nums.txt)
for /f %%j in ('sort nums.txt') do (set /a "var=%%j-1000000"
set "nums=!nums! !var!")
del /q nums.txt & echo !nums!
:eol
I am making a program and it has a change log. The programs change log must say the version and then show how many days ago it was. So it should say:
[2013/03/19, 2 days ago]
I used this code:
set versionreleasedate1=2013/03/17
set /a verionday=%versionreleasedate1%-%date%
echo Version: 1.0 BETA [2013/03/19, %verionday% days ago]
but I know that the date it is dividing because cmd thinks that the \ symbol is devide buy I want that a little sepricator for the date.
So it would work like this
set /a var1=2013/03/17-%date% (todays date)
and echo how many days ago. Any help would be awesome thanks.
This works for my machine, and hopefully for yours:
#echo off &setlocal
set "versionreleasedate1=2013/03/17"
set "versionreleasetime1=00:00:00,00"
call :GetInternational
call :GetSecs "%versionreleasedate1%" "%versionreleasetime1%" startsec
call :GetSecs "%date%" "%time%" stopsec
set /a versionday=(%stopsec%-%startsec%)/86400
echo Version: 1.0 BETA [%versionreleasedate1%, %versionday% days ago]
goto :eof
:GetInternational
:: Sets a bundle of variables by reading the registry settings
for /f "tokens=1,2*" %%a in (' reg query "HKCU\Control Panel\International"^|find "REG_SZ" ') do set "%%a=%%c"
goto :eof
:GetSecs "dateIn" "timeIn" secondsOut
:: Output: Seconds elapsed since 1th Jan. 1970 00:00:00
:: http://www.dostips.com/forum/viewtopic.php?p=6450#p6450
setlocal
set "dateIn=%~1"
for /f "tokens=2" %%i in ("%dateIn%") do set "dateIn=%%i"
for /f "tokens=1-3 delims=%sDate%" %%a in ("%dateIn%") do (
if %iDate%==0 set /a mm=100%%a%%100,dd=100%%b%%100,yy=10000%%c%%10000
if %iDate%==1 set /a dd=100%%a%%100,mm=100%%b%%100,yy=10000%%c%%10000
if %iDate%==2 set /a yy=10000%%a%%10000,mm=100%%b%%100,dd=100%%c%%100
)
for /f "tokens=1-3 delims=%sTime%%sDecimal% " %%a in ("%~2") do (
set "hh=%%a"
set "nn=%%b"
set "ss=%%c"
)
if 1%hh% lss 20 set hh=0%hh%
if "%nn:~2,1%" equ "p" if "%hh%" neq "12" (set "hh=1%hh%" &set /a hh-=88)
if "%nn:~2,1%" equ "a" if "%hh%" equ "12" set "hh=00"
if "%nn:~2,1%" geq "a" set "nn=%nn:~0,2%"
set /a hh=100%hh%%%100,nn=100%nn%%%100,ss=100%ss%%%100
set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2,j=j/5+dd+y*365+y/4-y/100+y/400-2472633,j=j*86400+hh*3600+nn*60+ss
endlocal &set "%~3=%j%"
goto :eof
endlocal
%versionreleasedate1% & %versionreleasetime1% must be in the same format like %date% & %time% on your machine!