In my scenario my file names all start with something like this Q1111_CONFIRM, Q1111_ORDER, Q2222_CONFIRM, Q2222_ORDER. How would I modify this one to combine the order/confirm matching the Qnumber? I tried a few different things with no success.
I know I need to change something in this line:
for /F "tokens=1* delims=_" %%a in ('dir /B *_KM_*.*') do (
but I cannot work out what to change it to in order for it to work in my scenario. Would someone be able to help me?
Code: Select all
@echo off
setlocal EnableDelayedExpansion
rem Initialize (delete) "lastFile" and "fileList" variables
set "lastFile="
set "fileList="
rem Next line get the output of a "dir /B" command, that show file names *only*
rem "for /F" command execute the dir, get the output and divide each line in two "tokens" ("%%a" and "%%b")
rem with the first part before the "_" in "%%a" and the *rest* (including further "_") in "%%b"
for /F "tokens=1* delims=_" %%a in ('dir /B *_KM_*.*') do (
rem If the base file name changed...
if "%%a" neq "!lastFile!" (
rem Process previous file list;
rem this "if" is just to avoid process the empty list the first time
if defined fileList (
pdftk !fileList! output !lastFile!.pdf
)
rem Reinitialize the new list
set "lastFile=%%a"
set "fileList=%%a_%%b"
) else (
rem Append this file to current list
set "fileList=!fileList! %%a_%%b"
)
)