I need all subfolders in a directory tree to be flattened to one layer of folders, with all the files intact inside the folder. for example:

inside parent folder: -folderA -folderB -file1 -file2 -file3 -folderC -file1 -file2 -file3

to this:

inside parent folder: folderA -file1 -file2 -file3 folderB -file1 -file2 -file3 folderC

I can do this manually by showing the folder path and expanding everything, then dragging and dropping one folder at a time into the parent folder. The problem is, I have 25,000+ folders to go through and a command to do this will save considerable man hours and speed up the project. I have seen lots of questions asking all files to be pulled into a parent folder but I need them separated from each other and still in their respective folders for auditing purposes as each file is tagged with the folder name it was stored in initially.

  • Please note that superuser.com is not a free script/code writing service. If you tell us what you have tried so far (include the scripts/code you are already using) and where you are stuck then we can try to help with specific problems. You should also read How do I ask a good question?. – DavidPostill Oct 29 '16 at 8:57

An easy example PowerShell script could look like this:

Get-ChildItem -Directory -Recurse -Path . | %{
    Move-Item $_ C:\Path\To\Parent

This would not watch out for name conflicts and I'm not sure what the default behaviour would be. With 25.000 folders it's pretty likely you'll run into duplicate names.

This also assumes your graph is wrong. As your initial suggestion means you'd flatten it after moving every file one layer up.

  • Perhaps I was not clear enough in my example. It was meant to show folderB and 3 files inside folderA, and folderC and 3 files inside folderB with folderC not containing any files. There are many duplicates, I suspect probably in the hundreds. I did a sample of 300 folders and 67 of them were named "old" or "OLD" or "Old" which would all be caught as the same thing I imagine. – Angelo M Oct 28 '16 at 17:49
  • You could make your first listing clearer by switching the files and folders around. Right now I'm reading the first section as folderA containing the directory and file 1-3. If you have that many duplicates you will have to think of a way to handles those. E.g. you get suffix or prefix them with something. – Seth Oct 28 '16 at 18:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.