-
-
Save nic0-lab/0b44cfece63352e4760d9b46921183cd to your computer and use it in GitHub Desktop.
Recursively list all files in FTP directory in PowerShell / List files (recursive)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$Server = "ftp://ftp.example.com/" | |
$User = "[email protected]" | |
$Pass = "[email protected]" | |
Function Get-FtpDirectory($Directory) { | |
# Credentials | |
$FTPRequest = [System.Net.FtpWebRequest]::Create("$($Server)$($Directory)") | |
$FTPRequest.Credentials = New-Object System.Net.NetworkCredential($User,$Pass) | |
$FTPRequest.Method = [System.Net.WebRequestMethods+FTP]::ListDirectoryDetails | |
# Don't want Binary, Keep Alive unecessary. | |
$FTPRequest.UseBinary = $False | |
$FTPRequest.KeepAlive = $False | |
$FTPResponse = $FTPRequest.GetResponse() | |
$ResponseStream = $FTPResponse.GetResponseStream() | |
# Create a nice Array of the detailed directory listing | |
$StreamReader = New-Object System.IO.Streamreader $ResponseStream | |
$DirListing = (($StreamReader.ReadToEnd()) -split [Environment]::NewLine) | |
$StreamReader.Close() | |
# Remove first two elements ( . and .. ) and last element (\n) | |
$DirListing = $DirListing[2..($DirListing.Length-2)] | |
# Close the FTP connection so only one is open at a time | |
$FTPResponse.Close() | |
# This array will hold the final result | |
$FileTree = @() | |
# Loop through the listings | |
foreach ($CurLine in $DirListing) { | |
# Split line into space separated array | |
$LineTok = ($CurLine -split '\ +') | |
# Get the filename (can even contain spaces) | |
$CurFile = $LineTok[8..($LineTok.Length-1)] | |
# Figure out if it's a directory. Super hax. | |
$DirBool = $LineTok[0].StartsWith("d") | |
# Determine what to do next (file or dir?) | |
If ($DirBool) { | |
# Recursively traverse sub-directories | |
$FileTree += ,(Get-FtpDirectory "$($Directory)$($CurFile)/") | |
} Else { | |
# Add the output to the file tree | |
$FileTree += ,"$($Directory)$($CurFile)" | |
} | |
} | |
Return $FileTree | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment