Batch Files: List file names and folder names

With cmd or .bat scripts, is there a way to list only file names and folder names? I would like to run a batch file to produce a text file that contains this information.

Suppose I have the following folder:

C:testtest.bat
C:testDoc1.exe
C:testDoc2.docx
C:testsubdir1Doc3.pdf
C:testsubdir2Doc4.txt

I’d love to have an output like this:

Doc1.exe
Doc2.docx
test.bat
subdir1
subdir2

So far, this answer is the closest I could get. With an output of:

Doc1     exe
DOC3~1   DOC
subdir1
subdir2
test     bat

I’ve tried dir:

dir /b /a-d >filelist.txt

But it didn’t get what I wanted. (i.e., it only list file names but not folder names).

Thank you!

Answer

Answer
Exclude the /a-d from your code and it should be exactly what you want it to do.

Also, to get the exact output format you specify, order the return by whether the item is a folder.

Explanation

https://ss64.com/nt/dir.html

/a is to specify an attribute
d specifies to look for directories, and the - is negating the d
so /a:-d means: where attribute is not directory and that parameter is taking away your directories from the list.

A simple dir /b will return all folders and files.

The parameter /o lets you organize the return. /o:g will list folders first in the list, and /o:-g will list them last.

Finally, if you ran dir /b /o:-g > filelist.txt your return would be

Doc1.exe
Doc2.docx
test.bat
subdir1
subdir2