Flatten directory structure
Emily Wong
I have a bunch folders within folders. Like this
Root Folder -- Folder 1 -- Folder 2 -- Folder 3 -- Folder 4I want to collapse all folders so that all files under folder1, folder 2, folder 3, ... folder n exist under root folder.
What is the quickest way to do this in windows?
14 Answers
There's no reason to use special tools or even scripting. Just use the search function in Explorer.
- Open the folder you wish to flatten in Explorer.
- Create a new folder, select all files (but not folders), and drag them to that folder (This will avoid getting two copies of those files.)
- In the search box (the one on the right of the address bar), type in
*and press Enter. - After the list populates, right click and select all, then right click and Cut.
- Press the back button to get back to the original folder, then right-click and paste.
- If you have any duplicate names, you will be prompted on how to deal with this. Choose the most appropriate option for your use case. (I would assume it would be to copy both files.) To avoid being prompted again, be sure to apply this to all files with the checkbox at the bottom.
- Select all folders, make sure they are empty by looking at the popup about the number of files selected, and then delete them.
If you would rather have a script so you can just start it and forget it, that can also be done. The command to replace mv *\* . is pretty easy (for /r %f in (*.*) do @move "%~f" .) but I'm sure you'd like to handle duplicates, without leaving them behind as in @Nicole Hamilton's answer. Appending a number to the filename of duplicates is probably the easiest way to do it.
Copy and paste the following batch file into Notepad and save it as "flatten.cmd" (including the quotation marks):
@echo off
cd "%~1"
md tempryfolder
move *.* tempryfolder
for /r %%f in ("*.*") do call :START "%%~f"
for /r /d %%f in (*.*) do rd "%%~f"
GOTO :EOF
:START
setlocal
If exist "%~n1%~x1" set /a num=2
:LOOP
If exist "%~n1%num%%~x1" ( set /a num+=1 goto loop
)
move "%~1" "%~n1%num%%~x1"To use the script, simply drag the folder you wish to flatten on top of it.
11You can flatten in Windows Explorer by navigating to the folder you wish to flatten and searching for System.Kind:<>folder to exclude folders from the results.
You can then select all the files with CTRL+A and copy and paste them into a new folder. Note that Windows Explorer will ask you how to deal with duplicate filenames.
4This kind of thing is easy with a Unix shell. Here's what you could do with my own Hamilton C shell (including the free version.) You could do something very similar with Cygwin bash.
mv *\* .If your directory is really huge, it's possible that could wildcard into too long a command line for Windows to pass to a child process. (The Windows CreateProcess call limits the argument list to 32K Unicode characters.) But the C shell has no limit internally, so you could iterate over the whole list this way:
foreach i ( *\* ) mv $i .
endTo @MarkAllen's point above, this kind of assumes there are no duplicate names. If there are, the later copies in the list will overwrite the older ones. If that's a concern, you could iterate over the files, mv'ing them only if you're not overwriting. The :t operator is the tail operator. The -e operator is the file exists test.
foreach i ( *\* ) if ( -e $i:t ) then echo -2 $i not moved because it already exists else mv $i . end
endThis still leaves the empty directories, which maybe you don't want after they've been emptied. That could be the next iteration if it mattered.
4i m using this one - in order to reduce my folders
2