如何批量在数字变量前添加 0

How can I add 0 before a numeric variable in batch

我似乎无法在任何地方找到答案,即使是 "basic" 我认为其他人已经做过的事情。

总之,这里什么都没有。

我制作了一个简单的批处理脚本作为计时器(计时) 它采用 dd:hh:mm:ss 格式,但只显示必要的内容。 例如,如果天数和小时数 = 0,则只有 mm:ss 等

现在我 运行 在从 0 到 9 时遇到了一个小问题...

我想让它说 00-01-02-03-04-05... 但现在它说 0-1-2- 3-4-5...

假设我把时间放在一个循环中,延迟 1 秒总是增加一秒。 我制作了一个数字 /a 变量,因为我正在处理数字。但我似乎无法弄清楚如果数字小于 10,如何在数字前添加 0...

到目前为止,我已经尝试了以下方法:

echo %s:""=0%

没关系,我在输入此内容时想出了解决方案...请参阅我的回答

解决方案是这样的 -> set s=0%s% 然后再转换回数值...通过做 set /a s+=0 该死的...

这实际上比看起来容易得多。

要将任何简单(非 32 位溢出)数字从前导 0 转换为数字:

只需减去前面有 2 的值减去前面有 1 的值与前面有 1 的值之间的差值。

即 1[你的价值] - ( 2[你的价值] - 1 [你的价值] )

转换为前导0:

确定所需的前导 0 将数字设置为具有 0 的数量加上它的当前值,然后 select 最后 N 个字符表示您希望拥有的总共 N 个数字。

即两个数字总填充 00 然后是当前值和 select 最后两个数字。

这里只是更容易演示,使用我的时间函数和一些简单的函数来转换值:

@(SETLOCAL EnableDelayedExpansion
  ECHO OFF
  CALL :Get-DateTime
)

CALL :Main

( ENDLOCAL
  EXIT /B
)

:Main
  ECHO.Initial Values:                        HH = "%HH%"  -- MN = "%MN%"  -- SS = "%SS%"
  CALL :Convert_To_Leading_Zero HH
  CALL :Convert_To_Leading_Zero MN %MN%
  CALL :Convert_To_Leading_Zero SS %SS%
  ECHO.After Convert_To_Leading_Zero Values:  HH = "%HH%"  -- MN = "%MN%"  -- SS = "%SS%"
  CALL :Convert_To_Number HH
  CALL :Convert_To_Number MN %MN%
  CALL :Convert_To_Number SS %SS%
  ECHO.Convert_To_Number:                     HH = "%HH%"  -- MN = "%MN%"  -- SS = "%SS%"
  CALL :Convert_To_Leading_Zero HH
  CALL :Convert_To_Leading_Zero MN %MN%
  CALL :Convert_To_Leading_Zero SS
  ECHO.After Convert_To_Leading_Zero Values:  HH = "%HH%"  -- MN = "%MN%"  -- SS = "%SS%"

GOTO :EOF

:Convert_To_Leading_Zero
  REM %1 -- Variable to Return
  REM %2 -- Value to Convert IF EMpty Will try to use !%1! instead!
  IF /I "%~2" NEQ "" (
    SET "%~1=0%~2"
    SET "%~1=!%~1:~-2,2!
  ) ELSE (
    IF NOT DEFINED %~1 (
      ECHO. Looks like you need to review your code.) ELSE (
      SET "%~1=0!%~1!"
      SET "%~1=!%~1:~-2,2!
    )
  )
GOTO :EOF

:Convert_To_Number
  REM %1 -- Variable to Return
  REM %2 -- Value to Convert IF EMpty Will try to use !%1! instead!
  IF /I "%~2" NEQ "" (
    SET /A "%~1=1%~2 - (2%~2 - 1%~2)"
  ) ELSE (
    IF NOT DEFINED %~1 (
      ECHO. Looks like you need to review your code.) ELSE (
      SET /A "%~1=1!%~1! - (2!%~1! - 1!%~1!)"
    )
  )
GOTO :EOF

:Get-DateTime
    FOR /F "Tokens=1-7 delims=MTWFSmtwfsouehrandit:-\/. " %%A IN ("%DATE% %TIME: =0%") DO (
        FOR /F "Tokens=2-4 Skip=1 Delims=(-)" %%a IN ('ECHO.^| DATE') DO (
            SET "%%~a=%%~A"
            SET "%%~b=%%~B"
            SET "%%~c=%%~C"
            SET /a "#%%~a=1%%~A - (2%%~A-1%%~A)"
            SET /a "#%%~b=1%%~B - (2%%~B-1%%~B)"
            SET /a "#%%~c=1%%~C - (2%%~C-1%%~C)"
            SET "HH=%%~D"
            SET "Mn=%%~E"
            SET "SS=%%~F"
            SET "Ms=%%~G"
            SET /a "#HH=1%%~D - (2%%~D-1%%~D)"
            SET /a "#Mn=1%%~E - (2%%~E-1%%~E)"
            SET /a "#SS=1%%~F - (2%%~F-1%%~F)"
            SET /a "#Ms=1%%~G - (2%%~G-1%%~G)"
            SET /a "#TMinutes=((1%%~D - (2%%~D-1%%~D))*60)+(1%%~E - (2%%~E-1%%~E))"
            SET /a "#TSeconds=((((1%%~D - (2%%~D-1%%~D))*60)+(1%%~E - (2%%~E-1%%~E)))*60)+(1%%~F - (2%%~F-1%%~F))"
        )
    )
    SET "TTime=%HH%.%Mn%.%SS%"
    SET "IsoTime=%HH%.%Mn%.%SS%.%Ms%"
    SET "TDate=%yy%-%mm%-%dd%"
    SET "IsoDate=%yy%-%mm%-%dd%"
    SET "IsoDateTime=%yy%-%mm%-%dd%_%HH%.%Mn%.%SS%.%Ms%"
