Using multiple colors in matrix batch file - batch-file

I've been messing around with batch files a bit, and have this code running. I did not write it, I found it, and it just runs random blue numbers in command prompt until you exit. My question is this, I want to make one where every character is a random color. so essentially, it'll look like a rainbow vomited skittles on my screen. But i digress. That is my challenge, and so far i have failed at solving it. Any ideas would be great!
#echo off
color 01
cls
:a
set /a a=%random%
if %a% geq 16384 set /a aa=1
if %a% lss 16384 set /a aa=0
set /a b=%random%
if %b% geq 16384 set /a bb=1
if %b% lss 16384 set /a bb=0
set /a c=%random%
if %c% geq 16384 set /a cc=1
if %c% lss 16384 set /a cc=0
set /a d=%random%
if %d% geq 16384 set /a dd=1
if %d% lss 16384 set /a dd=0
set /a e=%random%
if %e% geq 16384 set /a ee=1
if %e% lss 16384 set /a ee=0
set /a f=%random%
if %f% geq 16384 set /a ff=1
if %f% lss 16384 set /a ff=0
set /a g=%random%
if %g% geq 16384 set /a gg=1
if %g% lss 16384 set /a gg=0
set /a h=%random%
if %h% geq 16384 set /a hh=1
if %h% lss 16384 set /a hh=0
set /a i=%random%
if %i% geq 16384 set /a ii=1
if %i% lss 16384 set /a ii=0
set /a j=%random%
if %j% geq 16384 set /a jj=1
if %j% lss 16384 set /a jj=0
set /a k=%random%
if %k% geq 16384 set /a kk=1
if %k% lss 16384 set /a kk=0
set /a l=%random%
if %l% geq 16384 set /a ll=1
if %l% lss 16384 set /a ll=0
set /a m=%random%
if %m% geq 16384 set /a mm=1
if %m% lss 16384 set /a mm=0
set /a n=%random%
if %n% geq 16384 set /a nn=1
if %n% lss 16384 set /a nn=0
set /a o=%random%
if %o% geq 16384 set /a oo=1
if %o% lss 16384 set /a oo=0
set /a p=%random%
if %p% geq 16384 set /a pp=1
if %p% lss 16384 set /a pp=0
set /a q=%random%
if %q% geq 16384 set /a qq=1
if %q% lss 16384 set /a qq=0
set /a r=%random%
if %r% geq 16384 set /a rr=1
if %r% lss 16384 set /a rr=0
set /a s=%random%
if %s% geq 16384 set /a ss=1
if %s% lss 16384 set /a ss=0
set /a t=%random%
if %t% geq 16384 set /a tt=1
if %t% lss 16384 set /a tt=0
set /a u=%random%
if %u% geq 16384 set /a uu=1
if %u% lss 16384 set /a uu=0
set /a v=%random%
if %v% geq 16384 set /a vv=1
if %v% lss 16384 set /a vv=0
set /a w=%random%
if %w% geq 16384 set /a ww=1
if %w% lss 16384 set /a ww=0
set /a x=%random%
if %x% geq 16384 set /a xx=1
if %x% lss 16384 set /a xx=0
set /a y=%random%
if %y% geq 16384 set /a yy=1
if %y% lss 16384 set /a yy=0
set /a z=%random%
if %z% geq 16384 set /a zz=1
if %z% lss 16384 set /a zz=0
echo %aa% %bb% %cc% %dd% %ee% %ff% %gg% %hh% %ii% %jj% %kk% %ll% %mm% %nn% %oo% %pp% %qq% %rr% %ss% %tt% %uu% %vv% %ww% %xx% %yy% %zz%
goto a

I don't know if you got it yet but I had the same thought
and Decided to look it up I could not find anything but how to use colors and a random number generator so here's what I got
#echo off
SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
set "DEL=%%a"
)
:a-0a0
set eddie=%time:~-2%
set first=%eddie:~0,1%
if %first%==0 set eddie=%eddie:~1,1%
set /a eddie=(%eddie%%%9)
call :ColorText 0%eddie% " %random%"
goto a-0a0
pause >null
:ColorText
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1

Related

Trying to make a 8-4 hexadecimal number generator but for some reason it gives me 4-4 numbers

#echo off
echo lol testing
pause
#echo off
Setlocal EnableDelayedExpansion
Set _RNDLength=8
Set _Alphanumeric=0123456789ABCDEF
Set _Str=%_Alphanumeric%987654321
:_LenLoop
IF NOT "%_Str:~18%"=="" SET _Str=%_Str:~9%& SET /A _Len+=9& GOTO :_LenLoop
SET _tmp=%_Str:~9,1%
SET /A _Len=_Len+_tmp
Set _count=0
SET _RndAlphaNum=
SET :_loop
:_loop
Set /a _count+=1
SET _RND=%Random%
Set /A _RND=_RND%%%_Len%
SET _RndAlphaNum=!_RndAlphaNum!!_Alphanumeric:~%_RND%,1!
If !_count! lss %_RNDLength% goto :_loop
Set _RNDZLength=4
SET _RNDZ=%Random%
SET _RndAlphaNumz=
:_loop
SET _RndAlphaNumz=!_RndAlphaNumz!!_Alphanumeric:~%_RNDZ%,1!
If !_count! lss %_RNDZLength% goto _loop
Set _RNDZALength=4
SET _RNDZA=%Random%
SET _RndAlphaNumez=!_RndAlphaNumez!!_Alphanumeric:~%_RNDZA%,1!
If !_count! lss %_RNDZALength% goto _loop
Set _RNDZADLength=4
SET _RNDZAD=%Random%
SET _RndAlphaNumedz=!_RndAlphaNumedz!!_Alphanumeric:~%_RNDZAD%,1!
If !_count! lss %_RNDZALength% goto _loop
Set _RNDZADALength=12
SET _RNDZADA=%Random%
SET _RndAlphaNumedaz=!_RndAlphaNumedaz!!_Alphanumeric:~%_RNDZADA%,1!
If !_count! lss %_RNDZALength% goto _loop
echo !_RndAlphaNum!-!_RndAlphaNumz!
pause
this is my code (i am aware that only 2 variables are in the echo) i am trying to make it print 8 - 4 - 4 - 12
tried a lot of things could anyone help?
i copied the code from the 4-4 generator i made but i changed it from 4 to 8
been on this for like 2 hours trying to figure out whats wrong but have not found
This seems like an XY problem to me. If you're trying to generate a GUID, there are probably easier ways.
But if you prefer to reinvent the wheel, then I suggest using a Batch function to generate your randomness would be a much cleaner solution than your chutes-and-ladders code. Something like this:
#echo off & setlocal disabledelayedexpansion
call :rndhex 8 group[0]
call :rndhex 4 group[1]
call :rndhex 4 group[2]
call :rndhex 4 group[3]
call :rndhex 12 group[4]
echo %group[0]%-%group[1]%-%group[2]%-%group[3]%-%group[4]%
exit /b
:rndhex <length> <return_var>
setlocal enabledelayedexpansion
for %%I in (
"0=0" "1=1" "2=2" "3=3" "4=4" "5=5" "6=6" "7=7" "8=8"
"9=9" "10=A" "11=B" "12=C" "13=D" "14=E" "15=F"
) do set %%I
for /L %%I in (1,1,%~1) do (
set /a "digit = !random! * 16 / 32768"
for %%A in (!digit!) do set "hex=!%%A!!hex!"
)
endlocal & set "%~2=%hex%"
exit /b

Batch Script to get value from For Loop

I've been trying to read line by line from text file. In each line, I do have "starttime" & "endtime" field in 3 & 4 th column as I highlighted below.
File1,110543,2020-07-18T03:09:12.1321687+00:00,2020-07-18T03:10:22.4097433+00:00,000001
File2,210543,2020-07-18T04:19:28.0459100+00:00,2020-07-18T04:26:08.6626472+00:00,000002
I just want to find the difference between "starttime" & "endtime" column using below script.
#Echo off&Setlocal EnableExtensions EnableDelayedExpansion
IF EXIST "%csvFilePath%tempfile.txt" (
( for /f "tokens=1-5 delims=," %%A in (%csvFilePath%tempfile.txt) do (
echo fileRecord %%A,%%B,%%C,%%D,%%E
for /f "tokens=1,2,3,4 delims=T:." %%a in ("%%C") Do (
set starttime=%%b:%%c:%%d
)
for /f "tokens=1,2,3,4 delims=T:." %%a in ("%%D") Do (
set endtime=%%b:%%c:%%d
)
echo !starttime!
echo !endtime!
set options="tokens=1-4 delims=:.,"
for /f %options% %%a in (!starttime!) do set start_h=%%a&set /a start_m=100%%b %% 100&set /a start_s=100%%c %% 100&set /a start_ms=100%%d %% 100
for /f %options% %%a in (!endtime!) do set end_h=%%a&set /a end_m=100%%b %% 100&set /a end_s=100%%c %% 100&set /a end_ms=100%%d %% 1
set /a hours=%end_h%-%start_h%
set /a mins=%end_m%-%start_m%
set /a secs=%end_s%-%start_s%
set /a ms=%end_ms%-%start_ms%
if %ms% lss 0 set /a secs = %secs% - 1 & set /a ms = 100%ms%
if %secs% lss 0 set /a mins = %mins% - 1 & set /a secs = 60%secs%
if %mins% lss 0 set /a hours = %hours% - 1 & set /a mins = 60%mins%
if %hours% lss 0 set /a hours = 24%hours%
if 1%ms% lss 100 set ms=0%ms%
:: Mission accomplished
set /a totalsecs = %hours%*3600 + %mins%*60 + %secs%
echo command took %hours%:%mins%:%secs%.%ms% (%totalsecs%.%ms%s total))))
start(03:09:12) & endtime(03:10:22) value is being printed for line1 as expected when I'm using !starttime! & !endtime!. I'm trying to pass the same variable to calculate the difference in below line.
for /f %options% %%a in (!starttime!) do set start_h=%%a&set /a start_m=100%%b %% 100&set /a start_s=100%%c %% 100&set /a start_ms=100%%d %% 100
for /f %options% %%a in (!endtime!) do set end_h=%%a&set /a end_m=100%%b %% 100&set /a end_s=100%%c %% 100&set /a end_ms=100%%d %% 1
after execution, I'm getting "0 was unexpected at this time". Can you pls help me on this
There are some issues in your code:
don't use :: as a comment. Especially within a code block. It might break your code or lead to unexpected and hard to troubleshoot results.
you need delayed expansion for all variables defined in the code block (you used delayed expansion for a few variables, but not for all.
set /a doesn't need % or ! at all (optional, but here you'd need ! instead of %).
to process a string with for /f, it needs to be quoted, or for tries to find a file with that name.
You had a superfluent pair of parentheses.
%csvFilePath% is not defined.
for /f %options% ... works, but not with delayed variables, so I moved the declaration of %options% out of the loop.
Your code was not properly indented, so the structure was hardly recognizable (I spotted the lack of delayed expansion only after indenting, which made it blatantly obvious)
Your code with all those issues fixed:
#Echo off
Setlocal EnableExtensions EnableDelayedExpansion
set "options=tokens=1-4 delims=:.,"
IF EXIST "%csvFilePath%tempfile.txt" (
for /f "tokens=1-5 delims=," %%A in (%csvFilePath%tempfile.txt) do (
echo fileRecord %%A,%%B,%%C,%%D,%%E
for /f "tokens=1,2,3,4 delims=T:." %%a in ("%%C") Do set starttime=%%b:%%c:%%d
for /f "tokens=1,2,3,4 delims=T:." %%a in ("%%D") Do set endtime=%%b:%%c:%%d
echo !starttime!
echo !endtime!
for /f "%options%" %%a in ("!starttime!") do set start_h=%%a&set /a start_m=100%%b %% 100&set /a start_s=100%%c %% 100&set /a start_ms=100%%d %% 100
for /f "%options%" %%a in ("!endtime!") do set end_h=%%a&set /a end_m=100%%b %% 100&set /a end_s=100%%c %% 100&set /a end_ms=100%%d %% 1
set /a hours=end_h-start_h
set /a mins=end_m-start_m
set /a secs=end_s-start_s
set /a ms=end_ms-start_ms
if !ms! lss 0 set /a secs = secs - 1 & set /a ms = 100!ms!
if !secs! lss 0 set /a mins = mins - 1 & set /a secs = 60!secs!
if !mins! lss 0 set /a hours = hours - 1 & set /a mins = 60!mins!
if !hours! lss 0 set /a hours = 24!hours!
if 1!ms! lss 100 set ms=0!ms!
REM Mission accomplished
set /a totalsecs = hours*3600 + mins*60 + secs
echo command took !hours!:!mins!:!secs!.!ms! (!totalsecs!.!ms!s total^)
)
)
(Note: I didn't check your math - I trust you there, as the results seem plausible)
Output with your example file:
fileRecord File1,110543,2020-07-18T03:09:12.1321687+00:00,2020-07-18T03:10:22.4097433+00:00,000001
03:09:12
03:10:22
command took 0:1:10.00 (70.00s total)
fileRecord File2,210543,2020-07-18T04:19:28.0459100+00:00,2020-07-18T04:26:08.6626472+00:00,000002
04:19:28
04:26:08
command took 0:6:40.00 (400.00s total)

(Use a batch file) Get the filename only into a For loop

This is the new version according to your comments.
It still doesn't work.
I try to rename files like
2006 Gaspésie - Croisière à la baleine 1023.jpg
2006 Gaspésie - Croisière baleine 28.jpg
To
2006-06 Croisière baleine 001.jpg
2006-06 Croisière baleine 002.jpg
The problem is that the fields F1 and F2 are empty when I do echo and when I do rename.
#echo off
setlocal ENABLEDELAYEDEXPANSION
set /p "NewFileName=New name file (without the extension): "
set /p "LenghtMask=Mask lenght of the number: "
set /a FileNo=0
for %%I in (*.JPG) do (
echo.
#echo off
set /a FileNo=!FileNo!+1
call :return_no_mask !FileNo! %LenghtMask% FileNoMask
#echo on
set "F1=%%~I"
echo F1 %F1%
set ExtensionName=%%~xI
set "F2=%NewFileName% %FileNoMask%%ExtensionName%"
echo F2 %F2%
rename "%F1%" "%F2%"
pause
)
goto:EOF
::--------------------------------------------------
::Return the number (fill with 0 to have the right lenght)
:: in Number to use
:: in Mask's lenght ex: 2
:: out Number
::--------------------------------------------------
:return_no_mask
set NoIn=%1
::---Search the lenght of the number
set /a LenghtNo=1
if "%NoIn%" GEQ "10" set /a LenghtNo=2
if "%NoIn%" GEQ "100" set /a LenghtNo=3
if "%NoIn%" GEQ "1000" set /a LenghtNo=4
if "%NoIn%" GEQ "10000" set /a LenghtNo=5
::---Fill with zero
set NoOut=
:test_add
if "%LenghtNo%" LSS "%2" (
set NoOut=0%NoOut%
set /a LenghtNo+=1
goto :test_add
)
set NoOut=%NoOut%%1
set %3=%NoOut%
EXIT /B 0
::--------------------------------------------------
::End of file
::--------------------------------------------------
:EOF
endlocal
Thank you

Decimal to Hexadecimal & binary converter Batch program, Any suggestions how to implement it differently?

Ive been working on this decimal to hexadecimal and binary converter, the hexadecimal converts the decimal number into hex, and the binary converter does it's job.
This works just fine, but is there a different way to implement this code?
#echo off
setlocal EnableDelayedExpansion
:Main
setlocal
cls
set /p decIn=enter Decimal value:
call :DectoHex %decIn% hexOut
call :DectoBin %decIn% binOut
echo !hexOut!
echo !binOut!
pause>nul
goto :main
endlocal
::-------------------------------------------------------------------------------------
:DectoHex
set /a A=%1
set map=0123456789ABCDEF
set H=
:Hexloop
set /a B=!A! %%16 & set /a A /=16
set H=!map:~%B%,1!!H!
if !A! gtr 0 goto :Hexloop
set %2=!H!
goto :eof
::-------------------------------------------------------------------------------
:DectoBin
:: Note Dec Bit Bin (DBB)
set /a Dec=%1
set Bin=
for /L %%i in (1,1,32) do (
set /a "Bit=Dec&1, Dec>>=1"
set Bin=!Bit!!Bin!
)
:skimming
if %Bin:~0,1% == 0 (
set Bin=%Bin:~1% & goto :skimming
)
set %2=!Bin!
goto :eof
::-----------------------------------------------------------------------------
:BintoDec
set bin=11011
set dig=
set digval=1
set dec=
:Loop
if %bin% gtr 1 (
set dig=%bin:~-1%
set bin=%bin:~0,-1%
) else (
set /a dig=%bin%
set bin=0
)
if %dig% equ 1 (
set /a dec+=%digval%
)
set digval *=2
if %bin% gtr 0 goto :loop
echo %dec%
pause>nul
goto :eof
The main function recieves an input from the user and passes the variable as a parameter to a hexadecimal converter and a binary converter, the
To convert to/from any number base between 2 and 36:
:: Q:\Test\2017\09\16\SO_46254352.cmd
#echo off & setlocal EnableDelayedExpansion
set map=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
:Main
cls
set "DecIn="
set /p DecIn=enter value:
if not defined DecIn goto :Eof
Set /A DecIn=DecIn
call :Dec2Base 2 %DecIn% BaseOut
call :Base2Dec 2 %BaseOut% DecBack
echo DecBack=%DecBack% Base 2=%BaseOut%
call :Dec2Base 16 %DecIn% BaseOut
call :Base2Dec 16 %BaseOut% DecBack
echo DecBack=%DecBack% Base 16=%BaseOut%
call :Dec2Base 32 %DecIn% BaseOut
call :Base2Dec 32 %BaseOut% DecBack
echo DecBack=%DecBack% Base 32=%BaseOut%
goto :main
::-------------------------------------------------------------------------------------
:Dec2Base Base DecIn BaseOut
Setlocal
Echo:%2|findstr /i "^[%map:~0,10%]*$" >Nul 2>&1 ||(Echo invalid char for base 10&Goto :Eof)
set /a Num=%2
set "Ret="
:Dec2BaseLoop
set /a "Digit=Num %% %1"
set /a "Num /= %1"
set Ret=!map:~%Digit%,1!%Ret%
if "%Num%" neq "0" goto :Dec2BaseLoop
Endlocal&Set "%3=%Ret%"&Goto :Eof
::-------------------------------------------------------------------------------------
:Base2Dec Base BaseIn DecBack
Setlocal EnableDelayedExpansion
Set /A "Base=%1,PlaceVal=1,Ret=0"
Echo:%2|findstr /i "^[!map:,%Base%!]*$" >Nul 2>&1 ||(Echo invalid char for base %1&Goto :Eof)
Set Val=%2
:Base2DecLoop
Set "Digit=%Val:~-1%"
If %Digit% Leq 9 goto :Next
For /L %%i in (10,1,%Base%) Do If /i "!Digit!" Equ "!map:~%%i,1!" (Set "Digit=%%i" & Goto :Next )
Echo Something went wrong & Pause
:Next
set /A "Ret+=Digit * PlaceVal,PlaceVal *= Base"
Set "Val=%Val:~0,-1%"
If defined Val goto :Base2DecLoop
Endlocal & Set "%3=%Ret%" & Goto :Eof
Sample output:
> Q:\Test\2017\09\16\SO_46254352.cmd
enter value: 4095
DecBack=4095 Base 2=111111111111
DecBack=4095 Base 16=FFF
DecBack=4095 Base 32=3VV
enter value: 0xBadAffe
DecBack=195932158 Base 2=1011101011011010111111111110
DecBack=195932158 Base 16=BADAFFE
DecBack=195932158 Base 32=5QRBVU
In a function, it gaves that.
(Put in in a UTF-8 .txt file with BOM EFBBBF, first line must be blank)
cls
#echo off
cd /d "%~dp0"
chcp 65001 >nul
set title_script=Base conversion
title %title_script%
set "decimal_number=200"
set "result_hexadecimal="
call :function_change_base_number 10 decimal_number 16 result_hexadecimal
echo %decimal_number% base 10 = %result_hexadecimal% base 16
echo.
set "octal_number=310"
set "result_binary="
call :function_change_base_number 8 octal_number 2 result_binary
echo %octal_number% base 8 = %result_binary% base 2
echo.
set "quaternary_number=3020"
set "base_36_result="
call :function_change_base_number 4 quaternary_number 36 base_36_result
echo %quaternary_number% base 4 = %base_36_result% base 36
echo.
pause
exit
:function_change_base_number
setlocal enabledelayedexpansion
set /a input_base=%~1
set input_number=!%~2!
set /a output_base=%~3
rem UCase
for %%i in ("a=A" "b=B" "c=C" "d=D" "e=E" "f=F" "g=G" "h=H" "i=I" "j=J" "k=K" "l=L" "m=M" "n=N" "o=O" "p=P" "q=Q" "r=R" "s=S" "t=T" "u=U" "v=V" "w=W" "x=X" "y=Y" "z=Z") do (
call set "input_number=!input_number:%%~i!"
)
set map=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
set map_base=!map:~0,%input_base%!
set /a position=0
set input_number_check=%input_number%
:loop_1
set current_map_base_character=!map_base:~%position%,1!
set input_number_check=!input_number_check:%current_map_base_character%=!
if "!input_number_check!" == "" goto next_1
set /a position+=1
if "!map_base:~%position%,1!" NEQ "" goto loop_1
echo Error.
echo Invalid character in base %input_base%.
echo.
pause
exit
:next_1
set /a carry_flag=1
set "input_number_tmp=%input_number%"
set "result_decimal="
:loop_2
set "input_number_tmp_digit=%input_number_tmp:~-1%"
if %input_number_tmp_digit% LEQ 9 goto next_2
for /l %%i in (10,1,%input_base%) do (
if /i "%input_number_tmp_digit%" EQU "!map:~%%i,1!" (
set "input_number_tmp_digit=%%i"
goto :next_2
)
)
echo Error.
:next_2
set /a result_decimal+=input_number_tmp_digit * carry_flag
set /a carry_flag *= input_base
set "input_number_tmp=%input_number_tmp:~0,-1%"
if defined input_number_tmp goto :loop_2
set /a input_number_tmp=%result_decimal%
set "result="
:loop_3
set /a "division_remainder=input_number_tmp %% !output_base!"
set /a "input_number_tmp /= !output_base!"
set result=!map:~%division_remainder%,1!%result%
if "%input_number_tmp%" NEQ "0" goto loop_3
endlocal & set %~4=%result%
goto :eof

How to get PC uptime in batch?

I have a script here that will give me an uptime but it is not the correct up time and I dont really know where the error is or if this is even the best way to do it but it is what I have. End game I want to set the uptime to a variable and if greater than x I will do a reboot or promt the user for a reboot and I will put this script in under the task scheduler. Either way here is what I have. Hope it is sufficient enough and I dont get scaled too bad over a stupid question.
#echo off
setlocal ENABLEEXTENSIONS
call :Uptime d h n s
echo/Uptime is: %d% days, %h% hours, %n% minutes, %s% seconds.
pause
goto :EOF
:Uptime days hours mins [secs]
setlocal ENABLEEXTENSIONS & set "c=net statistics work"
set t=2&if "%date%z" LSS "A" set t=1
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('echo/^|date') do (
for /f "tokens=%t%-4 delims=.-/ " %%d in ('date/t') do (
set %%a=%%d&set %%b=%%e&set %%c=%%f))
for /f "tokens=5-8 delims=:. " %%a in ('echo/^|time') do (
set "hh=%%a" & set "nn=%%b" & set "ss=%%c")
if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
set /a dd=100%dd%%%100,mm=100%mm%%%100
set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633
if 1%hh% LSS 20 set hh=0%hh%
set /a hh=100%hh%%%100,nn=100%nn%%%100,f=j*1440+hh*60+nn
for /f "tokens=3-8 delims=/:M " %%a in ('%c%^|findstr/b /c:"Stat"') do (
set mm=%%a&set dd=%%b&set yy=%%c&set hh=%%d&set nn=%%e%%f)
if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
set /a dd=100%dd%%%100,mm=100%mm%%%100
set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633
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,s=j*1440+hh*60+nn,n=f-s
set /a d=n/1440,n%%=1440,h=n/60,n%%=60
endlocal & set "%1=%d%" & set "%2=%h%" & set "%3=%n%" & (if "%4" NEQ "" set "%4=%ss%") & goto :EOF
Try a batch file leveraging powershell.
#Echo Off&SetLocal
For /F "UseBackQ Tokens=1-4" %%A In (
`Powershell "$OS=GWmi Win32_OperatingSystem;$UP=(Get-Date)-"^
"($OS.ConvertToDateTime($OS.LastBootUpTime));$DO='d='+$UP.Days+"^
"' h='+$UP.Hours+' n='+$UP.Minutes+' s='+$UP.Seconds;Echo $DO"`) Do (
Set "%%A"&Set "%%B"&Set "%%C"&Set "%%D")
Echo(Uptime is: %d% days, %h% hours, %n% minutes, %s% seconds.
>Nul Timeout -1&Exit/B

Resources