A PowerShell script...
Clear all

A PowerShell script to list all files and folders within a directory.

Member Admin
Joined: 4 years ago
Posts: 31
Topic starter  

I've been trying to find a script that recursively prints all files and folders within a directory like this where the backslash is used to indicate directories:

Source code\
Source code\Base\
Source code\Base\main.c
Source code\Base\print.c

I'm using PowerShell 3.0 and most other scripts I've found do not work (though they didn't anything like what I'm asking).
Additionally: I need it to be recursive.

Member Moderator
Joined: 4 years ago
Posts: 52

What you are likely looking for is something to help distinguish a file from a folder. Luckily there is a property call PSIsContainer that is true for folder and false for files.

dir -r | % { if ($_.PsIsContainer) { $_.FullName + "\" } else { $_.FullName } }
C:\Source code\Base\
C:\Source code\List.txt
C:\Source code\Base\main.c
C:\Source code\Base\print.c

If the leading path information is not desirable, you can remove it easily enough using -replace:

dir | % { $_.FullName -replace "C:\\","" }

Hopefully this gets you headed off in the right direction.

Neha liked