A PowerShell script...
 
Share:
Notifications
Clear all

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


Neha
 Neha
(@asha)
Member Admin
Joined: 1 year ago
Posts: 27
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
Sample.txt

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.


Quote
myTechMint
(@mytechmint)
Member Moderator
Joined: 1 year ago
Posts: 29
 

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
ReplyQuote