How can I zero-pad numbers in a Batch FOR loop?

Canadian Luke

I am writing a batch file to do some operations on remote computers in my building. They are all named with the same prefix, but the number at the end changes (01–65). I tried to follow an online tutorial for creating a batch file loop based on numbers, and came up with this:

FOR /l %%N in (1,1,65) do (
    set HOSTNAMETOUSE=prefix-%%N
    ECHO %HOSTNAMETOUSE%
)

This works great after it hits 10, as then it's two digits. What I need is for the numbers 1-9 to appear as 01-09, as that matches our naming schema. Essentially, I need the single-digit of a leading zero for the first 9 iterations. How can I accomplish this with Windows Batch Files?

dbenham

Your posted code cannot possibly work because %HOSTNAMETOUSE% is expanded when the statement is parsed, and the entire parenthesized block is parsed in one pass, before the loop is executed. So the expanded value is constant for all iterations.

You must use delayed expansion to get the value upon execution time.

@echo off
setlocal enableDelayedExpansion
FOR /l %%N in (1,1,65) do (
    set HOSTNAMETOUSE=prefix-%%N
    ECHO !HOSTNAMETOUSE!
)

Now to prefix with 0 as needed.

Since you never need more than a single 0, you could use an IF statement.

@echo off
setlocal enableDelayedExpansion
FOR /l %%N in (1,1,65) do (
    if %%N lss 10 (set HOSTNAMETOUSE=prefix-0%%N) else set HOSTNAMETOUSE=prefix-%%N
    ECHO !HOSTNAMETOUSE!
)

But that is not convenient if your number width is more than 2 digits. A more general solution is to add a string of n zeros with your desired length, and then use a substring operation to keep the n right most digits.

@echo off
setlocal enableDelayedExpansion
FOR /l %%N in (1,1,65) do (
    set "n=00%%N"
    set "HOSTNAMETOUSE=!n:~-2!
    ECHO !HOSTNAMETOUSE!
)

You can easily extend this solution to virtually any number of leading zeros.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How can i pad the edges of a rectangle with points?

분류에서Dev

How can I run a batch file with a loop as a scheduled task?

분류에서Dev

How can I check in Apex if a batch is scheduled?

분류에서Dev

How can I call a batch file within another batch file?

분류에서Dev

How can I use vector for entering numbers?

분류에서Dev

How do I Successfully Echo a For Loop Into a Batch Script?

분류에서Dev

How can I break a symbolic link loop?

분류에서Dev

How can I iterate through a function with for loop?

분류에서Dev

How can I resolve this loop in c#?

분류에서Dev

How can i resolve the path laocation in batch file

분류에서Dev

how can I remove white space in a string and replace it with (;) in batch

분류에서Dev

How can I extract text before a character or string in a batch file?

분류에서Dev

how to store zero leading numbers in mongoDB

분류에서Dev

With regular expressions how can I get just the numbers inside brackets?

분류에서Dev

How can I remove the decimal parts from numbers in Kotlin?

분류에서Dev

How can I encode and decode an Array of Numbers in Objective-C?

분류에서Dev

How can I hide the line numbers in Notepad++?

분류에서Dev

How can i store 2 numbers in a 1 byte char?

분류에서Dev

How can I pause a loop until I get a users input?

분류에서Dev

how can i loop for store items i want in ExtJS XTemplate?

분류에서Dev

Nested IFs to pad numbers with zeroes

분류에서Dev

How I can I start a powershell command line from a batch file and enforce it to stay open?

분류에서Dev

How can i make the lose label to appear once the timer hits zero?

분류에서Dev

Python: How can I read csv and clean my data in a loop

분류에서Dev

How can I get time efficent by creating an array with for loop?

분류에서Dev

How can I store the count of a for loop with promises that return in an arbitrary order?

분류에서Dev

How can I loop this script from 1 to 50 in SPSS?

분류에서Dev

How Can I Pull An Entire Result From A Foreach Loop

분류에서Dev

How can i make infinite loop with timeout function

Related 관련 기사

  1. 1

    How can i pad the edges of a rectangle with points?

  2. 2

    How can I run a batch file with a loop as a scheduled task?

  3. 3

    How can I check in Apex if a batch is scheduled?

  4. 4

    How can I call a batch file within another batch file?

  5. 5

    How can I use vector for entering numbers?

  6. 6

    How do I Successfully Echo a For Loop Into a Batch Script?

  7. 7

    How can I break a symbolic link loop?

  8. 8

    How can I iterate through a function with for loop?

  9. 9

    How can I resolve this loop in c#?

  10. 10

    How can i resolve the path laocation in batch file

  11. 11

    how can I remove white space in a string and replace it with (;) in batch

  12. 12

    How can I extract text before a character or string in a batch file?

  13. 13

    how to store zero leading numbers in mongoDB

  14. 14

    With regular expressions how can I get just the numbers inside brackets?

  15. 15

    How can I remove the decimal parts from numbers in Kotlin?

  16. 16

    How can I encode and decode an Array of Numbers in Objective-C?

  17. 17

    How can I hide the line numbers in Notepad++?

  18. 18

    How can i store 2 numbers in a 1 byte char?

  19. 19

    How can I pause a loop until I get a users input?

  20. 20

    how can i loop for store items i want in ExtJS XTemplate?

  21. 21

    Nested IFs to pad numbers with zeroes

  22. 22

    How I can I start a powershell command line from a batch file and enforce it to stay open?

  23. 23

    How can i make the lose label to appear once the timer hits zero?

  24. 24

    Python: How can I read csv and clean my data in a loop

  25. 25

    How can I get time efficent by creating an array with for loop?

  26. 26

    How can I store the count of a for loop with promises that return in an arbitrary order?

  27. 27

    How can I loop this script from 1 to 50 in SPSS?

  28. 28

    How Can I Pull An Entire Result From A Foreach Loop

  29. 29

    How can i make infinite loop with timeout function

뜨겁다태그

보관