我可以看到有两种类型的回复:
示例
"08-25-12 06:52AM 139874418 3.03.06P13.12NB.rar"
"-r-xr-xr-x 1 owner group 1 Jun 3 1999 NotCurrentYear.txt"
我需要解析它并使用以下逻辑:
AnalyzedFolder folderToBeAnalyzed = new AnalyzedFolder();
folderToBeAnalyzed.Name = folder;
Job.AnalyzedFolders.Add(folderToBeAnalyzed);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(textBoxFTPSite.Text + folder);
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
request.Credentials = new NetworkCredential(textBoxFTPUserName.Text, textBoxFTPPassword.Text);
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
string[] outputlines = reader.ReadToEnd().Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
foreach (string info in outputlines)
{
var tokens = info.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
string name;
string type;
string size;
DateTime dateModified;
string lsLine;
if (tokens.Length == 4) //WINDOWS
{
name = tokens[3];
if (tokens[2] == "<DIR>")
{
type = "D";
size = "";
}
else
{
type = "F";
size = tokens[2];
}
dateModified = DateTime.ParseExact(tokens[0] + " " + tokens[1], "MM-dd-yy h:mmtt", CultureInfo.InvariantCulture);
lsLine = info;
FTPFolderEntity entity = new FTPFolderEntity() { FolderName = folder, Name = name, Type = type, Size = size, DateModified = dateModified, LSLine = lsLine };
folderToBeAnalyzed.Entities.Add(entity);
}
else //UNIX
{
}
}
问题在于此文件:
"11-15-12 10:02PM 324 Copy (10) of 1040.txt.zip"
由于空格,逻辑失败。此外,像这个错误,我怀疑我也可能遇到其他问题。有人可以指导我找一个更好的解析方法吗?
答案 0 :(得分:0)
您可以在此处使用正则表达式删除额外的 Whitesapces :
string info = "11-15-12 10:02PM 324 Copy (10) of 1040.txt.zip";
string result = Regex.Replace(info, @"\s\s+", " ");
之后,您将获得result
// result = "11-15-12 10:02PM 324 Copy (10) of 1040.txt.zip";
已添加,如果您想限制令牌,假设第一个始终是日期,第二个是时间,休息是您的文件名或其他内容:
var tokens = Regex.Split(info, @"\s+");
var newTokens = new string[]
{
tokens[0],
tokens[1],
tokens[2],
tokens[3] + ' ' + tokens[4] + ' ' + tokens[5] + ' ' + tokens[6]
};
答案 1 :(得分:0)
您可以使用Regex进行拆分。
var tokens = Regex.Split(info, @"\s+");