having trouble with padding zeros in a for /f

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
kzerafa87
Posts: 2
Joined: 24 Feb 2018 22:24

having trouble with padding zeros in a for /f

#1 Post by kzerafa87 » 24 Feb 2018 22:40

I'm having issues getting this to pad leading zeros
so i have a list of *.pdf named 1.pdf - 999.pdf in a folder. inside JobIDs.txt is the file name needed for each pdf in order line 1 for 1.pdf , 2 for 2.pdf so on so on
i get a new set of pdf files everyday 7 days a week issue I'm having is when i dir the directory it will list
1
10
11
13
then 100s
2
20s
200s

so i need to rename my file from NUMBER.pdf to "PADDEDNUMBER - %%a.pdf"
example line from JobIDs.txt

ERIE - 31102 - 191786 - 8305 MAPLE TRACT RD

so input would be 1.pdf to 001 - ERIE - 31102 - 191786 - 8305 MAPLE TRACT RD.pdf


setlocal EnableDelayedExpansion
set count=0
set /a "num=001"
@echo off
for /f "tokens=* delims= " %%a in (JobIDs.txt) do (
echo Renaming !count!.pdf to !count! - %%a.pdf
set /a count+=1

echo num: %num%
set "num=1%num%"
set /a num=num+1
set /a num=%num:~1%
echo num: %num%
REM "This works outside of for /f but not inside"


REM This works with out padding
REM ren !count!.pdf "!count! - %%a.pdf"

echo ren !count!.pdf "%num% - %%a.pdf"
Rem Does not work
pause
)
echo Done

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: having trouble with padding zeros in a for /f

#2 Post by penpen » 25 Feb 2018 03:56

Your approach mainly doesn't work, becuase you are using the normal expansion instead of the delayed one.
Padding to three digits in your example could be done this way:

Code: Select all

set "paddedCount=000!count!"
set "paddedCount=!paddedCount:~-3!"
penpen

pieh-ejdsch
Posts: 240
Joined: 04 Mar 2014 11:14
Location: germany

Re: having trouble with padding zeros in a for /f

#3 Post by pieh-ejdsch » 25 Feb 2018 04:36

this should look like this in the command line.

Code: Select all

< JobIDs.txt cmd /v /c "set "x=" & (for /l %i in (1001 1 1999) do @ set /p "x=" && >nul set /a "padNr=%i,nr=padNr%1000" && echo ren "!Nr!.pdf" "!padNr:~1! - !x!") "

kzerafa87
Posts: 2
Joined: 24 Feb 2018 22:24

Re: having trouble with padding zeros in a for /f

#4 Post by kzerafa87 » 25 Feb 2018 10:54

Thank you ill give these a try when i get back home

Post Reply