#1 2025-06-20 03:26

ActingPrimeMinister
Member
Registered: 2025-06-20
Posts: 2

Serializing page count as a prefix, continuing from previous files

Let's say I have 400 pdfs, all of differing page count. I have added a number to the bottom right corner of each page of each file. For example, in the bottom right hand corner, File1 has 000001 on pg. 1, 000002 on pg. 2, and so on. For the sake of clarity, lets say these are the four files I need renamed along with the numbers that have been added to each page of each file.

File1 - 000001-000005
File2 - 000006-000027
File3 - 000028-000032
File4 - 000033-000103

Is there a way to build a script that will add each file's portion of the range as a prefix to the original file name? From the above example, the file names File1, File2, File3, File4 would become

000001-000005 File1
000006-000027 File2
000028-000032 File3
000033-000103 File4

You can see how long this would take to do manually for 400 files. I can't simply use the pagecount, because each prefix needs to carry over from the end of the file immediately before it alphabetically. If you can help me out on this, you could save me many hours per week and I would be eternally thankful.

Thanks!

Offline

#2 2025-06-20 14:19

den4b
Administrator
From: den4b.com
Registered: 2006-04-06
Posts: 3,487

Re: Serializing page count as a prefix, continuing from previous files

To paraphrase it slightly, you want to serialise your files with a cumulative page count range. Correct?

Yes, that's possible, using the Pascal Script rule with the help from a 3rd party utility for extracting PDF page counts.

First, download Xpdf command line tools: https://www.xpdfreader.com/download.html

You'll just need the "pdfinfo.exe" tool from the downloaded archive. You can install this tool anywhere you like, even into ReNamer's folder.

Then, use this script in a Pascal Script rule:

const
  PdfInfoExe = 'pdfinfo.exe';
  Pattern = '[\n\r]Pages:\s+(\d+)\b';
  PadNumbersToLength = 6;

function ExtractPdfPageCount(const Path: WideString): Integer;
var
  Command: WideString;
  Output: String;
  Matches: TWideStringArray;
begin
  Result := 0;
  Output := '';
  Command := PdfInfoExe + ' "' + Path + '"';
  if ExecConsoleApp(UTF8Encode(Command), Output) = 0 then
  begin
    Matches := SubMatchesRegEx(UTF8Decode(Output), Pattern, False);
    if Length(Matches) > 0 then
      Result := StrToInt(Matches[0]);
  end;
end;

function PadZeros(const Number: Integer; MaxLength: Integer): String;
begin
  Result := IntToStr(Number);
  while Length(Result) < MaxLength do
    Result := '0' + Result;
end;

var
  Initialized: Boolean;
  TotalPageCount, PageCount: Integer;

begin
  if not Initialized then
  begin
    Initialized := True;
    TotalPageCount := 0;
  end;
  PageCount := ExtractPdfPageCount(FilePath);
  if PageCount > 0 then
  begin
    FileName :=
      PadZeros(TotalPageCount + 1, PadNumbersToLength) + '-' +
      PadZeros(TotalPageCount + PageCount, PadNumbersToLength) + ' ' +
      FileName;
    TotalPageCount := TotalPageCount + PageCount;
  end;
end.

You may need to adjust the "PdfInfoExe" constant to point it to the correct location of the "pdfinfo.exe" tool.

If that helps, consider supporting the development by purchasing a Pro license.

Offline

#3 2025-06-20 17:19

ActingPrimeMinister
Member
Registered: 2025-06-20
Posts: 2

Re: Serializing page count as a prefix, continuing from previous files

Thanks so much. Unfortunately I am not able to install this third party software. Is there no way to pull page count data without third party software? That seems almost too shortsighted to be true, but I suppose Windows has done worse things before.

Offline

#4 2025-06-21 00:03

den4b
Administrator
From: den4b.com
Registered: 2006-04-06
Posts: 3,487

Re: Serializing page count as a prefix, continuing from previous files

ActingPrimeMinister wrote:

Unfortunately I am not able to install this third party software.

There is no installation required. It is simply a ZIP archive.

You can unpack it anywhere you like, even on your on Desktop, and then update the path in the script accordingly.

Offline

Board footer

Powered by FluxBB