Use Dir Command for List of Filenames Only

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
johnbil
Posts: 11
Joined: 06 Jul 2013 09:01

Use Dir Command for List of Filenames Only

#1 Post by johnbil » 11 Sep 2016 20:36

I need to make a list of all files, all types, in a folder and its subfolders. I don't want the folder and subfolder names to appear in the list, just the file names. Does any one know if this can be done and if so, what command options I should use? :?:

Regards...John

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Use Dir Command for List of Filenames Only

#2 Post by ShadowThief » 11 Sep 2016 22:24

Code: Select all

for /f "delims=" %A in ('dir /b /S') do echo %~nxA


Obviously, if you're doing this from a script instead of the command line, use %% instead of %.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Use Dir Command for List of Filenames Only

#3 Post by Compo » 12 Sep 2016 10:38

Just a correction for the above solution, (which lists folder names too)!

Code: Select all

For /F "Delims=" %A In ('Dir/B/S/A-D') Do @Echo=%~nxA

…and a few alternatives.

Code: Select all

For /R "%UserProfile%\Desktop" %I In (*) Do @Echo=%~nxI
three not using the Dir command

Code: Select all

ForFiles /P "%UserProfile%\Desktop" /S

Code: Select all

For /F "Delims=" %I In ('Where /R "%UserProfile%\Desktop" *.* 2^>Nul') Do @Echo=%~nxI

Code: Select all

For /F "Delims=" %I In ('RoboCopy /L /S /NS /NC /NP /NDL /NJH /NJS "%UserProfile%\Desktop" null *.*') Do @Echo=%~nxI

johnbil
Posts: 11
Joined: 06 Jul 2013 09:01

Re: Use Dir Command for List of Filenames Only

#4 Post by johnbil » 12 Sep 2016 11:59

Thanks for the replies. Forgive the newbie question, but where can I download the 'For' command? :?

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Use Dir Command for List of Filenames Only

#5 Post by ShadowThief » 12 Sep 2016 12:05

johnbil wrote:Thanks for the replies. Forgive the newbie question, but where can I download the 'For' command? :?

it's built into batch.

johnbil
Posts: 11
Joined: 06 Jul 2013 09:01

Re: Use Dir Command for List of Filenames Only

#6 Post by johnbil » 13 Sep 2016 00:25

Not sure, but don't I need a For.exe/For.com file somewhere on my Windows 7 system? I keep getting a 'file not found' error message.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Use Dir Command for List of Filenames Only

#7 Post by ShadowThief » 13 Sep 2016 00:37

For is a command, it isn't a program. It's impossible for it to not be on your system, since it's part of your system.

What are you doing with the list of files? If you're just displaying them, there's no reason for you to be getting that error message. If you're doing things with a file that's in a subdirectory and you aren't in that subdirectory, the file isn't found because it's looking for a file with that name in the current directory instead of the directory where the file actually is.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Use Dir Command for List of Filenames Only

#8 Post by Squashman » 13 Sep 2016 09:23

johnbil wrote:Not sure, but don't I need a For.exe/For.com file somewhere on my Windows 7 system? I keep getting a 'file not found' error message.

It is an internal cmd. Just like CD, CALL, COPY, DEL, ETC.....

Open up a cmd prompt and type:

Code: Select all

for /?

Post Reply