views:

226

answers:

3

Hi, 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.

A: 
^(\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
Regex regex = new Regex(@"^(\d{2}-\d{2}-\d{2}\s*\d{2}:\d{2}(A|P)M)\s*(<DIR>){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
A: 

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
A: 

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.
$4=type (could be null),here seems to be problem. In dir case I am getting '>'Please recheck it.
marshalprince
ok maybe c# needs ([\w<>]+)*, my intention was: maybe there could be something different than "<DIR>", so iff only <DIR> appears or not replace it by (<DIR>)? (?->optional means zero or one)
Carsten C.