19

For example, say that I have the following folder hierarchy:

Folder1
   File1
   Folder2
      File2
   Folder3
      File3
      Folder4
         File4

I want to perform some command that results in:

Folder1
   File1
   File2
   File3
   File4

or something similar. I'm not very familiar with Windows, so I would appreciate as much detail as possible in the answer.

3
  • Are you looking for an existing program to do this for you, or a piece of code (what programming language) that does this?
    – Matt Ball
    Oct 31, 2012 at 2:06
  • @MattBall: I'm looking for the easiest possible way of doing this. Ideally, this would be opening up a terminal and inputting some command. Again, I'm not very experienced with Windows, so please let me know if there is a better way. Thanks.
    – abw333
    Oct 31, 2012 at 5:05
  • Possible duplicate of Easiest way to extract the contents of many folders at once? Feb 9, 2018 at 13:30

7 Answers 7

37

The absolute easiest way is to enter the common root folder and do a search for all files (i.e. search for *). When all files are found, mark all files, press Ctrl + X and navigate to the common root folder again. Now press Ctrl + V to paste all the files into the root folder. When finished, delete all subdirectories.

I do not know if this can be done as a batch job.

3
  • 5
    I like the simplicity of this, but I tried it with 40k files and it crashed the explorer process.
    – UpTheCreek
    Dec 11, 2013 at 15:49
  • The total commander answer seems easier to me.
    – Ev0oD
    Mar 17, 2017 at 11:28
  • May or may not apply to OPs problem exactly, but great example of also leveraging the "dumb" tools in our toolbox :)
    – jgangso
    Jul 8, 2020 at 7:54
19

I used this powershell approach in the end when I needed to flatten a large hierarchical structure (in my case pngs) :

Get-ChildItem C:\sourcefolder -Recurse -Filter "*.png" | Copy-Item -destination C:\destinationfolder\
1
  • 2
    For flattening maybe Move-Item is better suited? Oct 16, 2017 at 18:19
4

windows exe: http://en.sourceforge.jp/projects/sfnet_flatfolder/

or AHK:

fileselectfolder,MyFold,::{20d04fe0-3aea-1069-a2d8-08002b30309d}
SetWorkingDir, %MyFold%
loop, *.*,0,1
{
  parentpath := RegExReplace(A_LoopFileDir,"\\","-")
  ;StringReplace, parentpath, A_LoopFileDir, \,-,All
  newname = %parentpath%-%A_LoopFileName%
  ;msgbox %newname%
  If a_loopfiledir <>
    filemove, %a_loopfilefullpath%,%newname%
}

loop, %myfold%\*.*,2,1
  fileremovedir, %a_loopfilefullpath%,1

exitapp

or use Directory Opus

or Powershell

(ls -r -include *.jpg) | % { mv -literal $_ $_.Name.Insert(0, [String]::Format("{0} - ", $_.Directory.Name))}

or Batch (as mentioned above)

or the manual search, cut and paste as mentioned above

There are many ways, depending on your skill and inclinations you can choose any of these, and refine according to your needs.

You might need this Remove Empty Directories after the above operation

1
  • 1
    powershell command is short and good working Oct 8, 2013 at 3:22
4

If you only need to flatten dir manually from time to time, Total Commander is perfect.

In Total Commander: Goto to your dir you want to flatten. In menu click Commands > Branch View. And you see it all flattened, you can move/copy it to another folder.

If you need to tinker a bit with filenames: Select all files in flattened view, in menu go to Files > Multi-Rename Tool. Here you can add some info from the path to actual filenames using "Rename mask", you just need to use plugin tag/button there, search & replace feature and the little foolder button, which lets you to make some last edits to filenames in text editor. May sound complicated, but really the easiest way IMO :)

5
  • What is Windows Commander? Do you mean Total Commander?
    – UpTheCreek
    Dec 11, 2013 at 15:51
  • Exactly :) Been using it for 10 years, name was Windows Commander back then. Edited out
    – teejay
    Dec 11, 2013 at 15:59
  • Thanks. Just tried it. Wouldn't work for for 40k small files though :( consumed a lot of CPU for 10 mins then came up with repeated 'couldnt create file' errors.
    – UpTheCreek
    Dec 11, 2013 at 16:11
  • CPU usage and implied slowness is unfortunate but understandable:) The error is something else.. might be bad naming, total commander bug or just TC's inability to handle this many files. I would try renaming smaller number of files first or use newer version of TC
    – teejay
    Dec 11, 2013 at 16:17
  • @teejay, I had to fish for the details of your suggestion, but I finally managed to make it. I used the expander2 plugin to extract parts of the path and make them the prefix of the file name. You might want to add some details for the next guy. Tx.
    – killogre
    Mar 5, 2015 at 9:51
4

Pretty simple with a command-line option for those not super technically inclined.

  1. Create a file somewhere called "flatten.cmd"
  2. Open that file in Notepad
  3. In the file, place the following:

    FOR /R {SourcePath} %%G IN (*.mp3) DO move "%%G" {Destination}
    
  4. Replace "{SourcePath}" with the folder you want to flatten. In your case "c:\Folder1"

  5. Replace "{Destination"} with the folder you want the files moved to. In your case also "c:\Folder1". The code should now look like:

    FOR /R c:\Folder1 %%G IN (*.mp3) DO move "%%G" c:\Folder1
    
  6. Open up a command line window. Can do this a number of ways, but this is fast:

    • Click the Start button
    • Click All Programs
    • Click Accessories
    • Click Command Prompt
  7. In the command line window, type:

    cd {folder where you put the flatten.cmd file}
    flatten
    

And that will do it (Windows 8+) I just did that to flatten a folder of 10,000 music files. Works like a charm.

You can find the options for the FOR command-line utility at https://technet.microsoft.com/en-us/library/bb490909.aspx.

1

using a batch script (off the top of my head):

Look up the FOR command

The first line moves all the files from the subdirectories up to the root The second delete the the sub folders

for /f %f in ('dir "c:\folder\*" /s/b/a-d') do if not %~ff"=="c:\folder" move "%f" "c:\folder"
for /f %f in ('dir "c:\folder\*" /s/b/ad') do if not "%~ff"=="c:\folder" rd /s/q "%f" 
5
  • so I have to open up a terminal and input these commands, making sure that I replace 'folder' with the appropriate path? Thanks.
    – abw333
    Oct 31, 2012 at 5:06
  • If you don't want to write it in a script yes. OTOH ..... Oct 31, 2012 at 6:24
  • Can this be generalized and attached to a context menu action? I'd love to be able to right-click on a file, click "Flatten File", then have all the files within it moved up to the folder's level. Deleting the folder after would be nice too.
    – MAW74656
    Feb 14, 2013 at 3:44
  • yes it can put it intoo a batch file with %1 as folder name. stackoverflow.com/questions/6162415/… Feb 14, 2013 at 5:06
  • -Perhaps you can help with this superuser.com/q/552768/59747
    – MAW74656
    Feb 15, 2013 at 21:31
1

Hi you can also use xxcopy (www.xxcopy.com) tool, its xcopy on steriods (sort-of). With this tool you can "flatten" the files in folders into one folder.

c:> xxcopy /source-folders /flatten /SG

Just read this link: http://www.xxcopy.com/xxcopy16.htm

Cheers.

You must log in to answer this question.