I have multiple folders s1,s2.....sn on main directory. Each of these folders have multiple sub folders, folders within folders and then files. Structure kind of looks like:
mainDir
-s1
-f1
-c1
-a.txt
-b.txt
-c2
-m.txt
-n.txt
-f2
-c1
-aaa.txt
-bbb.txt
-c2
-mmm.txt
-nnn.txt
-s2
-f1
-c1
-a.txt
-b.txt
-c2
-m.txt
-n.txt
-f2
-c1
-aaa.txt
-bbb.txt
-c2
-mmm.txt
-nnn.txt
-s3 ..................
Now we need to get rid of all subfolders and keep only . the first level folders and all the underlying files. So final destination should like:
DestDirectory
-s1
-a.txt
-b.txt
-m.txt
-n.txt
-aaa.txt
-bbb.txt
.....so on
-s2
-a.txt
-b.txt
-m.txt
-n.txt
-aaa.txt
-bbb.txt
.....so on
The first thing you need to do is to find out if there would be any filename collisions occur when moving files from multiple directories into a single directory.
=== Find-DupeFilenames.ps1
If you need to run this from a cmd.exe shell prompt, use this to get a quick count.
Use this to list all conflicted filenames.
How is this any different from your Previous question? How to copy files from sub folder only using cmd?
Perhaps in your previous Q you had all same-named folders?
The Code below should also work for the differently named folders as you described here.
CMD Script:
I would use a
for /D
loop to iterate the first-level sub-directories, then a nestedfor /R
loop to iterate through all files in the immediate sub-directories, then usecopy
to copy each file individually in order to achieve the flat destination directory hierarchy. To avoid file name collisions (meaning that files with the same name appear in different source sub-directory hierarchy levels), anif exist
directive can be used. So here is the code: