FTP list result

Hi,

I'm retrieving a detailed list of files into a string array via FTP. Each item in the array looks something like this

"drwxr-x--- 3 vincent vincent 4096 Jul 12 12:16 public_ftp".

What would be the easiest way to break this info down and put it into the relevent columns on a listview control(name, date, attibutes etc). I'm really stuck on where to start.

appreciate any help

Chris



Answer this question

FTP list result

  • Kenshin1591

    I don't agree with Sergey Galich, because the results aren't a fixed length.


  • Ice2Fire

    Try parse from the front & back, cutting from string part by part. The last one will be file name.

  • dickdunbar

    Hi,

    Thanks a lot for your help, your code has been very usefull to me. I was wandering how you deal with getting files that contain spaces in the file name

    cheers,

    Chris


  • Flippy_TK

    We don't, but you know how many whitespaces there are after the filename. So you should find the last whitespace that can indicate a filename.


  • NicolasP

    Here are some methods we use:


    public class ServerFileData
    {
    public bool isDirectory;
    public string fileName;
    public int size;
    public string type;
    public string date;
    public string permission;
    public string owner;
    public string group;

    public ServerFileData()
    {
    }
    }

    private ServerFileData ParseDosDirLine(string line)
    {
    ServerFileData sfd = new ServerFileData();

    try
    {
    string[] parsed = new string[3];
    int index = 0;
    int position = 0;

    // Parse out the elements
    position = line.IndexOf(' ');
    while (index<parsed.Length)
    {
    parsed[index] = line.Substring(0, position);
    line = line.Substring(position);
    line = line.Trim();
    index++;
    position = line.IndexOf(' ');
    }
    sfd.fileName = line;

    if (parsed[2] != "<DIR>")
    sfd.size = Convert.ToInt32(parsed[2]);

    sfd.date = parsed[0]+ ' ' + parsed[1];
    sfd.isDirectory = parsed[2] == "<DIR>";
    }
    catch
    {
    sfd = null;
    }
    return sfd;
    }

    private ServerFileData ParseUnixDirLine(string line)
    {
    ServerFileData sfd = new ServerFileData();

    try
    {
    string[] parsed = new string Music;
    int index = 0;
    int position;

    // Parse out the elements
    position = line.IndexOf(' ');
    while (index<parsed.Length)
    {
    parsed[index] = line.Substring(0, position);
    line = line.Substring(position);
    line = line.Trim();
    index++;
    position = line.IndexOf(' ');
    }
    sfd.fileName = line;

    sfd.permission = parsed[0];
    sfd.owner = parsed[2];
    sfd.group = parsed[3];
    sfd.size = Convert.ToInt32(parsed[4]);
    sfd.date = parsed[5]+ ' ' + parsedDevil + ' ' + parsed[7];
    sfd.isDirectory = sfd.permission[0] == 'd';
    }
    catch
    {
    sfd = null;
    }
    return sfd;
    }

    private void ParseUnixDirList(string sDir)
    {
    const string CRLF = "\r\n";

    try
    {
    m_filecount = 0;
    int i = 0;
    sDir = sDir.Replace (CRLF, "\r");
    string[] sFile = sDir.Split(new Char[]{'\r'});
    ServerFileData sfd = null;
    int autodetect = 0;

    foreach (string fileLine in sFile)
    {
    if (autodetect == 0)
    {
    sfd = ParseDosDirLine(fileLine);
    if (sfd == null)
    {
    sfd = ParseUnixDirLine(fileLine);
    autodetect = 2;
    }
    else
    autodetect = 1;
    }
    else
    if (autodetect == 1)
    sfd = ParseDosDirLine(fileLine);
    else
    if (autodetect == 2)
    sfd = ParseUnixDirLine(fileLine);

    if (sfd != null)
    {
    m_filesIdea = sfd;
    i++;
    m_filecount = i;
    }
    }
    }
    catch (Exception e)
    {

    }
    }




  • ssmorgan

    Hi!

    It looks like you have fixed length columns in line. So easiest way may be to SubString() from line.



  • FTP list result