-1

I am writing a little Batch skript for deleting first page of every pdf file in the directory and subdirectories. The problem is, that I don't know how to use the filename variable.

setlocal enabledelayedexpansion

    pause

    FOR /R %%i IN (*.pdf) DO (
        set fileName=%%~nxi
        echo %fileName%
        cd /d %%i
        ren %%i test.pdf
        pdftk test.pdf cat 2-end output %fileName%
        rm test
        cd D:\Daten

    )

    pause

the output of this script is as followed

set fileName=test.pdf
echo
cd \d D:\Daten\Scanned_but_not_ready\Bewerbung_CV\test.pdf
ren D:\Daten\Scanned_but_not_ready\Bewerbung_CV\test.pdf test.pdf
pdftk test.pdf cat 2-end output
rm test
cd D:\Daten

I have already tried everything I know, with %% as well as with single %.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • Your initial code problem was a delayed expansion problem. But you soon realized that you did not need to assign the `FOR` variable to an environmental variable. – Squashman Jun 22 '18 at 13:22

1 Answers1

1

Found now a solution by myself... for everybody who needs it...

setlocal enabledelayedexpansion

pause

FOR /R %%i IN (*.pdf) DO (
    echo %%~nxi
    ren %%i test.pdf
    pdftk %%~dpitest.pdf cat 2-end output %%~dpi%%~nxi
    del %%~dpitest.pdf
    cd D:\Daten
)

pause
  • You should always double quotes pathes possibly containing spaces. `%%~dpi%%~nxi` is the same as `%%~fi` but default `%%i` should already contain the fullpath from the `for /r` Nevertheless good job for a batch newbie +1 –  Jun 22 '18 at 09:13