Parse response from FTP LIST command (syntax varia

2019-02-14 23:58发布

问题:

The FTP LIST command displays a listing of all the files and directories in the current working directory. The problem is, it returns several different formats depending on the server. Does anybody know of a .NET library that is able to parse the most popular formats? I am OK with a "try this regex, if it fails, try the next regex" approach.

回答1:

Here's the one that I've been using for a FileZilla server:

^(?<dir>[\-ld])(?<permission>([\-r][\-w][\-xs]){3})\s+(?<filecode>\d+)\s+(?<owner>\w+)\s+(?<group>\w+)\s+(?<size>\d+)\s+(?<timestamp>((?<month>\w{3})\s+(?<day>\d{2})\s+(?<hour>\d{1,2}):(?<minute>\d{2}))|((?<month>\w{3})\s+(?<day>\d{1,2})\s+(?<year>\d{4})))\s+(?<name>.+)$

http://chrishaas.wordpress.com/2009/06/10/regex-for-parsing-ftp-list-command/



回答2:

Here is a RegEx I used on a project. Seems to work for both Windows and Unix based FTP servers. Someone might be able to clean it up but I build it by concatenating a bunch of properties on a class. So it's not as brutal to maintain for me.

^((?<DIR>([dD]{1}))|)(?<ATTRIBS>(.*))\s(?<SIZE>([0-9]{1,}))\s(?<DATE>((?<MONTHDAY>((?<MONTH>(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec))\s(?<DAY>([0-9\s]{2}))))\s(\s(?<YEAR>([0-9]{4}))|(?<TIME>([0-9]{2}\:[0-9]{2})))))\s(?<NAME>([A-Za-z0-9\-\._\s]{1,}))$


回答3:

^(?<dir>[\-ld])(?<permission>([\-r][\-w][\-xs]){3})\s+(?<filecode>\d+)\s+(?<owner>\w+)\s+(?<group>\w+)\s+(?<size>\d+)\s+(?<timestamp>((?<month>\w{3})\s+(?<day>\d{1,2})\s+(?<hour>\d{1,2}):(?<minute>\d{2}))|((?<month>\w{3})\s+(?<day>\d{1,2})\s+(?<year>\d{4})))\s+(?<name>.+)$

Changed Chris Haas version a tiny little bit. Changed so that the day grouping can consist of a single number as well. \d{2} -> \d{1,2}

Thanks for original version.



回答4:

^(?<dir>[\-ld])(?<permission>([\-r][\-w][\-xs]){3})\s+(?<filecode>\d+)\s+(?<owner>\S+)\s+(?<group>\S+)\s+(?<size>\d+)\s+(?<timestamp>((?<month>\w{3})\s+(?<day>\d{1,2})\s+(?<hour>\d{1,2}):(?<minute>\d{2}))|((?<month>\w{3})\s+(?<day>\d{1,2})\s+(?<year>\d{4})))\s+(?<name>.+)$

On the site I'm on, the owner is displayed in Email address format. I changed owner and group to be non-space characters instead of word characters.

This is expanding on Yodiz' version of Chris Haas' version. Thanks so much!



标签: .net ftp