Skip to content

Commit

Permalink
Merge pull request #452 from damienfinck/Improve_Get-SFTPChildItem
Browse files Browse the repository at this point in the history
Improve Get-SFTPChildItem
  • Loading branch information
darkoperator committed May 20, 2022
2 parents d62f86a + 8218bcb commit a39169a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 20 deletions.
49 changes: 32 additions & 17 deletions Posh-SSH/Posh-SSH.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -1079,8 +1079,17 @@ function Get-SFTPChildItem
$Path,
[Parameter(Mandatory=$false,
Position=2)]
[Alias('Recursive')]
[switch]
$Recursive
$Recurse,
[Parameter(Mandatory=$false,
Position=3)]
[switch]
$Directory,
[Parameter(Mandatory=$false,
Position=4)]
[switch]
$File

)

Expand All @@ -1091,14 +1100,27 @@ function Get-SFTPChildItem
{
param($Path,$SFTPSession)

$total = $Sess.Session.ListDirectory($Path)

#List Files
$total | Where-Object {$_.IsDirectory -eq $false}

#Get items in a path
$total | Where-Object {$_.IsDirectory -eq $true -and @('.','..') -notcontains $_.Name } |
ForEach-Object {$_; Get-SFTPDirectoryRecursive -Path $_.FullName -SFTPSession $sess}
$Sess.Session.ListDirectory($Path) | ForEach-Object {
if ($File -and $Directory)
{
# Item cannot be a file AND a directory
}
elseif ((!$File -and !$Directory) -or ($File -and !$_.IsDirectory) -or ($Directory -and $_.IsDirectory))
{
if (@('.','..') -notcontains $_.Name)
{
# Item to keep
$_
}
}
if ($Recurse)
{
if ($_.IsDirectory -eq $true -and @('.','..') -notcontains $_.Name)
{
Get-SFTPDirectoryRecursive -Path $_.FullName -SFTPSession $sess
}
}
}

}

Expand Down Expand Up @@ -1139,14 +1161,7 @@ function Get-SFTPChildItem
throw "Specified path of $($Path) is not a directory."
}
}
if($Recursive)
{
Get-SFTPDirectoryRecursive -Path $Path -SFTPSession $Sess
}
else
{
$Sess.Session.ListDirectory($Path)
}
Get-SFTPDirectoryRecursive -Path $Path -SFTPSession $Sess
}
}
End{}
Expand Down
38 changes: 35 additions & 3 deletions docs/Get-SFTPChildItem.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ Gets the items and child items in a specified path.

### Index (Default)
```
Get-SFTPChildItem [-SessionId] <Int32[]> [[-Path] <String>] [-Recursive] [<CommonParameters>]
Get-SFTPChildItem [-SessionId] <Int32[]> [[-Path] <String>] [-Recurse] [-Directory] [-File]
[<CommonParameters>]
```

### Session
```
Get-SFTPChildItem [-SFTPSession] <SftpSession[]> [[-Path] <String>] [-Recursive] [<CommonParameters>]
Get-SFTPChildItem [-SFTPSession] <SftpSession[]> [[-Path] <String>] [-Recurse] [-Directory] [-File]
[<CommonParameters>]
```

## DESCRIPTION
Expand Down Expand Up @@ -119,7 +121,7 @@ Gets the items in the specified locations and in all child items of the location
```yaml
Type: SwitchParameter
Parameter Sets: (All)
Aliases:
Aliases: Recursive

Required: False
Position: 2
Expand All @@ -143,6 +145,36 @@ Accept pipeline input: True (ByValue)
Accept wildcard characters: False
```
### -Directory
To get a list of directories.
```yaml
Type: SwitchParameter
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False
```
### -File
To get a list of files.
```yaml
Type: SwitchParameter
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False
```
### CommonParameters
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).
Expand Down

0 comments on commit a39169a

Please sign in to comment.