Melab
Melab

Reputation: 2822

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

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
List.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.

Upvotes: 13

Views: 86287

Answers (9)

Goyuix
Goyuix

Reputation: 24340

It seems that you are looking for a way to distinguish files from directories. Luckily there is a property called PSIsContainer that is true for directories 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:\\","" }

Upvotes: 19

N0517V
N0517V

Reputation: 1

I made an improved version of the code submitted (since the code output are inside powershell which has an output limit)

  1. Shift + Right Click in the folder you're trying to scan files and folder on
  2. copy and paste this (just edit your_pc_name)

dir -r | % { if ($.PsIsContainer) { $.FullName + "" } else { $_.FullName } } | Out-File -FilePath c:\users\your_pc_name\desktop\OUTPUT.txt

This will print all the files and folders into a txt file in your dekstop.

extra tips:

  1. copy the output and paste it in excel
  2. use ctrl + f to search for the filename you wanted.

This help me a lot in searching for a large database of files.

Upvotes: 0

UCN
UCN

Reputation: 23

You can achieve this through the get-childitem command in PowerShell. Refer to the below syntax:

Get-ChildItem "Folder name or Path" -Recurse | select FullName > list.txt

This will help you write all the plain files and folders names recursively onto a file called list.txt Refer to this for more information. https://ss64.com/ps/get-childitem.html

Answering late, but it might help someone!

Upvotes: 1

drmkjaiswal
drmkjaiswal

Reputation: 31

PowerShell Command For Directory List into Txt File:

For Full Path Directory List (Folder & File) to text file:

ls -r | % { $_.FullName + $(if($_.PsIsContainer){'\'}) } > filelist.txt

For Relative Path Directory List (Folder & File) to text file:

ls -r | % { $_.FullName.substring($pwd.Path.length+1) + $(if($_.PsIsContainer){'\'}) } > filelist.txt

Upvotes: 3

Tim
Tim

Reputation: 776

Not powershell, but you can use the following within command prompt to recursively list files into a textfile:

dir *.* /s /b /a:-d > filelist.txt

Upvotes: 2

CB.
CB.

Reputation: 60918

It could be like:

$path = "c:\Source code"
DIR $path -Recurse | % { 
    $_.fullname -replace [regex]::escape($path), (split-path $path -leaf)
}

Following the @Goyuix idea:

$path = "c:\source code"
DIR $path -Recurse | % {
    $d = "\"
    $o = $_.fullname -replace [regex]::escape($path), (split-path $path -leaf)
    if ( -not $_.psiscontainer) {
        $d = [string]::Empty 
    }
    "$o$d"
}

Upvotes: 5

Michael Sorens
Michael Sorens

Reputation: 36708

This one shows full paths, as some of the other answers do, but is shorter:

ls -r | % { $_.FullName + $(if($_.PsIsContainer){'\'}) }

However, the OP I believe asked for relative paths (i.e. relative to the current directory) and only @C.B.'s answer addressed that point. So by just adding a substring we have this:

ls -r | % { $_.FullName.substring($pwd.Path.length+1) + $(if($_.PsIsContainer){'\'}) }

Upvotes: 3

E.V.I.L.
E.V.I.L.

Reputation: 2166

(ls $path -r).FullName | % {if((get-item "$_").psiscontainer){"$_\"}else{$_}}

Only use in PS 3.0

Upvotes: 0

Shay Levy
Shay Levy

Reputation: 126742

dir | % {
   $p= (split-path -noqualifier $_.fullname).substring(1)
   if($_.psiscontainer) {$p+'\'} else {$p}
}

Upvotes: 3

Related Questions