Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a folder (let's call it A) with lots of subfolders B, C, D, E, which then again have subfolders B1, B2, B3, ... C1, C2, C3 ...

How can I using cmd move all files from all subfolders into the "root" folder A?

share|improve this question
I am assuming you want every file in the one root, not in subfolders? So A\File1, A\File2, not A\Folder2\File1? – Canadian Luke Oct 18 '11 at 19:45
@Luke - Yes, you got it. Every file that exists in folders and subfolders moved to root. – Rook Oct 18 '11 at 20:45
up vote 12 down vote accepted

My original post neglected to include the file's extension in the move command. %~ni only returns the basename, you need to use %~nxi to get both the filename and extension! I hope you haven't mangled your filenames on my account!(

From a Command Prompt:

cd /d C:\Path\To\A
for /r %i in (*) do @move "%i" "%~nxi"

If you need to cleanup the empty folders afterward:

for /d %i in (*) do @rd /s /q "%i"

Now, if you have any filename clashes, you're on your own. You can add the /y parameter to the move command (before the "%i") to force overwrites. If you want something different, you'll need another program that can auto-rename everything for you.

Also, if you want to use this in a batch file, change all the % to %%.

share|improve this answer
Didn't know about FOR /r -- nice! – Daniel Beck Oct 18 '11 at 20:29
Wow, very awesome. I bow to your command-fu. – Mokubai Oct 18 '11 at 21:23

Create a folder and copy all the other folders into it, then right click that folder and select Search. In the search box enter *.*.

This will list all the files and folders. Select all the files you want in your root directory and cut them. Navigate to your root directory and paste all the files there.

share|improve this answer
search doesn't exist as a command in my cmd. – Rook Nov 12 '12 at 15:12
Richard wasn't talking about a a command. Have edited his answer to make it clearer. – Isaac Rabinovitch Nov 13 '12 at 4:44

Install Cygwin (or another Unix environment for Windows) and run the following:

cd A
find . -type f -exec mv {} . \;
share|improve this answer
Depending on your mv and find variants, you might need to use find . -type f -exec mv -t . {} \; instead. – Daniel Beck Oct 18 '11 at 19:57
For native Windows ports of many Unix tools I like – Dennis Oct 18 '11 at 19:58
@Dennis UnxUtils are nice, unfortunately quite old by now. If you're using them at the moment, could you verify my post works with them? mkdir -p a/a1 a/a2 a/a3 b/b1 b/b2 b/b3 c and touch a/a1/foo a/a1/bar a/a2/baz a/a3/qux b/b1/quux b/b2/quuux b/b3/quuuux c/ccc for preparation. – Daniel Beck Oct 18 '11 at 20:08
-1 That may be true, but I was inquiring about how to do it in cmd. – Rook Oct 18 '11 at 20:43
@Daniel No doubt they are old, I have been dragging them around for years. I just tried find . -type f -exec mv {} . ; And it seemed to work ok. – Dennis Oct 18 '11 at 20:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .