-1

i need to rename pdf files so that the file name has a certain length. inpu file names will be 123456_1.pdf, 123_25.pdf and output should be 0000123456001.pdf, 000000013025.pdf. Before the _ the number should be filled till 10 characters with zeros and after the _ filled till 3 characters. the numbers before and after the _ are variabel. These are customer_account numbers. thanks in advance

Martin Schröder
  • 3,086
  • 3
  • 38
  • 69
  • Well newbie at this, i found different scripts which say how you can edit the first number of characters with the ???????_??? 000???00? but i couldn't find how to declare the numbers as variabel. the amount of zeros that need to placed are dependant on the input. Goal is to attempt a script when i have this info. – user3266251 Feb 03 '14 at 15:43

2 Answers2

1

It is important that only names that meet your starting template are renamed.

The DIR /B lists all pdf files, and the FINDSTR only keeps files that have all digits before and after the _. The IF statements make sure the first number does not already exceed 10 digits, and the second number doesn't already exceed 3 digits.

Finally, a classic technique is used to pad the numbers to a given length. Prefix each number with a string of zeros, then use a substring operation to preserve the correct amount of the right side.

@echo off
setlocal enableDelayedExpansion
for /f "tokens=1,2 delims=_." %%A in (
  'dir /b /a-d *.pdf^|findstr /rix "[0-9]*_[0-9]*\.pdf"'
) do (
  set "part1=%%A"
  set "part2=%%B"
  if "!part1:~0,10!" equ "!part1!" if "!part2:~0,3!" equ "!part2!" (
    set "part1=0000000000%%A"
    set "part2=000%%B"
    ren "%%A_%%B.pdf" "!part1:~-10!!part2:~-3!.pdf"
  )
)

Below is an entirely different method using REPL.BAT - a hybrid JScript/batch utility that performs a regular expression search and replace on stdin and writes the result to stdout. The utility is pure script that will run natively on any modern Windows machine from XP onward. Full documentation is built into the utility. REPL.BAT should be placed somewhere within your PATH.

The FOR IN() clause uses line continuation for readability. It works just as well as one continuous line.

The DIR /B simply lists all the pdf files.

The first REPL only matches names that consist of 1-10 digits, followed by _, followed by 1-3 digits, followed by .pdf. It reformats the name as follows: originalName:000000000FirstNumber_00SecondNumber.pdf.

The second REPL preserves the original name untouched, and then reformats the new name, preserving only the right most 10 digits of the first number and the right most 3 digits of the second number, plus the extension.

The FOR /F parses each result into the original name and the new name so that each file can be renamed.

@echo off
for /f "tokens=1,2 delims=:" %%A in (
  'dir /b *.pdf^|^
   repl "^([0-9]{1,10})_([0-9]{1,3})\.pdf$" "$&:000000000$1_00$2.pdf" ai^|^
   repl "(.*:).*(.{10})_.*(.{3}\.pdf)" "$1$2$3"'
) do ren "%%A" "%%B"
Community
  • 1
  • 1
dbenham
  • 119,153
  • 25
  • 226
  • 353
1

Try this:

@echo off
setlocal enabledelayedexpansion

pushd c:\temp
for /f "tokens=1,2 delims=_." %%a in ('dir /b *.pdf') do (
  set num1=0000000000%%a
  set num1=!num1:~-10!
  set num2=000%%b
  set num2=!num2:~-3!
  echo ren %%a_%%b.pdf !num1!!num2!.pdf
)

Remove the echo to do the actual rename.

Matt Williamson
  • 6,687
  • 1
  • 20
  • 33