Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 898
A_Elric Avatar asked Oct 11 '12 19:10

A_Elric


People also ask

How do I list files in a batch file?

The listing of folder contents can be done with the dir command. This command allows you to see the available files and directories in the current directory. The dir command also shows the last modification date and time, as well as the file size.


2 Answers

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.

like image 78
Adriano Repetti Avatar answered Oct 28 '22 18:10

Adriano Repetti


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.

like image 41
SirOsis Avatar answered Oct 28 '22 19:10

SirOsis