none
How can I list directories with file (question's really about recursion)

Odpovědi

  • Get-ChildItem $path -rec | Where {$_.Psiscontainer -and $_.GetFiles()} | ft fullname
    27. února 2012 22:42
  • 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 " "

    27. února 2012 22:33
  • 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'}
    }

    27. února 2012 22:38

Všechny reakce

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


    27. února 2012 20:49
  • Are 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:52
  • Both.  That's the next step.  But, I was thinking maybe a function could do both if I set it up correctly.
    27. února 2012 20:53
  • $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 " "

    27. února 2012 21:11
  • 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?
     >
     
    27. února 2012 21:18
  • 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.

    Al Dunbar

    27. února 2012 21:45
  • Thanks.  Obviously I misread the request.  But as you have analyzed, it could be a first step
    towards 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:51
  • $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 -unique


    Al Dunbar

    27. února 2012 21:54
  • That'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 21:56
  • That did not work for me.
     >
     >
    On 2/27/2012 3:56 PM, mjolinor wrote:
    > That'll work, but I'd add an -expand to that select.
    >
     
    27. února 2012 22:05
  • 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 " "

    27. února 2012 22:33
  • 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'}
    }

    27. února 2012 22:38
  • Get-ChildItem $path -rec | Where {$_.Psiscontainer -and $_.GetFiles()} | ft fullname
    27. února 2012 22:42
  • dir $path -rec | ?{! $_.psiscontainer} | %{$_.directory.fullname} | sort -unique
     
    as a more terse version.
     
    27. února 2012 22:46
  • 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:47
  • Get-ChildItem $path -rec | Where {$_.Psiscontainer -and $_.GetFiles()} | ft fullname

    even better, nice!

    Al Dunbar

    27. února 2012 22:48
  • So,
     
    (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 0:04
  • Who did I say was the King of Concise?

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    28. února 2012 3:12