How can I list directories with file (question's really about recursion)
-
27. února 2012 20:46
I have a folder with several subfolders. Some folders have files, others, have more folders with folders with files. This repeats unpredictably ad nauseum. How would I get a list of all only containing files? I found Keith Hill's function GetFiles (http://stackoverflow.com/questions/8024103/how-to-retrieve-a-recursive-directory-and-file-list-from-powershell-excluding-so), but, it only goes 1 deep. I need something that will recurse until all files are found:
function GetFiles($path = $pwd, [string[]]$exclude) { foreach ($item in Get-ChildItem $path) { if ($exclude | Where {$item -like $_}) { continue } if (Test-Path $item -PathType Container) { $item GetFiles $item.FullName $exclude } else { $item } } }
Všechny reakce
-
27. února 2012 20:49
Check this out:
http://tasteofpowershell.blogspot.com/2008/08/recursion-in-powershell.html
http://stackoverflow.com/questions/4989021/powershell-recursion-with-return
Thanks & Regards
Bhavik Solanki
Please click “Mark as Answer” if this post answers your question and click "Vote as Helpful if this Post helps you.- Upravený Bhavik Solanki 27. února 2012 20:50
-
27. února 2012 20:52Are you wanting a list of folders that contain any files, or a list of folders that contain only files?
[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
-
27. února 2012 20:53Both. That's the next step. But, I was thinking maybe a function could do both if I set it up correctly.
-
27. února 2012 21:11
$path = 'c:\testfiles' (cmd /c dir $path /a-d /s /b) -replace '^(.+)\\.+$','$1' | sort -unique
Does that work for directories that contain any files?[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
- Navržen jako odpověď Al Dunbar 27. února 2012 21:46
-
27. února 2012 21:18
-
27. února 2012 21:45cmd /c dir /b /s /a-d- Larry>>On 2/27/2012 2:46 PM, Will Steele wrote:> ... How would I get a list of all only> containing files?>
that shows all of the files, conveniently missing the empty directories. To get a list of the folders containing the files you'd have to strip the filenames out of the list and remove duplicates.Al Dunbar
-
27. února 2012 21:51Thanks. Obviously I misread the request. But as you have analyzed, it could be a first steptowards the true goal.- Larry>>On 2/27/2012 3:45 PM, Al Dunbar wrote:> cmd /c dir /b /s /a-d> - Larry> >> >> On 2/27/2012 2:46 PM, Will Steele wrote:> > ... How would I get a list of all only> > containing files?.> >>>> that shows all of the files, conveniently missing the empty directories. To get a list of the> folders containing the files you'd have to strip the filenames out of the list and remove duplicates.
-
27. února 2012 21:54
$path = 'c:\testfiles' (cmd /c dir $path /a-d /s /b) -replace '^(.+)\\.+$','$1' | sort -unique
Does that work for directories that contain any files?
[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
Or, if you want a powershell-only solution, try this:
$path = 'c:\testfiles'
dir $path -rec | where {-not $_.psiscontainer}| select directory | sort -uniqueAl Dunbar
-
27. února 2012 21:56That'll work, but I'd add an -expand to that select.
[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
-
27. února 2012 22:05
-
27. února 2012 22:33
No, it doesn't.
$path = 'c:\testfiles\' dir $path -rec | where {-not $_.psiscontainer} | % {$_.directory.fullname} | sort -unique[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
- Označen jako odpověď Yan Li_Microsoft Contingent Staff, Moderator 28. února 2012 4:49
-
27. února 2012 22:38
Yeah, I ended up doing something along these lines and it worked. More than one step, but, I got what I needed.
cls function Recurse ([string]$path, [string]$fileglob){ if (-not (Test-Path $path)) { Write-Error "$path is an invalid path." return $false } $files = @(dir -Path $path -Include $fileglob) foreach ($file in $files) { if ($file.GetType().FullName -eq 'System.IO.FileInfo') { Write-Output $file.FullName }elseif ($file.GetType().FullName -eq 'System.IO.DirectoryInfo') { Recurse $file.FullName } } } <# Holding onto in case I need it later. filter Get-ParentNameFromFileInfo { param( [Parameter( ValueFromPipeline = $true )] $filepath ) $split = ((Split-Path $filepath.fullname) -Split '\\')# [((Split-Path $filepath.fullname) -Split '\\').Count - 1] $split[$split.length - 1] }#> $paths = Recurse 'C:\some\really\annoying\folder\with\stuff' | % { Split-Path $_ } | select -Unique foreach($dir in (Get-ChildItem $paths | Where {$_.extension -eq '.iso'})) { Get-ChildItem $dir.FullName | select name,@{e={([System.IO.FileInfo] $_.fullname).Directory.Name};l='directory'} }
- Označen jako odpověď Yan Li_Microsoft Contingent Staff, Moderator 28. února 2012 4:49
-
27. února 2012 22:42
Get-ChildItem $path -rec | Where {$_.Psiscontainer -and $_.GetFiles()} | ft fullname- Navržen jako odpověď BigteddyMicrosoft Community Contributor 28. února 2012 3:11
- Označen jako odpověď Yan Li_Microsoft Contingent Staff, Moderator 28. února 2012 4:49
-
27. února 2012 22:46
-
27. února 2012 22:47
That'll work, but I'd add an -expand to that select.
[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
Aack! lack of proper testing. it worked when only one folder contained any files... ;-)Al Dunbar
-
27. února 2012 22:48
Get-ChildItem $path -rec | Where {$_.Psiscontainer -and $_.GetFiles()} | ft fullname
even better, nice!Al Dunbar
-
28. února 2012 0:04So,(gi .).getfiles()is a way to get just the files at a FileSystem location (in that case the current directory).So much rich expressiveness in the .Net objects that I don't know how to remember all of it.>>On 2/27/2012 4:42 PM, Kazun [MVP] wrote:> Get-ChildItem $path -rec | Where {$_.Psiscontainer -and $_.GetFiles()} | ft fullname
-
28. února 2012 3:12Who did I say was the King of Concise?
Grant Ward, a.k.a. Bigteddy