问题
I have a number of .bat files that are opening a program and finding a specific instance that is ran everyday. This instance is timestamped and changes everyday. So I am having the .bat file run through everynumber looking for that instance. The format is YYYYMMDDHHMMSS.
From this site I already have a .bat file that finds the previous business day. I even have it looping through the numbers
The problem is if the number is less than 10, it needs to have a 0 in front of it:
01,02,03,04 etc...
The way the .bat file spits it out, though, is 1,2,3 etc...
Here is the code I have so far:
For /L %%G IN (3,1,9) DO (
For /L %%H IN (0,1,59) DO (
If %%H LSS 10 SET %%H=0%%H
ECHO %prevbusday%0%%G%%H
)
)
timeout /t 60
The first line loops through the hours (3 to 9)
The nested formula then loops through the minutes (0 to 59)
回答1:
You can't modify %%H
directly. You have to set "variable=%%H"
and use delayed expansion to retrieve !variable!
. As long as you're setting another variable anyway, just use variable substrings. Prepend a zero to everything, then use the rightmost two digits.
setlocal enabledelayedexpansion
For /L %%G IN (3,1,9) DO (
For /L %%H IN (0,1,59) DO (
set "h=0%%H"
ECHO %prevbusday%0%%G!h:~-2!
)
)
timeout /t 60
回答2:
Try enabling delayedexpansion then setting a new varialbe inside your inner for
loop:
@echo off
setlocal enabledelayedexpansion
For /L %%G IN (3,1,9) DO (
For /L %%H IN (0,1,59) DO (
If %%H LSS 10 (SET hours=0%%H) else (SET hours=%%H)
ECHO %prevbusday%0%%G!hours!
pause
)
)
timeout /t 60
Don't forget to use exclamation marks for your new varialbe within the inner for
loop
来源:https://stackoverflow.com/questions/30170213/bat-file-to-loop-through-numbers-need-to-add-0-to-numbers-less-than-10