How to copy files from folder tree dropping all the folders with Robocopy?
Asked Answered
S

3

34

I have the following folder structure:

FolderA
--Folder1
--Folder2
--Folder3
...
--Folder99

Folders 1 through 99 have files in them.

All I want to do is to copy ALL THE FILES into ONE FOLDER, basically do a FolderA copy, and wipe out Folders 1-99 keeping all the files.

I'd like to do it with Robocopy from cmd.exe if possible (Windows Server 2008)

Sizeable answered 1/10, 2009 at 6:15 Comment(3)
This doesn't seem like a programming question.Hers
you guys can move it to SU or SF, i don't want to delete it and waste a useful answerSizeable
batch programming is still programmingTwopiece
B
51

Why use robocopy? It's a good tool for a specific task but this is not the one.

You can simply use what cmd already gives you:

for /r %f in (*) do @copy "%f" target

This will essentially "flatten" your directory hierarchy. for /r will walk a directory tree recursively, looking for file names matching the given pattern. You can also specify the directory to start in:

for /r FolderA %f in (*) do @copy "%f" target

Within the loop it's just a simply copy of the file into a specified folder.

Bevel answered 1/10, 2009 at 6:21 Comment(3)
There are 2 solutions here if you have files that don't have unique names.Lyrebird
Which option can be used when there are duplicates with the same name?Thumbprint
@Martin: See help copy. It's /y.Bevel
D
3

Robocopy is a great tool... when you have a job it can handle. Why not use xcopy?

If you have two drives you can just use xcopy:

XCOPY  C:\*.*  D:\NewFolder\   /S

Or use XXCOPY for one drive:

XXCOPY C:\*.*  C:\NewFolder\   /S /CCY

XXCOPY

Disinfect answered 1/10, 2009 at 6:28 Comment(2)
user /SGF attribute to copy into one folderFederalist
xcopy duplicates the directory structure. I think the question is about flattening the directory structure.Sunfast
C
3
Get-ChildItem -Path source -Recurse -File | Move-Item -Destination dest
Crosspatch answered 5/10, 2017 at 8:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.