Last active
February 12, 2019 00:26
-
-
Save Adobe-Android/6656c39ba50b503619f0a8dd4cdf51dc to your computer and use it in GitHub Desktop.
C#-FTP - Connect to a server over FTPS, get a list of files in a specified directory, and rename those files
This file contains hidden or 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
<?xml version="1.0" encoding="utf-8" ?> | |
<configuration> | |
<startup> | |
<!--Replace with your .NET version here--> | |
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" /> | |
</startup> | |
<appSettings> | |
<add key="FilePath" value="/sourcePath/"/> | |
<add key="DestinationPath" value="/destinationPath/"/> | |
<add key="UserName" value="user"/> | |
<add key="Password" value="*****"/> | |
</appSettings> | |
</configuration> |
This file contains hidden or 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
using System; | |
using System.Collections.Generic; | |
using System.Configuration; | |
using System.IO; | |
using System.Net; | |
namespace FtpsListFiles | |
{ | |
class Program | |
{ | |
static string UserName = ConfigurationManager.AppSettings["UserName"].ToString(); | |
static string Password = ConfigurationManager.AppSettings["Password"].ToString(); | |
static void Main(string[] args) | |
{ | |
string filePath = ConfigurationManager.AppSettings["FilePath"].ToString(); | |
string[] servers = new string[] { "hostname" }; | |
bool ContinueProgram = true; | |
List<string> fileList = new List<string>(); | |
foreach (var server in servers) | |
{ | |
if (ContinueProgram) | |
{ | |
ContinueProgram = GetFilesList(server, filePath, fileList); | |
} | |
} | |
} | |
private static bool GetFilesList(string server, string filePath, List<string> fileList) | |
{ | |
bool _results = true; | |
string filesString = ""; | |
try | |
{ | |
string ftpString = $@"ftp://{server}{filePath}"; | |
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpString); | |
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails; | |
request.Credentials = new NetworkCredential(UserName, Password); | |
request.EnableSsl = true; | |
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true; | |
ServicePointManager.Expect100Continue = false; | |
FtpWebResponse response = (FtpWebResponse)request.GetResponse(); | |
Stream responseStream = response.GetResponseStream(); | |
StreamReader reader = new StreamReader(responseStream); | |
var line = reader.ReadToEnd(); | |
filesString = line.Replace("\r\n", "*").Replace("\n", "*").Replace("\r", "*"); | |
string[] arrayFileList = filesString.Split('*'); | |
ParseFilesList(filesList, arrayFileList); | |
Console.WriteLine("Directory List Complete, status {0}", response.StatusDescription); | |
reader.Close(); | |
response.Close(); | |
RenameFiles(server, filePath, ftpString, filesList); | |
} | |
catch (Exception ex) | |
{ | |
_results = false; | |
} | |
return _results; | |
} | |
private static void ParseFilesList(List<string> filesList, string[] arrayFileList) | |
{ | |
foreach (var file in arrayFileList) | |
{ | |
if (!string.IsNullOrEmpty(file)) | |
{ | |
if (file.Substring(file.LastIndexOf(" ") + 1) != "is.done") | |
{ | |
if (Convert.ToDateTime(file.Substring(0, file.IndexOf("M") + 1)) > DateTime.Now.AddMonths(-2)) | |
{ | |
var fileName = file.Substring(file.LastIndexOf(" ") + 1); | |
filesList.Add(fileName); | |
} | |
} | |
} | |
} | |
} | |
private static void RenameFiles(string server, string filePath, string ftpString, List<string> filesList) | |
{ | |
try | |
{ | |
foreach (var file in filesList) | |
{ | |
Console.WriteLine(filePath + server); | |
var ftpFileString = $"{ftpString}{file}"; | |
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpFileString); | |
request.Method = WebRequestMethods.Ftp.Rename; | |
request.RenameTo = $"test-{file}"; | |
request.Credentials = new NetworkCredential(UserName, Password); | |
request.EnableSsl = true; | |
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true; | |
ServicePointManager.Expect100Continue = false; | |
FtpWebResponse response = (FtpWebResponse)request.GetResponse(); | |
response.Close(); | |
} | |
Console.WriteLine("Success! All files were renamed"); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine($"Error! Failed to rename files {ex}"); | |
throw ex; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment