Get bare file names recursively in command prompt

19

5

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

TMRW

Posted 2011-06-07T15:59:37.317

Reputation: 974

Answers

15

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

Nik So

Posted 2011-06-07T15:59:37.317

Reputation: 529

Could you explain this? To me it makes little sense. – Simon Sheehan – 2011-11-08T23:27:26.433

4This 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. – Gaff – 2011-11-09T02:28:55.780

edited the answer; and just got my hands on a windows box again, I think this time should do it (win732bit) – Nik So – 2011-11-09T05:27:17.180

6

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 /?

Multiverse IT

Posted 2011-06-07T15:59:37.317

Reputation: 4 228

If you want the output sent to a file, append >> filename.txt – Gruber – 2019-10-01T14:26:23.233

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 – 2011-06-08T04:10:01.083

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 – 2011-11-08T22:17:32.570

2

for /r %i in (*) do @echo %~ni

or

forfiles /s /c "cmd /c if @isdir==FALSE noquotes.bat @fname"

assuming a file noquotes.bat in your %PATH% with this content

@echo %~1

for /r approach explained

for /r walks the current directory recursively (you can specify a directory for /r drive:\path\, the current directory is assumed) and executes the command specified by do for each file matched in the set (*). The set (.) would match only directories. @echo %~ni This command works as-is from the prompt. Double up on your quotes if you put it inside a batch file. i.e. for /r %%i in (*) do @echo %%~ni

forfiles approach explained

/s enumerates the current and all subdirectories
/c executes the command inside the quotes
@isdir and @fname is a symbol emitted into the command string
The extra batch file noquotes.bat helps by stripping the double-quotes with %~1 (parameter 1)
forfiles also allows you to specify a path to start at forfiles /P C:\Windows ...

JJS

Posted 2011-06-07T15:59:37.317

Reputation: 500

@DavidPostill yes, it was a poor answer. Thanks for your comments. Please let me know if I can improve my answer further. – JJS – 2016-07-26T23:46:50.007

@DavidPostill you have no previous answer or comment. were you moderating questions, or looking for an answer and stumble over my poor quality answer? – JJS – 2016-07-26T23:51:39.350

Your answer popped up in the "Low Quality Posts" review queue. As I know a lot about batch files I took a close at it. – DavidPostill – 2016-07-27T06:53:37.123

@DavidPostill thanks for your review. I welcome your review of the changes I made to my answer. – JJS – 2016-07-27T14:17:42.810

Much better ... :) – DavidPostill – 2016-07-27T14:20:26.493

-1

List all files, bare, recursively, using attributes (not directory) (files only)

dir /B /S /A:-D

C:\test\cache>dir /B /S /A:-D

C:\test\cache\7\0f\7b50ed0522645513da90345120eaf0f7 C:\test\cache\d\23\814644aa6a8195c91e54d2f7bb64e23d

Brandon Hunt

Posted 2011-06-07T15:59:37.317

Reputation: 1

The question says that the OP is “trying to get only the filenames (no extensions or file paths).  …  But when I [tried dir /b /s] to scan recursively, I also got file paths before filenames, which I do not want.” So, you are telling the OP to try a trivial variation on what they’ve already tried, and your answer demonstrates that it produces the same result as when the OP tried it (i.e., not what they want). – Scott – 2019-04-27T23:24:03.417