{"id":1089,"date":"2016-09-16T09:06:27","date_gmt":"2016-09-16T09:06:27","guid":{"rendered":"https:\/\/poiseddevelopers.com\/reality-tech\/?p=1089"},"modified":"2024-05-06T12:17:18","modified_gmt":"2024-05-06T12:17:18","slug":"use-powershell-to-automate-migration-of-ftp-files-to-a-file-share","status":"publish","type":"post","link":"https:\/\/poiseddevelopers.com\/reality-tech\/automate-migration-of-ftp-files-to-a-file-share\/","title":{"rendered":"Use PowerShell to Automate Migration of FTP files to a File Share"},"content":{"rendered":"<p>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:<\/p>\n<pre lang=\"php\">function Get-FtpDir ($url,$credentials) {\r\n    $request = [Net.WebRequest]::Create($url)\r\n    $request.Method = [System.Net.WebRequestMethods+FTP]::ListDirectory\r\n    if ($credentials)\r\n    {\r\n        $request.Credentials = $credentials\r\n    }\r\n    $response = $request.GetResponse()\r\n    $reader = New-Object IO.StreamReader $response.GetResponseStream()\r\n    $reader.ReadToEnd()\r\n    $reader.Close()\r\n    $response.Close()\r\n}\r\n<\/pre>\n<p>Let\u2019s set some basic parameters for the file migration:<\/p>\n<pre lang=\"php\">$url = 'ftp:\/\/sftp.SomeDomain.com';\r\n$user = 'UserID';\r\n$pass = 'Password'  #single quotes recommended if unusual characters are in use\r\n$DeleteSource = $true;  #controls whether to delete the source files from the FTP Server after copying\r\n$DestLocation = '\\DestinationServerAnyLocation';\r\n<\/pre>\n<p>Let\u2019s start the processing with connecting to the FTP server, getting the file list, and processing:<\/p>\n<pre lang=\"php\">$credentials = new-object System.Net.NetworkCredential($user, $pass)\r\n $webclient = New-Object System.Net.WebClient\r\n $webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass)  \r\n \r\n$files=Get-FTPDir $url $credentials\r\n$filesArr = $files.Split(\"`n\")\r\n \r\nForeach ($file in ($filesArr )){\r\n    if ($file.length -gt 0) #this actually happens for last file\r\n    {\r\n    $source=$url+$file\r\n    $dest = $DestLocation + $file\r\n    $dest = $dest.Trim()        # this is actually needed, as trailing blank appears in FTP directory listing\r\n    $WebClient.DownloadFile($source, $dest)\r\n    }\r\n}\r\n<\/pre>\n<p>Let\u2019s now delete the source files, if configured to do so:<\/p>\n<pre lang=\"php\">$credentials = new-object System.Net.NetworkCredential($user, $pass)\r\n $webclient = New-Object System.Net.WebClient\r\n $webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass)  \r\n \r\n$files=Get-FTPDir $url $credentials\r\n$filesArr = $files.Split(\"`n\")\r\n \r\nForeach ($file in ($filesArr )){\r\n    if ($file.length -gt 0) #this actually happens for last file\r\n    {\r\n    $source=$url+$file\r\n    $dest = $DestLocation + $file\r\n    $dest = $dest.Trim()        # this is actually needed, as trailing blank appears in FTP directory listing\r\n    $WebClient.DownloadFile($source, $dest)\r\n    }\r\n}\r\n<\/pre>\n<p>Let\u2019s now delete the source files, if configured to do so:<\/p>\n<pre lang=\"php\">if ($DeleteSource)\r\n{\r\nForeach ($file in ($filesArr )){\r\n    if ($file.length -gt 0) #this actually happens for last file\r\n    {\r\n    $source=$url+$file\r\n    $ftprequest = [System.Net.FtpWebRequest]::create($source)\r\n    $ftprequest.Credentials =  New-Object System.Net.NetworkCredential($user,$pass)\r\n    $ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::DeleteFile\r\n    $ftprequest.GetResponse()\r\n    }\r\n}\r\n}\r\n<\/pre>\n<p>That\u2019s it in a nutshell. Just don\u2019t 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!<\/p>\n<div style=\"background-color: white; box-shadow: 0 0 10px whitesmoke; padding: 20px; border-radius: 5px; display: flex; justify-content: space-around; width: 950px;\">\n<div class=\"content\">\n<h4>Start Your PowerShell Migration Project In A Click<\/h4>\n<p style=\"width: 70%;\">Our technology and wide delivery footprint have created billions of dollars in value for clients globally and are widely recognized by industry professionals and analysts.<\/p>\n<\/div>\n<div style=\"display: flex; align-items: center;\">\n<div class=\"content-btn\" style=\"padding: 8px; border: 1px solid #009a00; width: 238px; height: fit-content; cursor: pointer;\"><a style=\"color: #009a00;\" href=\"https:\/\/poiseddevelopers.com\/reality-tech\/contact-us\/\" target=\"_blank\" rel=\"noopener\">Let\u2019s connect and brainstorm!<\/a><\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":12,"featured_media":1091,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[26,17],"tags":[],"class_list":["post-1089","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","category-sharepoint-migration-services"],"acf":[],"_links":{"self":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts\/1089","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/comments?post=1089"}],"version-history":[{"count":36,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts\/1089\/revisions"}],"predecessor-version":[{"id":3522,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts\/1089\/revisions\/3522"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/media\/1091"}],"wp:attachment":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/media?parent=1089"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/categories?post=1089"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/tags?post=1089"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}