Page 1 of 1

List of files on HDD

Posted: 14 Jul 2018 22:14
by JohnC
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

Re: List of files on HDD

Posted: 15 Jul 2018 05:31
by penpen
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

Re: List of files on HDD

Posted: 15 Jul 2018 12:29
by Compo
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"

Re: List of files on HDD

Posted: 17 Jul 2018 22:49
by JohnC
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

Re: List of files on HDD

Posted: 19 Jul 2018 00:42
by JohnC
Sorry, one more request....in the same command line - how do I isolate specific file extensions eg *.jpg *.mov *.mp4 ? thanks again, John

Re: List of files on HDD

Posted: 19 Jul 2018 03:45
by Compo
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"

Re: List of files on HDD

Posted: 19 Jul 2018 06:00
by sst
@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\" ...

Re: List of files on HDD

Posted: 19 Jul 2018 17:52
by JohnC
Perfect...thank you