Batchfile: If file exist move it to subdirectory

mailivres

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

bgaleas hint helped to prevent the loop:

for /f %%A in ('dir *.pdf /b /s') do [...]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Cannot move file to subdirectory of itself

From Dev

Cannot move file to subdirectory of itself

From Dev

Find file , create subdirectory and move

From Dev

How to move some file to a subdirectory?

From Dev

Renaming a file with mv: "cannot move to a subdirectory of itself"

From Dev

Move file and rename it if exist in folder

From Dev

How to File.AppendAllText create subdirectory if doesn't exist?

From Dev

Move specific file from all subdirectories and rename based on subdirectory

From Dev

Move files to parent directory, prefixing file name with former subdirectory name

From Dev

How to move a file from a subdirectory to another subdirectory located outside the first one?

From Dev

mv: Move file only if destination does not exist

From Dev

Move website to subdirectory

From Dev

Move pages to subdirectory in Wordpress

From Dev

the system cannot find the file specified in batchfile

From Dev

Windows Batch File: Look for directory, if not exist, create, then move file to it

From Dev

Windows Batch File: Look for directory, if not exist, create, then move file to it

From Dev

Move all files in a directory in a subdirectory

From Dev

Directory with same name subdirectory move

From Dev

Create and Move Phone Module into a subdirectory

From Dev

move or copy a directory to a subdirectory of itself

From Dev

Move all files in certain subdirectory

From Dev

How to move the only file in every subdirectory to outside and change the name to the directory name?

From Dev

Copy a file into a subdirectory and rename it

From Java

Import a file from a subdirectory?

From Dev

.gitignore not working for file in subdirectory

From Dev

Create a file in every subdirectory

From Dev

Relocate file into subdirectory

From Dev

Rewrite to file if that exists in subdirectory

From Dev

Foreach python file in subdirectory

Related Related

  1. 1

    Cannot move file to subdirectory of itself

  2. 2

    Cannot move file to subdirectory of itself

  3. 3

    Find file , create subdirectory and move

  4. 4

    How to move some file to a subdirectory?

  5. 5

    Renaming a file with mv: "cannot move to a subdirectory of itself"

  6. 6

    Move file and rename it if exist in folder

  7. 7

    How to File.AppendAllText create subdirectory if doesn't exist?

  8. 8

    Move specific file from all subdirectories and rename based on subdirectory

  9. 9

    Move files to parent directory, prefixing file name with former subdirectory name

  10. 10

    How to move a file from a subdirectory to another subdirectory located outside the first one?

  11. 11

    mv: Move file only if destination does not exist

  12. 12

    Move website to subdirectory

  13. 13

    Move pages to subdirectory in Wordpress

  14. 14

    the system cannot find the file specified in batchfile

  15. 15

    Windows Batch File: Look for directory, if not exist, create, then move file to it

  16. 16

    Windows Batch File: Look for directory, if not exist, create, then move file to it

  17. 17

    Move all files in a directory in a subdirectory

  18. 18

    Directory with same name subdirectory move

  19. 19

    Create and Move Phone Module into a subdirectory

  20. 20

    move or copy a directory to a subdirectory of itself

  21. 21

    Move all files in certain subdirectory

  22. 22

    How to move the only file in every subdirectory to outside and change the name to the directory name?

  23. 23

    Copy a file into a subdirectory and rename it

  24. 24

    Import a file from a subdirectory?

  25. 25

    .gitignore not working for file in subdirectory

  26. 26

    Create a file in every subdirectory

  27. 27

    Relocate file into subdirectory

  28. 28

    Rewrite to file if that exists in subdirectory

  29. 29

    Foreach python file in subdirectory

HotTag

Archive