I need some help with parsing the response from ListDirectoryDetails
in C#.
I only need the following fields.
- File Name/Directory Name
- Date Created
- and the File Size.
Here's what some of the lines look like when I run ListDirectoryDetails
:
d--x--x--x 2 ftp ftp 4096 Mar 07 2002 bin
-rw-r--r-- 1 ftp ftp 659450 Jun 15 05:07 TEST.TXT
-rw-r--r-- 1 ftp ftp 101786380 Sep 08 2008 TEST03-05.TXT
drwxrwxr-x 2 ftp ftp 4096 May 06 12:24 dropoff
Thanks in advance.
Building on the regex idea of Ryan Conrad, this is my final reading code:
And the FtpFileInfo class that's filled:
Not sure if you still need this, but this is the solution i came up with:
Match Groups:
For this specific listing, the following code will do:
You will get (as of year 2016):
But, actually trying to parse the listing returned by the
ListDirectoryDetails
is not the right way to go.You want to use an FTP client that supports the modern
MLSD
command that returns a directory listing in a machine-readable format specified in the RFC 3659. Parsing the human-readable format returned by the ancientLIST
command (used internally by theFtpWebRequest
for itsListDirectoryDetails
method) should be used as the last resort option, when talking to obsolete FTP servers, that do not support theMLSD
command (like the Microsoft IIS FTP server).Many servers use a different format for the
LIST
command response. Particularly IIS can use DOS format. See C# class to parse WebRequestMethods.Ftp.ListDirectoryDetails FTP response.For example with WinSCP .NET assembly, you can use its
Session.ListDirectory
orSession.EnumerateRemoteFiles
methods.They internally use the
MLSD
command, but can fall back to theLIST
command and support dozens of different human-readable listing formats.The returned listing is presented as collection of
RemoteFileInfo
instances with properties like:Name
LastWriteTime
(with correct timezone)Length
FilePermissions
(parsed into individual rights)Group
Owner
IsDirectory
IsParentDirectory
IsThisDirectory
(I'm the author of WinSCP)
Most other 3rd party libraries will do the same. Using the
FtpWebRequest
class is not reliable for this purpose. Unfortunately, there's no other built-in FTP client in the .NET framework.This is my algorithm to get the File/Dir name, Date Created, Attribute(File/Dir), Size. Hope this helps...