GOTO :EOF

结果如下:

C:\Users\Administrator>C:\Admin\S-O_Convert_Numbers.cmd
Initial Values:                        HH = "01"  -- MN = "06"  -- SS = "47"
After Convert_To_Leading_Zero Values:  HH = "01"  -- MN = "06"  -- SS = "47"
Convert_To_Number:                     HH = "1"  -- MN = "6"  -- SS = "47"
After Convert_To_Leading_Zero Values:  HH = "01"  -- MN = "06"  -- SS = "47"

C:\Users\Administrator>C:\Admin\S-O_Convert_Numbers.cmd
Initial Values:                        HH = "01"  -- MN = "08"  -- SS = "05"
After Convert_To_Leading_Zero Values:  HH = "01"  -- MN = "08"  -- SS = "05"
Convert_To_Number:                     HH = "1"  -- MN = "8"  -- SS = "5"
After Convert_To_Leading_Zero Values:  HH = "01"  -- MN = "08"  -- SS = "05"

C:\Users\Administrator>C:\Admin\S-O_Convert_Numbers.cmd
Initial Values:                        HH = "01"  -- MN = "08"  -- SS = "12"
After Convert_To_Leading_Zero Values:  HH = "01"  -- MN = "08"  -- SS = "12"
Convert_To_Number:                     HH = "1"  -- MN = "8"  -- SS = "12"
After Convert_To_Leading_Zero Values:  HH = "01"  -- MN = "08"  -- SS = "12"

这是我的最终代码,它之所以有效,是因为我只是在有空间的情况下一直应用填充。

简单问题的简单解决方案。

@echo off
title Stream counter
color a
cd /d "%~dp0"
set /a x=3
set nl=^&echo.
goto :top
:retry
cls
if %x% LEQ 0 (
    exit()
    ) else (
    title OOPS!
    echo Oops! You either didn't press anything or waited too long,
    echo if you're still there you can try again in a few seconds.
    ping 127.0.0.1 -n 10 >nul
    )
:top
title Stream counter
cls
echo Attemps left^: %x%
echo Start counter from zero? (y/n)
choice /n /c YNC /t 10 /d C>nul
if %errorlevel%==1 (
    goto :yes
    ) else if %errorlevel%==2 (
    goto :no
        ) else if %errorlevel%==3 (
        set /a x-=1
        goto :retry
        )
:yes
cls
set /a d=0
set /a h=0
set /a m=0
set /a s=0
goto :start
:no
cls
echo where do you want your timer to start?
echo Start by entering day^(s^)
set /p d=""
echo Now enter hour^(s^)
set /p h=""
echo Then enter minute^(s^)
set /p m=""
echo And finally second^(s^)
set /p s=""

goto :start
:start
rem delete this if you want to keep the result (it will get overwritten anyways...)
del final.txt
rem Use the choice to ask if you want to continue, and use that as a "Press any key to stop", kind of thing.
:startloop
::Check point "seconds"
:cps
if %s% geq 60 (
set /a s-=60
set /a m+=1
goto :startloop
)
::Check point "minutes"
:cpm
if %m% geq 60 (
set /a m-=60
set /a h+=1
goto :startloop
)
::Check point "hours"
:cph
if %h% geq 24 (
set /a h-=24
set /a d+=1
goto :startloop
) else (
goto :mainloop
)
:mainloop
cls
set ps=00%s%
set pm=00%m%
set ph=00%h%
set ps=%ps:~-2%
set pm=%pm:~-2%
set ph=%ph:~-2%
if %d% geq 1 (
    if %h% geq 1 set "output=%d% day^(s^) %ph%^:%pm%^:%ps%"
    if %h% equ 0 set "output=%d% day^(s^) %pm%^:%ps%"
) else if %h% geq 1 (
    set "output=%ph%^:%pm%^:%ps%"
) else (
    set "output=%pm%^:%ps%"
)
echo %output% > counter.txt
echo Current time^: %output% &echo.
echo Do you wish to abort? (Y/N)
choice /n /c yn /t 1 /d n >nul
if %errorlevel%==1 goto :end
set /a s+=1
::Check point "seconds"
:cps
if %s% geq 60 (
set /a s-=60
set /a m+=1
)
::Check point "minutes"
:cpm
if %m% geq 60 (
set /a m-=60
set /a h+=1
)
::Check point "hours"
:cph
if %h% geq 24 (
set /a h-=24
set /a d+=1
)
goto :mainloop
:end
:: END ::
rem COPY TO RESULT.txt and del counter.txt
copy /V counter.txt final.txt & del counter.txt
exit()