Jake
Jake

Reputation: 25

Creating a perl script to batch process files

I have this line of code:

convert 1234_Page_1_....png 1234_Page_2_....png output.pdf 

This merges those particular pngs to a single pdf (using ImageMagick). I have a bunch of files in this format. I would like to perform this merging/converting-to-pdf action on files that have the same number before the "Page". Sometimes there are more than two pages to convert.

I would like to have this done in a perl script that I can run on Windows.

Thanks in advance, Jake

Upvotes: 1

Views: 1416

Answers (3)

pilcrow
pilcrow

Reputation: 58534

If you wanted to call convert(1) as few times as necessary:

#! /usr/bin/perl

use strict;
use warnings;

my %processed = ();
for my $prefix (map { /^(\d+)/ } glob('[1-9]*_Page_*.png')) {
    next if $processed{$prefix}++;
    system("convert ${prefix}_Page_*.png ${prefix}_output.pdf");
}

Upvotes: 2

Blender
Blender

Reputation: 298156

Can't you use an asterisk for this? I will try it right now.

convert 1234_Page_*.png output.pdf

If you want the readable file, here you go:

import os

for i in xrange(int(raw_input('How many sets of pages are there? '))):
  os.system('convert {0}_Page_*.png output_{0}.pdf'.format(str(i)))

Here's a one-liner:

python -c "import os; [os.system('convert {0}_Page_*.png output_{0}.pdf'.format(str(i))) for i in xrange(int(raw_input('How many sets of pages are there? ')))]"

Upvotes: 0

sarnold
sarnold

Reputation: 104050

If you have cygwin (maybe mingw too?) installed, try this:

for i in `seq 1234 1350` ; do convert ${i}_Page_*.png ${i}_output.pdf ; done

Upvotes: 1

Related Questions