Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I ran into a little snag trying to get only the filenames (no extensions or file paths) recursively.This worked for me in the root folder:

dir /b

But when i added /s to scan recursively i also got file paths before filenames which i do not want. Is there a way to get bare filenames from all subfolders in a directory?

Im on Windows 7 x64 I'd rather use regular command prompt not PS or VBS

share|improve this question

Use the following command:

dir /b /a /s
  • /b strips the date and other details from the output
  • /a only outputs the filename, no paths
  • /s enables a recursive directory listing

If you need to save the output to a file, you can use:

dir /b /a /s >> list_of_names.txt

EDIT Actually the above solution doesn't reach the original question's goals. One thing I did notice from the question is that the post asks for recursive listing. which the other answer lacks so I think adding "/s" in the other answerer's answer will do the trick

for /f %a in ('dir /b /s') do @echo %~na
share|improve this answer
Could you explain this? To me it makes little sense. – Simon Sheehan Nov 8 '11 at 23:27
This doesn't work, /a doesn't work like that - it's a filter for which type of files you want to list. It doesn't strip the path from the filename when doing a recursive directory listing. – 8088 Nov 9 '11 at 2:28
edited the answer; and just got my hands on a windows box again, I think this time should do it (win732bit) – Nik So Nov 9 '11 at 5:27

Try this:

for /f "delims=" %a in ('dir /b /s') do @echo %~na

More information on how for works and what it's doing, type for /?

share|improve this answer
This almost works but there's a big problem.It only outputs the first word of a filename.If a filename is for example "my vacation image 1" then it only outputs "my" – TMRW Jun 8 '11 at 4:10
try this: for /f "delims=|" %a in ('dir /b') do @echo %~na. By default the /f parameter of FOR will tokenize on spaces. Setting the token to a character that shouldn't appear in file/directory names will give you the entire name including spaces. – Scott McKinney Nov 8 '11 at 22:17

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.