I take as an example the question that I found interesting as an exercise: Merge pdf files with related filenames
In the question I tried to answer with a partial success because it has updated the question with more information.
To summarize the problem is a folder containing:
123456_ABCD.pdf
123456_EFGH.pdf
123456_IJKL.pdf
111111_ABCD.pdf
111111_EFGH.pdf
222222_IJKL.pdf
222222_WXYZ.pdf
And in a command FOR
I wanted to get output like this:
123456_ABCD.pdf, 123456_EFGH.pdf, 123456_IJKL.pdf
111111_ABCD.pdf, 111111_EFGH.pdf
222222_IJKL.pdf, 222222_WXYZ.pdf
Each line here are supposed representing the same prefix found in the command for
Here's what I tried:
@echo off
(
copy nul 123456_ABCD.pdf
copy nul 123456_EFGH.pdf
copy nul 123456_IJKL.pdf
copy nul 111111_ABCD.pdf
copy nul 111111_EFGH.pdf
copy nul 222222_IJKL.pdf
copy nul 222222_WXYZ.pdf
)
setlocal enabledelayedexpansion
:: set _pdffiles=
set _prevfiles=
for /f "delims=" %%i in ('dir /b /a-d /o:n "??????_????.pdf"') do (
set "files=%%nxi"
if "!files:~0,6!" neq "!_prevfiles:~0,6!" (
set "_prevfiles=%%i"
set _pdffiles=!_pdffiles! "%%i"
set "_outputpdf=%%~ni"
) else (
set _prevfiles=
set _pdffiles=
)
echo pdftk.exe !_pdffiles! cat output "!_outputpdf:~0,6!.pdf"
)
But that give me that output:
pdftk.exe "111111_ABCD.pdf" cat output "111111.pdf"
pdftk.exe "111111_ABCD.pdf" "111111_EFGH.pdf" cat output "111111.pdf"
pdftk.exe "111111_ABCD.pdf" "111111_EFGH.pdf" "123456_ABCD.pdf" cat output "123456.pdf"
pdftk.exe "111111_ABCD.pdf" "111111_EFGH.pdf" "123456_ABCD.pdf" "123456_EFGH.pdf" cat output "123456.pdf"
pdftk.exe "111111_ABCD.pdf" "111111_EFGH.pdf" "123456_ABCD.pdf" "123456_EFGH.pdf" "123456_IJKL.pdf" cat output "123456.pdf"
pdftk.exe "111111_ABCD.pdf" "111111_EFGH.pdf" "123456_ABCD.pdf" "123456_EFGH.pdf" "123456_IJKL.pdf" "222222_IJKL.pdf" cat output "222222.pdf"
pdftk.exe "111111_ABCD.pdf" "111111_EFGH.pdf" "123456_ABCD.pdf" "123456_EFGH.pdf" "123456_IJKL.pdf" "222222_IJKL.pdf" "222222_WXYZ.pdf" cat output "222222.pdf"
I searched for similar examples but I have not found how to avoid this.
ps: If somebody can find a great title suggestion please :)
If I understand you right, you have to execute
pdftk.exe
only at end of a group.EDIT: Furthermore I have added
~
inset files
, negate yourif
-condition and distinguish between names and paths. Now it looks like you want.