@echo off
setlocal
cd ..
if exist *.pdf(
for /f "tokens=*" %%a in ('dir /b *.pdf') do call :append %%a
)
echo pdfList: %pdfList%
goto :eof
:append
if defined pdfList (
set pdfList=%pdfList% %1
) else (
set pdfList=%1
)
This will add the list of PDF filenames separated by spaces to the pdfList variable. If you want to include the PDF filenames in all subdirectories as well, then change the dir command in the for statement to:
dir /s /b *.pdf
But using this will have a side-effect that a list of absolute paths would be added to the pdfList variable, and not relative paths as you expressed in your question.
The first if statement ensures that we execute the dir command only if PDF files are present. We don't want to invoke DIR command if they are not present; otherwise dir would print 'File not found' error.
The :append subroutine is necessary because if we try to append to the pdfList variable in the for statement itself, we'll find that the updated value of pdfList in one iteration doesn't survive till the next iteration.
The if statement in the :append subroutine is to make sure that we do not have a leading space in the value of pdfList.