Is there a way to copy directory and its subdirectories using powershell from ftp server?

MoTaar 310 Reputation points
2023-12-14T13:44:46.5933333+00:00

Is there a script that allows to copy ftp directory and its subdirectories from server to local ?

I tried this one but it downloads only one file:

# Replace the values below with your own

$resourceGroupName = "myResourceGroup"

$appName = "myAppService"

$username = "username"

$password = "password"

$localPath = "C:\local\path"

$remotePath = "/site/wwwroot/remote/path"

# Connect to the Azure App Service using FTPS

$ftpsUri = "ftps://$appName.ftp.azurewebsites.windows.net/$remotePath"

$ftpsRequest = [System.Net.FtpWebRequest]::Create($ftpsUri)

$ftpsRequest.Credentials = New-Object System.Net.NetworkCredential($username, $password)

$ftpsRequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile

$ftpsRequest.EnableSsl = $true

# Download the files

$response = $ftpsRequest.GetResponse()

$stream = $response.GetResponseStream()

$localFilePath = Join-Path $localPath (Split-Path $ftpsRequest.RequestUri.LocalPath -Leaf)

$stream.ReadTimeout = 10000

$buffer = New-Object byte[] 1024

$fs = New-Object System.IO.FileStream($localFilePath,[System.IO.FileMode]::Create)

while (($read = $stream.Read($buffer,0,$buffer.Length)) -gt 0) {

$fs.Write($buffer,0,$read)

}

$fs.Close()

$response.Close()

Write-Host "Files downloaded to $localFilePath"

Thank you!

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2023-12-14T16:22:05.9833333+00:00

    I don't believe the FTP protocol makes provisions for recursive operations. The ftp.exe utility doesn't, and I don't think that the .Net FTP class does.

    There are ftp clients the will do this, though.

    WinSCP is one (https://winscp.net/eng/index.php)

    NcFTP is another (https://www.ncftp.com/download/)

    FSync used to be another, but it looks like its domain is up for sale.

    This might be helpful: https://freefilesync.org/


  2. MoTaar 310 Reputation points
    2024-01-03T21:41:48.3866667+00:00
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.