A_Elric
A_Elric

Reputation: 3568

Batch file to list all files in a dir in windows 7

I'm trying to list all the files in a directory and then write that to a text file using a batch file. I think it should be something like

dir / (some flags here) >> files.txt

which will contain a listing like

a.exe
b.exe
c.exe

etc. etc

Upvotes: 6

Views: 36836

Answers (4)

Adriano Repetti
Adriano Repetti

Reputation: 67070

You have to use the /b switch (to write file list only, without header and other informations).

dir /b >> list.txt

Then if you need to list all .exe files in c:\windows the full command is:

dir c:\windows\*.exe /b >> list.txt

Please note that >> will append the list to the file. Do not forget you can use multiple search patterns, for example:

dir *.jpg;*.png /b > list.txt

It'll write all the .jpg and .png files (only names, without path or any other informations) to the file list.txt (overwriting the file if it did exist before).

If you need to exclude directories you can rely on /a switch (to include/exclude items according to an attribute). In your case you want to exclude directories then you have to use -d:

dir /b /a-d >> list.txt

Finally do not forget dir can be used recursively with /s switch (to list files that match search pattern in the given directory and in all sub-directories) and sorted with /o option. Use dir /? for more details about that.

Upvotes: 7

Andre
Andre

Reputation: 1

you can also use tree for recursive list files in directory tree

and for dumping to text file you can use this:

C:\tree "D:\my directory" > "D:\dump file.txt"

Upvotes: 0

SirOsis
SirOsis

Reputation: 64

You pretty much have it down already. if you are only wanting to list the files in that directory then

dir /A-D >> somefile.txt

OR

dir /A-D > somefile.txt    

dir /
  • >> appends
  • > creates new or overwrites existing

    dir /A-D /ON > somefile.txt

This will sort by name.

Upvotes: 3

Bali C
Bali C

Reputation: 31231

Here you go

dir C:\ /s /b >files.txt

Upvotes: 2

Related Questions