Let's say I have the following Tree:

│   │   hippopotamus.txt
│   │   lion.txt
│   │
│   └───dog
│           poodle.txt
│           terrier.txt
    │       apple.txt
    │       orange.txt

What command would I use to create a directory that contains all of the .txt files, but without the subfolders, essentially 'flattening' the tree.

  • What operating system? What "command line" are you using? What have you tried so far? The tag "command line" denotes a way of working and on its own is not enough to tell us what you are using.
    – Mokubai
    May 10, 2017 at 16:39
  • @Mokubai CMD, as in the title, refers to windows CMD.exe.
    – tuskiomi
    May 10, 2017 at 16:42
  • That's not clear enough. Next time, please add a corresponding OS tag.
    – slhck
    May 10, 2017 at 16:57
  • @slhck >Command Prompt, also known as cmd.exe or cmd (after its executable file name), is the command-line interpreter on Windows . From: en.wikipedia.org/wiki/Cmd.exe
    – tuskiomi
    May 10, 2017 at 17:05
  • 1
    @tuskiomi Tags enable users to quickly find questions in which they are interested. Adding an OS-specific tag improves the visibility of your question to those answering and future users asking your same question. Thanks for contributing to Super User. May 10, 2017 at 18:51

2 Answers 2



Assuming you have a target directory in mind (C:\Target) and all of the .txt files are under the C:\Tree directory, the following command would get a list of all of the .txt files and copy them to your desired destination:

for /F "delims=" %a in ('dir /s /b "C:\Tree\*.txt" ') do (copy "%a" "C:\Target")

I put double-quotes around parameters that may contain spaces.


for /F

Performs a for loop parsing text. By default, tokens will be split on spaces into variables %a, %b, %c, etc. Since we do not want this behavior, I specified:


Which means no delimiters. %a will be the variable containing each line of text from the output of our command. The command I used was:

dir /s /b "C:\Tree\*.txt"

This performs a directory listing of all files matching *.txt under the C:\Tree folder. The /s flag performs recursively, searching inside of all subdirectories/subfolders. The /b flag outputs the listing in a "bare" format, which basically just lists the file and path. The output looks like:


And of course:

copy "%a" "C:\Target"

Copies file signified by the %a variable to the C:\Target directory. The for loop basically executes the following commands:

copy "C:\Tree\animals\hippopotamus.txt" "C:\Target"
copy "C:\Tree\animals\lion.txt" "C:\Target"
copy "C:\Tree\animals\dog\poodle.txt" "C:\Target"
copy "C:\Tree\animals\dog\terrier.txt" "C:\Target"
copy "C:\Tree\food\fruit\apple.txt" "C:\Target"
copy "C:\Tree\food\fruit\orange.txt" "C:\Target"
copy "C:\Tree\food\vegetables\borcolli.txt" "C:\Target"
copy "C:\Tree\food\vegetables\carrot.txt" "C:\Target"
copy "C:\Tree\food\vegetables\corn.txt" "C:\Target"

For more help on DIR options or FOR loops in Windows Command Prompt (CMD), you can type help dir or help for. Beware, the FOR loop help page is quite lengthy. ;)

  • Please let me know if this doesn't quite answer your question. If it does, please consider accepting this as an answer. :)
    – Damian T.
    May 10, 2017 at 17:10
  • This is definitely the right answer. Just throwing it out there though, this is one of those things I like doing in PowerShell because it just feels "cleaner"... example: get-childitem -recurse -include *.txt | copy-item -destination C:\Target
    – BrianC
    May 10, 2017 at 22:32

A PowerShell solution:

mkdir C:\AllTxt ; gci C:\Install *.txt -Recurse | copy-item -Destination C:\AllTxt -Force 
  • mkdircreates a new directory, like C:\AllTxt
  • gci gets all txt-Files in C:\install (the top directory of your tree structure) recursively
  • copy-item will copy the files to the destination

You must log in to answer this question.

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