Home
Run a sumatrapdf-tool command on every PDF in a folder with a for loop in the Command Prompt (cmd.exe) or in PowerShell.
Available in pre-release 3.7
Open a terminal in the folder with the PDFs and run a loop. At a glance:
  • Command Prompt: for %f in (*.pdf) do sumatrapdf-tool ... "%f"
  • .bat file: the same, with %%f instead of %f.
  • PowerShell: Get-ChildItem *.pdf | ForEach-Object { sumatrapdf-tool ... $_.FullName }
  • Search: grep takes many files in one command.
The examples use sumatrapdf-tool, which the installer adds to PATH. SumatraPDF.exe takes the same arguments.

Write a loop in the Command Prompt #

for %f in (*.pdf) do @sumatrapdf-tool clean "%f" "fixed\%~nf.pdf"
  • %f is the full file name, e.g. annual report.pdf.
  • %~nf is the name without the extension, e.g. annual report. Use it to build output names.
  • Put quotes around "%f" and around output names so names with spaces work.
  • @ stops cmd from printing each command before it runs it.
  • for /r %f in (*.pdf) do ... also goes into subfolders; %f is then the full path.
Write the results to a separate folder (create it first with mkdir fixed). That keeps them apart from the originals, and running the loop again won’t process them.

Write a loop in a .bat file #

In a .bat file, double every %: %%f, %%~nf, and %%d for the page number in image names.
@echo off
mkdir text
for %%f in (*.pdf) do sumatrapdf-tool convert -o "text\%%~nf.txt" "%%f"
Note: a single %d in a .bat file is eaten by cmd. convert then gets broken arguments and prints its usage text.

Write a loop in PowerShell #

mkdir fixed
Get-ChildItem *.pdf | ForEach-Object { sumatrapdf-tool clean $_.FullName "fixed\$($_.BaseName).pdf" }
  • $_.FullName is the full path of the file; $_.BaseName is the name without the extension.
  • Use $(...) to put $_.BaseName inside a string.
  • Names with spaces work without extra quotes.
  • Use Get-ChildItem -Recurse *.pdf to include subfolders.
This works the same in PowerShell 7 (pwsh) and in Windows PowerShell 5.1.

Compress all PDFs #

Command Prompt:
mkdir small
for %f in (*.pdf) do @sumatrapdf-tool clean -gggg -e 100 -f -i -t -Z "%f" "small\%~nf.pdf"
PowerShell:
mkdir small
Get-ChildItem *.pdf | ForEach-Object { sumatrapdf-tool clean -gggg -e 100 -f -i -t -Z $_.FullName "small\$($_.BaseName).pdf" }
A file that is already compressed may not get smaller, and a very small one can even grow a little. See Compress a PDF.

Extract the text of all PDFs #

Each PDF becomes a .txt file with the same name.
Command Prompt:
mkdir text
for %f in (*.pdf) do @sumatrapdf-tool convert -o "text\%~nf.txt" "%f"
PowerShell:
mkdir text
Get-ChildItem *.pdf | ForEach-Object { sumatrapdf-tool convert -o "text\$($_.BaseName).txt" $_.FullName }
See Extract text from PDF.

Convert all PDFs to PNG images #

One image per page, named <file>-<page>.png, e.g. annual report-1.png, annual report-2.png. resolution=150 sets the image resolution in dots per inch.
Command Prompt:
mkdir png
for %f in (*.pdf) do @sumatrapdf-tool convert -o "png\%~nf-%d.png" -O resolution=150 "%f"
In a .bat file:
mkdir png
for %%f in (*.pdf) do sumatrapdf-tool convert -o "png\%%~nf-%%d.png" -O resolution=150 "%%f"
PowerShell:
mkdir png
Get-ChildItem *.pdf | ForEach-Object { sumatrapdf-tool convert -o "png\$($_.BaseName)-%d.png" -O resolution=150 $_.FullName }
See Convert PDF to images.

Search all PDFs #

grep takes many files, so you don’t always need a loop. -i ignores case, -H prints the file name and -n the page number:
sumatrapdf-tool grep -i -H -n "green" "annual report.pdf" invoice.pdf
Each match is one line: file name, page number and the line of text, separated by tabs:
annual report.pdf	1	Green text on page 1
annual report.pdf	2	Green text on page 2
annual report.pdf	3	Green text on page 3
invoice.pdf	1	Green text on page 1
invoice.pdf	2	Green text on page 2
invoice.pdf	3	Green text on page 3
grep doesn’t expand *.pdf itself: sumatrapdf-tool grep -i foo *.pdf fails with cannot open *.pdf. Let the shell list the files instead.
PowerShell, all PDFs in one command:
sumatrapdf-tool grep -i -H -n "green" (Get-ChildItem *.pdf).Name
PowerShell, including subfolders:
sumatrapdf-tool grep -i -H -n "green" (Get-ChildItem -Recurse *.pdf).FullName
Command Prompt, one file at a time (for /r to include subfolders):
for %f in (*.pdf) do @sumatrapdf-tool grep -i -H -n "green" "%f"
See sumatrapdf-tool grep for regular expressions and other options.

Tips #

  • Try the command on one file first, then put it in a loop.
  • Use -H with grep so you know which file each match comes from.
  • Use a .bat file for a job you repeat; remember to double the %.
  • Other tools work the same way: grayscale, repair, PDF info.

See also #