0

I am trying to connect to FTP server through the c# code and I am getting the list of Files and directories. And that I am saving in a ArrayList(with all attributes). I can find the FTP Server type through the SYS ftp command. I have a regular expression for UNIX based files to parse the file\directories attributes. But I have no expression for Windows FTP server files parsing. I need help in making that..

04-30-09  10:40AM       <DIR>          Acrobat
12-08-09  10:36PM                 9058 AuthCheck.zip
12-06-09  12:49PM                  174 desktop.ini
11-09-09  03:33PM       <DIR>          FailedPDF

I need to parse these. Date, Time, Dir\File, Name of the file

Please help. Thanks.

marshalprince
  • 105
  • 4
  • 11

3 Answers3

1

I don't know much about C#, but if you just need a RegEx try this one:

^(\d\d-\d\d-\d\d)\s+(\d\d:\d\d(AM|PM))\s+([\w<>]*)\s+(\d*)\s+([\w\._\-]+)\s*$

$1 = date, $2=time, $3=am or pm, $4=type (could be null), $5=size(null if dir), $6=name

or iff $4 is only or empty

^(\d\d-\d\d-\d\d)\s+(\d\d:\d\d(AM|PM))\s+(<DIR>)?\s+(\d*)\s+([\w\._\-]+)\s*$

I suppose that "<" and ">" are no special chars at c#

Carsten C.
  • 211
  • 1
  • 7
  • $4=type (could be null), here seems to be problem. In dir case I am getting '>' Please recheck it. – marshalprince Dec 16 '09 at 07:28
  • 1
    ok maybe c# needs ([\w<>]+)*, my intention was: maybe there could be something different than "", so iff only appears or not replace it by ()? (?->optional means zero or one) – Carsten C. Dec 16 '09 at 09:33
0
^(\d{2}-\d{2}-\d{2}\s*\d{2}:\d{2}(A|P)M)\s*(<DIR>){0,1}\s*(\d*)\s*(\w)\s*$

Capture groups: 0: datetime 1: Is dir 2: FileSize (or null for directory) 3: Name

I dont have a compiler handy so I cant test that, but it should be close enough to get started.

GrayWizardx
  • 19,561
  • 2
  • 30
  • 43
  • Regex regex = new Regex(@"^(\d{2}-\d{2}-\d{2}\s*\d{2}:\d{2}(A|P)M)\s*(){0,1}\s*(\d*)\s*(\w)\s*$", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace); Match match = regex.Match(filedetail); here match returns null. – marshalprince Dec 16 '09 at 07:11
0

It looks like the lines have fixed structure. So you could simply do this:

Date fileDate;
bool isDir;
int fileSize;
string fileName;

fileDate=line.Substring(0,18).ParseExact("MM-dd-yy  hh-sstt");
isDir=line.Substring(24,5)=="DIR";
if (!isDir)
{
    fileSize=int.Parse(line.Substring(29,10).Trim());
}
fileName=line.Substring(39);
yu_sha
  • 4,290
  • 22
  • 19