1

I hardly work with batch files and I need a batchfile which moves PDF files to a new subdirectory "PDF" in the current context path.

For example my directory tree looks like this:

A/a.xml
A/b.xml
A/x.pdf
A/AA/a.xml
A/AA/y.pdf
B/z.pdf

Desired tree after batch processing:

A/a.xml
A/b.xml
A/PDF/x.pdf
A/AA/a.xml
A/AA/PDF/y.pdf
B/PDF/z.pdf

My first try looks like this:

@ECHO OFF
FOR /r %%a IN (*.pdf) DO (
    MKDIR "%%~pa"/pdf
    MOVE %%a "%%~pa"/pdf
)

But this creates a loop because the batch file also process all moved PDF files. Any help appreciated. Thank you!

mailivres
  • 100
  • 1
  • 2
  • 8
  • 1
    Use dir in a for command `for /f %%A in ('dir /b /s') do echo %%A`. This lists the files before they are moved. See `for /?` and see how you can get parts of the filename eg `%%~dpA` if you just want the folder path. –  Feb 15 '16 at 11:15
  • You might be interested in [this thread](http://stackoverflow.com/q/31975093/5047996) which shows how `for` actually behaves in situations where the enumerated directory is changed during being processed... – aschipfl Feb 15 '16 at 23:59

1 Answers1

1

bgaleas hint helped to prevent the loop:

for /f %%A in ('dir *.pdf /b /s') do [...]
mailivres
  • 100
  • 1
  • 2
  • 8