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
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With