Advertisement
Guest User

Untitled

a guest
Sep 30th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. $Server = "test.rebex.net"
  2. $User = "demo"
  3. $Pass = "password"
  4. #
  5. #
  6. Function Get-FtpDirectory($Directory) {
  7.  
  8. # Credentials
  9. $FTPRequest = [System.Net.FtpWebRequest]::Create("$($Server)$($Directory)")
  10. $FTPRequest.Credentials = New-Object System.Net.NetworkCredential($User,$Pass)
  11. $FTPRequest.Method = [System.Net.WebRequestMethods+FTP]::ListDirectoryDetails
  12.  
  13. # Don't want Binary, Keep Alive unecessary.
  14. $FTPRequest.UseBinary = $False
  15. $FTPRequest.KeepAlive = $False
  16.  
  17. $FTPResponse = $FTPRequest.GetResponse()
  18. $ResponseStream = $FTPResponse.GetResponseStream()
  19.  
  20. # Create a nice Array of the detailed directory listing
  21. $StreamReader = New-Object System.IO.Streamreader $ResponseStream
  22. $DirListing = (($StreamReader.ReadToEnd()) -split [Environment]::NewLine)
  23. $StreamReader.Close()
  24.  
  25. # Remove first two elements ( . and .. ) and last element (\n)
  26. $DirListing = $DirListing[2..($DirListing.Length-2)]
  27.  
  28. # Close the FTP connection so only one is open at a time
  29. $FTPResponse.Close()
  30.  
  31. # This array will hold the final result
  32. $FileTree = @()
  33.  
  34. # Loop through the listings
  35. foreach ($CurLine in $DirListing) {
  36.  
  37. # Split line into space separated array
  38. $LineTok = ($CurLine -split '\ +')
  39.  
  40. # Get the filename (can even contain spaces)
  41. $CurFile = $LineTok[8..($LineTok.Length-1)]
  42.  
  43. # Figure out if it's a directory. Super hax.
  44. $DirBool = $LineTok[0].StartsWith("d")
  45.  
  46. # Determine what to do next (file or dir?)
  47. If ($DirBool) {
  48. # Recursively traverse sub-directories
  49. $FileTree += ,(Get-FtpDirectory "$($Directory)$($CurFile)/")
  50. } Else {
  51. # Add the output to the file tree
  52. $FileTree += ,"$($Directory)$($CurFile)"
  53. }
  54. }
  55.  
  56. Return $FileTree
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement