Richard Prado
Richard Prado

Reputation: 13

Copy file from multiple folder to a new folder - files are with the same name

The thing is that I have pdf.pdf file in multiple folders, and I need to copy all those files to the same folder, but is keeping replacing. The solution will be to copy the files with the folder's name E.g. foldername1.pdf foldername2.pdf

but I dont know how to do it

the script I using is this

for /r "BOOK\" %%i in (*.pdf) do copy "%%i" "BOOK\"

My file structure is like this

C:\BOOK\FOLDERNAME1\PDF.PDF
C:\BOOK\FOLDERNAME2\PDF.PDF
C:\BOOK\FOLDERNAME3\PDF.PDF
C:\BOOK\FOLDERNAME4\PDF.PDF
C:\BOOK\FOLDERNAME5\PDF.PDF

I would appreciate any help, Thanks

Upvotes: 1

Views: 85

Answers (1)

user6811411
user6811411

Reputation:

Inserting another for to extract the found files parent directory name.

:: Q:\Test\2018\07\27\SO_51560631.cmd
@Echo off
Set "Base=C:\BOOK\"
For /r "%Base%" %%A in (*.pdf
  ) Do For %%B in ("%%~fA\.."
    ) Do Copy "%%~fA" "%Base%%%~nxB%%~xA"  

Sample tree after running the batch:

> tree /F
└───BOOK
    │   FOLDERNAME1.PDF
    │   FOLDERNAME2.PDF
    │   FOLDERNAME3.PDF
    │   FOLDERNAME4.PDF
    │   FOLDERNAME5.PDF
    │
    ├───FOLDERNAME1
    │       PDF.PDF
    │
    ├───FOLDERNAME2
    │       PDF.PDF
    │
    ├───FOLDERNAME3
    │       PDF.PDF
    │
    ├───FOLDERNAME4
    │       PDF.PDF
    │
    └───FOLDERNAME5
            PDF.PDF

Upvotes: 1

Related Questions