Is there a way to list all files in all subdirectories excluding those in the current directory?
dir /s /b
lists all files, including those in the current directory. That is not what I want.
Use Powershell:
Get-ChildItem .\*\*
Above example indeed only gets the 2nd level or directories and not contents of subfolders.
@guest's answer is the better one:
Get-ChildItem -Directory | ForEach-Object {Get-ChildItem -Recurse | Select-Object FullName}
dir -recurse *\*
list content of the current directory. That's not what OP wants. Your 2nd PoSh example is right and better than my answer. You can drop the 2nd foreach
and speed it by doing Get-ChildItem -Directory | ForEach-Object {Get-ChildItem -Recurse | Select-Object FullName}
or really short ls -Dir | % {ls -rec | select fullname}
Jul 12, 2016 at 9:59
dir -recurse *\*
doesn't list current dir files for me, tested on W10 PoSh
There are multiple solutions, which I will try to explain one by one.
Batch
1) for /d %i in (*) do @dir /b /s "%i"
(*)
expands to all files in current directory.
for /d
then loops through all directories, excluding regular files, executing dir /b /s
for each directory.
Since for
echos executed commands, @
is used to suppress the echo.
2) for /f "delims=" %i in ('dir /b /A:D') do @dir /b /s "%i"
'dir /b /A:D'
returns all directories in current directory, with each directory on one line.
"delims="
instructs for /f
not to tokenize a line but assign the whole line to %i
.
for /f
then loops through all lines, executing dir /b /s
for each line.
Use %%i
instead of %i
in batch file.
Powershell
dir *\*
only returns files in level 2 directories.
dir -recurse *\*
also returns files in deeper directories but doesn't show empty folder in level 2.
Correct commands which take the same approach to that in Batch:
1) dir | where {$_.PsIsContainer} | foreach {dir -recurse $_} | foreach {echo $_.fullname}
2) dir -directory | foreach {dir -recurse $_} | foreach {echo $_.fullname}