Windows – Copy multiple files from different folders to 1 folder

copycopyingrobocopywindowsxcopy

i'm looking for a sollution to copy multiple files from different folders to 1 folder.

What I have is that I need to copy files from

c:\Customers\folderA\folderB\files

copy to e:\FolderB\files

The problem is that the "Customers" everytime is different. Also FolderA is different. Only FolderB is the same.

I've tried it with robocopy or with copy. But I always have to fill in the Customers name.

Can some one help me?

So I tried it into powershell

and i came to

Copy-Item -Path C:\customer -Recurse -filter *.xls -Destination e:\folderB -Force

only with this i filter to files and all the folders al copied. And i only want the files in it.

Best Answer

You can use the FOR /D command to loop through the directories in a path:

FOR /D %%I IN (C:\Customers\*) DO (
REM %%I is "C:\Customers\FolderA", etc.
    robocopy.exe /E "%%I\FolderB\files" "C:\FolderB\files"
)

Let's assume the directory C:\Customers contains:

  • A. Datum Corporation
  • AdventureWorks Cycles
  • Alpine Ski House
  • Awesome Computers
  • Baldwin Museum of Science
  • Blue Yonder Airlines

If we run this simple command script:

FOR /D %%I IN (C:\Customers\*) DO (
    ECHO %%I
)

We get this output:

C:\Customers\A. Datum Corporation
C:\Customers\AdventureWorks Cycles
C:\Customers\Alpine Ski House
C:\Customers\Awesome Computers
C:\Customers\Baldwin Museum of Science
C:\Customers\Blue Yonder Airlines

So taking that a step further, this command script:

FOR /D %%I IN (C:\Customers\*) DO (
    robocopy.exe /E "%%I\FolderB\files" "C:\FolderB\files"
)

Would run the following commands in sequence:

robocopy.exe /E "C:\Customers\A. Datum Corporation" "C:\FolderB\files"
robocopy.exe /E "C:\Customers\AdventureWorks Cycles" "C:\FolderB\files"
robocopy.exe /E "C:\Customers\Alpine Ski House" "C:\FolderB\files"
robocopy.exe /E "C:\Customers\Awesome Computers" "C:\FolderB\files"
robocopy.exe /E "C:\Customers\Baldwin Museum of Science" "C:\FolderB\files"
robocopy.exe /E "C:\Customers\Blue Yonder Airlines" "C:\FolderB\files"

The output, of course, would be the result of each robocopy command.