FOR loop group each iteration with same prefix in

2019-08-07 01:39发布

问题:

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 :)

回答1:

echo off
setlocal EnableDelayedExpansion

set "list="
for /F "tokens=1,2 delims=_" %%a in ('dir /B *.pdf') do (
   if "!last!" neq "%%a" (
      if defined list echo pdftk.exe !list:~2!
      set "list="
      set "last=%%a"
   )
   set "list=!list!, %%a_%%b"
)
echo pdftk.exe !list:~2!


回答2:

If I understand you right, you have to execute pdftk.exe only at end of a group.

EDIT: Furthermore I have added ~ in set files, negate your if-condition and distinguish between names and paths. Now it looks like you want.

set name=
set file=
set pdfFiles=
set prevFile=
for /f "delims=" %%i in ('dir /b /s /a-d /o:n "??????_????.pdf"') do (
    set prevName=!name!
    set prevFile=!file!
    set name=%%~nxi
    set file="%%i"
    if "!prevName!" == "" set prevName=!name!
    if "!name:~0,6!" == "!prevName:~0,6!" (
        set pdfFiles=!pdfFiles! !file!
        set _outputpdf=%%~ni
    ) else (
        echo pdftk.exe !pdfFiles! cat output "!_outputpdf:~0,6!.pdf"
        set pdfFiles=!file!
    )
) 
echo pdftk.exe %pdfFiles% cat output "%_outputpdf:~0,6%.pdf"