Given a folder with several subdirectories, I want to move their contents to the top folder and remove the subdirectories. How can I do that on the Windows command line?

E.g. I have a folder named "Jack" in D:. It contains several subfolders and each subdirectory contains additional files (text files, PDF files, etc.), but no directories. I want to move all subfolders' files into "Jack", and remove the then empty subdirectories. If duplicate file found do replace or do nothing, I need only one copy I need to do this repeatedly, so a batch solution would be best.

  • Welcome to Super User! Please take a moment to read the FAQ to learn how the site works. – Daniel Beck Oct 9 '12 at 5:18
  • 4
    How do you want to deal with naming collisions? Will there be D:\Jack\A\1.pdf and D:\Jack\B\1.pdf? – Daniel Beck Oct 9 '12 at 5:18
  • thanks, I am using these at office, File names are different, If same filename found just replace, Thanks in advnce Mr. Beck – Nikhil Oct 9 '12 at 6:39
  • "If same filename found just replace"... to what? – Siva Charan Oct 9 '12 at 6:51
  • 1
    You can use xcopy to do this. Type xcopy /? at the command prompt for usage information. – martineau Oct 9 '12 at 7:17

Put this in a batch script.

for /f "delims==" %%k in ('dir D:\Jack\*.* /s /b') do copy "%%k" D:\Jack\ /Y

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.