0

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.

2 Answers 2

2

Use Powershell:

 Get-ChildItem .\*\*

Edit:

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}
4
  • +1 Best answer I saw, and it does not use batch-files (and OP did not ask for batch-files, only for being able to do this in batches).
    – Hennes
    Jul 12, 2016 at 9:36
  • @Hennes I have a nitpick, please check my updated answer.
    – guest
    Jul 12, 2016 at 9:41
  • @guest I can't comment on your post, so here. 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
  • thank for tips (i actually just crank up powershell in response to your answer). however dir -recurse *\* doesn't list current dir files for me, tested on W10 PoSh
    – guest
    Jul 12, 2016 at 10:08
1

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}

2
  • While this may answer the question, it would be a better answer if you could provide some explanation why it does so.
    – DavidPostill
    Jul 11, 2016 at 11:31
  • @David done, correct me if i am wrong, feel free to edit.
    – guest
    Jul 12, 2016 at 9:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .