@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\" ...