5

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?

4 Answers 4

13

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.

2
  • That worked beautifully! I just need to remember to use an exclamation mark instead of a percent symbol. Thanks! Oct 27, 2014 at 18:15
  • That's brilliant. May 30, 2022 at 17:10
2

I would do this like that:

  • Add '0' to every number (so '01' but also '065')
  • Substring last 2 characters from this string

How do you substring in batch file (2 last chars):

SET string=abcd 
echo %string% 
SET string2=%string:~-2%
echo %string2%
1
  • 1
    Can you show a demo, preferably with how that works? I'm just getting the variable looped 65 times, with the number always 65. Oct 27, 2014 at 18:09
1

Nothing new to @dbenham's answer, just made a few of the values variables collected from the command line and putting up here for ease of copy and paste - credit is all @dbenham's above:

set first=%1
set last=%2
set len=%3
setlocal EnableDelayedExpansion
FOR /l %%N in (%first%, 1, %last%) do (
    set "n=0000000000000%%N"
    set "padded=!n:~-%len%!
    echo !padded!
)
0

Using ZtreeWin, navigate to the correct directory by pressing \, then press Enter to enter the directory where you can CtrlT to tag the files you wish to rename and then CtrlR to rename them to ????<0>*.*

For example:

foo-0-bar.jpg -> foo-00-bar.jpg
foo-1-bar.jpg -> foo-01-bar.jpg
foo-2-bar.jpg -> foo-02-bar.jpg
...

In the above example, ???? represents the first 4 characters (foo-), <0> inserts a zero in the 5th position, and *.* represents the remainder of the filename (-bar.jpg) which will remain unchanged.

Tagging your files:

enter image description here

Renaming your files:

enter image description here

ZTreeWin is a powerful 32bit & 64bit text-mode file & directory manager, modeled closely on the legendary XTreeGold(tm), but enhanced for today's popular operating systems.

3
  • Hi Acorn, welcome to Super User. The answer you posted is a low-quality one for what we typically accept. Can you expand it on how that program works, how you found it (or if you wrote it), the link, and possibly even a screen shot? It should be helpful to other visitors on the question as well. See How to Answer for more information. Thanks! Aug 23, 2016 at 16:41
  • Done - I now consider myself soundly spanked and the lesson learned. :) Aug 23, 2016 at 17:28
  • You're welcome. Thanks for adding the keycaps. :) Aug 24, 2016 at 17:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .