Parsing File details for Windows FTP Server
-
19-09-2019 - |
Question
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.
Solution
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#
OTHER TIPS
^(\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.
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);