How can I within Windows command prompt list all files that are contained in that directory and all subdirectories but the list should only contain a list of files and not the directory location that each file is found in?
Example: dir /d /s /b /q *.txt will get a list of all text files in all directories so I will end up with c:\example1.txt c:\folder1\example2.txt but I need only the list to show example1.txt example2.txt I hope my question is understandable. just a list of files not showing their paths?
CodePudding user response:
You can try with this command in PowerShell
dir *.txt -r | % Name
From cmd you can execute like
PowerShell -Command "dir *.txt -r | % Name"
CodePudding user response:
There can be used in a Windows command prompt window:
for /R %I in (*.txt) do @echo %~nxI
That results in searching recursive because of option /R
in current directory and all its subdirectories for non-hidden files of which long or short 8.3 file name is matched by the wildcard pattern *.txt
. Output is just the file name with extension.
Run in a command prompt window for /?
to get output the help of the Windows command FOR.
There can be used in a command prompt window also:
for /F "delims=" %I in ('dir *.txt /A-D /B /S 2^>nul') do @echo %~nxI
This command line starts in background one more cmd.exe
with option /c
to execute the command line enclosed in '
appended as additional arguments. So there is executed in background:
C:\Windows\System32\cmd.exe /c dir *.txt /A-D /B /S 2>nul
The command DIR searches now
- in current directory and all its subdirectories because of option
/S
- for just files because of option
/A-D
(all attributes except directory attribute) including also hidden files - of which long or short 8.3 name is matched by the wildcard pattern
*.txt
.
There is output to handle STDOUT of background command process just the list of found file names in bare format because of option /B
with full path because of option /S
.
It is possible that DIR does not find any file system entry matched by the criteria which results in an error message output to handle STDOUT. This error message is redirected with 2>nul
to the device NUL to suppress it.
Read the Microsoft documentation about Using command redirection operators for an explanation of 2>nul
. The redirection operator >
must be escaped with caret character ^
on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir
command line in a separate command process started in background.
The output list of fully qualified file names is captured by cmd.exe
being opened as Windows command prompt and processed by FOR after cmd.exe
started in background closed itself. So it can take some time until there is something displayed in the command prompt window depending on how much file system entries must be searched for by the command DIR of background command process.
FOR would split up by default each line into substrings using normal space and horizontal tab as string delimiters. This line splitting behavior is not wanted here and therefore the option delims=
is used to define an empty list of string delimiters to turn off the line splitting. So each file name is assigned one after the other to the specified loop variable I
.
The command ECHO outputs finally the file name assigned to loop variable I
with just name and extension.
Run in the command prompt window also dir /?
for help on this internal command of the Windows Command Processor.