List of files on HDD

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
JohnC
Posts: 4
Joined: 14 Jul 2018 16:25

List of files on HDD

#1 Post by JohnC » 14 Jul 2018 22:14

Hi,
Can anyone help in showing me a command line to extract a simple recursive list of all files and extensions but no path.

Ive tried dir /s/b/a-d > j.txt but that still gives me the path as well

thanks

John

penpen
Expert
Posts: 1723
Joined: 23 Jun 2013 06:15
Location: Germany

Re: List of files on HDD

#2 Post by penpen » 15 Jul 2018 05:31

If i understand your task right, then the following might help you.
Command line sample:

Code: Select all

@(>"j.txt" (for /f "tokens=*" %a in ('dir /s/b/a-d ') do @echo(%~nxa))
Batch file example:

Code: Select all

@echo off
>"j.txt" (for /f "tokens=*" %%a in ('dir /s/b/a-d ') do @echo(%%~nxa)
goto :eof
penpen

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

Re: List of files on HDD

#3 Post by Compo » 15 Jul 2018 12:29

Using ForFiles, whilst not the most efficient command, is a simple command to use, (Enter ForFiles /? at the Command Prompt for usage information).

Code: Select all

ForFiles /S /C "Cmd /C If @IsDir==FALSE Echo @File">"j.txt"

JohnC
Posts: 4
Joined: 14 Jul 2018 16:25

Re: List of files on HDD

#4 Post by JohnC » 17 Jul 2018 22:49

Thanks for your suggestions. I now have what I was looking for. Apologies for not responding sooner - I was expecting an email notification for replies...cheers John

JohnC
Posts: 4
Joined: 14 Jul 2018 16:25

Re: List of files on HDD

#5 Post by JohnC » 19 Jul 2018 00:42

Sorry, one more request....in the same command line - how do I isolate specific file extensions eg *.jpg *.mov *.mp4 ? thanks again, John

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

Re: List of files on HDD

#6 Post by Compo » 19 Jul 2018 03:45

You could use the Dir command as provided by penpen:

Code: Select all

(For /F "Delims=" %A In ('Dir /B/S/A-D *.jpg *.mov *.mp4') Do @Echo %~nxA)>"j.txt"
You could also use the RoboCopy command:

Code: Select all

(For /F "Delims=" %A In ('RoboCopy . null *.jpg *.mov *.mp4 /S /XJ /L /NS /NC /NDL /NJH /NJS') Do @Echo %~nxA)>"j.txt"
Or my usual preference, the Where command:

Code: Select all

(For /F "Delims=" %A In ('Where /R . *.jpg *.mov *.mp4') Do @Echo %~nxA)>"j.txt"
If, for instance, the search mask is *.doc *.xls, this would return only those file types. My experience with the Dir and RoboCopy options is that they seem to also include those with *.docm *.docx *.xlsb *.xlsm *.xlsx etc.

Remember also that if used from a batch file to double up the % characters:

Code: Select all

@(For /F "Delims=" %%A In ('Where /R . *.jpg *.mov *.mp4') Do @Echo %%~nxA)>"j.txt"

sst
Posts: 79
Joined: 12 Apr 2018 23:45

Re: List of files on HDD

#7 Post by sst » 19 Jul 2018 06:00

@Compo,
I don't like the weirdness of echo( and the confusion it causes, I only use it whenever I can't be sure that "/?" will not appear in the echo's parameter which in this case can not happen with file names, maybe you feel the same about that; I don't know, but you should at least use echo, to protect against on and off (or whitespace/empty parameter which can't happen is this case).

@JohnC,
Parsing the output from DIR or any other command that lists files, can get slow if the number of files is very large(it has the potential to reach millions), because FOR command has to wait for DIR to finish its job, then only after that it starts feeding the body of loop with gathered data.
So another alternative is to use build-in file listing capabilities of FOR command. But The disadvantage is that it can not list hidden files - (files within hidden directories will be listed). That being said you should decide which method fits best to your needs.

Also note that on an NTFS volumes, directory permissions may prevent you from accessing some directories thus not being able to list files in them, no matter which method you use.

These are for using at command prompt, for use in batch files you need to double the percents(%)

List all files (except hidden files):

Code: Select all

(for /D /R %a in (.) do @for %a in ("%~a\*") do @echo,%~nxa)>"j.txt"
OR

Code: Select all

(for /R %a in (*) do @if not exist "%~a\" echo,%~nxa)>"j.txt"

Filtered list based on file extension (except hidden files):

Code: Select all

(for /D /R %a in (.) do @for %a in ("%~a\*.jpg" "%~a\*.mov" "%~a\*.mp4") do @echo,%~nxa)>"j.txt"
OR

Code: Select all

(for /R %a in (*.jpg *.mov *.mp4) do @if not exist "%~a\" echo,%~nxa)>"j.txt"
In the first alternative solution the reason for using (.) instead of (*) is to enable the FOR command to see and recurse into the hidden directories as well.
In the second alternative solution, if not exist "%~a\" filters out the directories from listing, so the trailing backslash (\) in "%~a\" is crucial.

The above codes list files in current directory and all of its sub directories. Alternatively you can explicitly specify which directory to list:

Code: Select all

for /D /R "C:\MyFolder\" %a in (.) @for %a in ("%~a\*") ...
for /R "C:\MyFolder\" %a in (*) do @if not exist "%~a\" ...

JohnC
Posts: 4
Joined: 14 Jul 2018 16:25

Re: List of files on HDD

#8 Post by JohnC » 19 Jul 2018 17:52

Perfect...thank you

Post Reply