0

I can see that there are 2 types of responses:

  1. Windows
  2. Unix

Examples

"08-25-12  06:52AM            139874418 3.03.06P13.12NB.rar"  
"-r-xr-xr-x   1 owner    group               1 Jun  3  1999 NotCurrentYear.txt"  

I need to parse it and I used the following logic:

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
                    {
                    }
                }

The problem is that for this file:

"11-15-12  10:02PM                  324 Copy (10) of 1040.txt.zip"

because of the spaces, the logic fails. Also, like this bug, I suspect I may run into other problems also. Can anyone guide me for a better parsing method please ?

Akshay J
  • 5,362
  • 13
  • 68
  • 105

2 Answers2

0

You can use a Regular Expression here to remove extra Whitesapces:

string info = "11-15-12  10:02PM                  324 Copy (10) of 1040.txt.zip";
string result = Regex.Replace(info, @"\s\s+", " ");

After that you will get result as

// result = "11-15-12 10:02PM 324 Copy (10) of 1040.txt.zip";

ADDED, If you want to limit your tokens assuming the first is always the date, second is the time, and rest is your fileName or something:

var tokens = Regex.Split(info, @"\s+");

var newTokens = new string[] 
{ 
    tokens[0], 
    tokens[1], 
    tokens[2],
    tokens[3] + ' ' + tokens[4] + ' ' + tokens[5] + ' ' + tokens[6]
};
Furqan Safdar
  • 16,260
  • 13
  • 59
  • 93
0

You could do a split using a Regex.

var tokens = Regex.Split(info, @"\s+");
Garett
  • 16,632
  • 5
  • 55
  • 63