Use PowerShell to Automate Migration of FTP files to a File Share

A common business process to automate is moving files from an FTP server. To copy the files from an FTP server we first need to get a file listing. The following routine serves nicely:

function Get-FtpDir ($url,$credentials) {
$request = [Net.WebRequest]::Create($url)
$request.Method = [System.Net.WebRequestMethods+FTP]::ListDirectory
if ($credentials)
{
$request.Credentials = $credentials
}
$response = $request.GetResponse()
$reader = New-Object IO.StreamReader $response.GetResponseStream()
$reader.ReadToEnd()
$reader.Close()
$response.Close()
}

Let’s set some basic parameters for the file migration:

$url = 'ftp://sftp.SomeDomain.com';
$user = 'UserID';
$pass = 'Password'  #single quotes recommended if unusual characters are in use
$DeleteSource = $true;  #controls whether to delete the source files from the FTP Server after copying
$DestLocation = '\DestinationServerAnyLocation';

Let’s start the processing with connecting to the FTP server, getting the file list, and processing:

$credentials = new-object System.Net.NetworkCredential($user, $pass)
$webclient = New-Object System.Net.WebClient
$webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass)  
$files=Get-FTPDir $url $credentials
$filesArr = $files.Split("`n")
Foreach ($file in ($filesArr )){
if ($file.length -gt 0) #this actually happens for last file
{
$source=$url+$file
$dest = $DestLocation + $file
$dest = $dest.Trim()		# this is actually needed, as trailing blank appears in FTP directory listing
$WebClient.DownloadFile($source, $dest)
}
}

Let’s now delete the source files, if configured to do so:

if ($DeleteSource)
{
Foreach ($file in ($filesArr )){
if ($file.length -gt 0) #this actually happens for last file
{
$source=$url+$file
$ftprequest = [System.Net.FtpWebRequest]::create($source)
$ftprequest.Credentials =  New-Object System.Net.NetworkCredential($user,$pass)
$ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::DeleteFile
$ftprequest.GetResponse()
}
}
}

That’s it in a nutshell. Just don’t try to move zero length files, and trim the filenames for trailing blanks. Ensure your FTP ports are open, and you are good to go!

1 reply

Trackbacks & Pingbacks

  1. […] previous post covered how to programmatically download documents via FTP: How to download from FTP programmatically If FTP over SSL is needed, that’s just a property to enable […]

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *