Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

migrated from stackoverflow.com Oct 31 '12 at 6:07

This question came from our site for professional and enthusiast programmers.

    
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 '12 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 '12 at 5:05

5 Answers 5

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.

share|improve this answer
1  
I like the simplicity of this, but I tried it with 40k files and it crashed the explorer process. –  UpTheCreek Dec 11 '13 at 15:49

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

share|improve this answer
1  
powershell command is short and good working –  efr Oct 8 '13 at 3:22

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\
share|improve this answer

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" 
share|improve this answer
    
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 '12 at 5:06
    
If you don't want to write it in a script yes. OTOH ..... –  Preet Sangha Oct 31 '12 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 '13 at 3:44
    
yes it can put it intoo a batch file with %1 as folder name. stackoverflow.com/questions/6162415/… –  Preet Sangha Feb 14 '13 at 5:06
    
-Perhaps you can help with this superuser.com/q/552768/59747 –  MAW74656 Feb 15 '13 at 21:31

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 :)

share|improve this answer
    
What is Windows Commander? Do you mean Total Commander? –  UpTheCreek Dec 11 '13 at 15:51
    
Exactly :) Been using it for 10 years, name was Windows Commander back then. Edited out –  teejay Dec 11 '13 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 '13 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 '13 at 16:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.