从WebRequestMethods.Ftp.ListDirectoryDe​​tails解压文件名文件名、Ftp、WebRequestMethods、tails

2023-09-04 03:36:54 作者:虎口險

我有一个执行以下操作的应用程序:目录列表,下载文件,下载所有。

我有一个问题,从WebRequestMethods.Ftp.ListDirectoryDe​​tails获取文件名。它似乎是不可能这样做的每个场景。

WebRequestMethods.Ftp.ListDirectoryDe​​tails以下述方式返回LINEITEM:

- RW-R - R-- 1 FTP FTP 39979 8月1日16:02分贝到电脑2014年8月5日07-30-00.csv

我使用的第一个字符,以确定它是否是一个文件或目录。然后我分裂的空间文件和分裂中的固定指标量后得到的文件名。在我执行的问题是,如果一个文件有多个空格,则它会被错误引用更少的空间,并试图下载该文件时就不会被发现。

我不能够使用split.last(),因为文件名可以有空格,也不WebRequestMethods.Ftp.ListDirectory,因为它不允许我们到一个目录和文件之间区分不带扩展名。也不因为文件名的正则表达式可以具有在它的日期。找到一个解决方案,完全覆盖所有的情况下,将是巨大的任何帮助。

 布尔isDirectory = line.Substring(0,1).Equals(D,System.StringComparison.OrdinalIgnoreCase);

字符串[]与itemNames = line.Split(新[] {'','\ T'},StringSplitOptions.RemoveEmptyEntries)。选择((价值指数)=>新建{值,指数})。其中(I =&GT ; i.index大于7)。选择(ⅰ=> i.value).ToArray();
字符串VAL =的string.join(,如果itemNames);
 

解决方案

最后的解决方案是使用正则表达式和使用组拆呢。这解决了所有的问题,让我得到的文件/目录名和它是否是一个目录或文件。

 字符串正则表达式=
@^+ //#开始行
@(?<目录> [\  -  LD])+ //#文件大小
(?<许可> [\  -  RWX] {9})@ + //#空白\ñ
@\ S ++ //#空白\ñ
@(小于?文件code取代; \ D +)+
@\ S ++ //#空白\ñ
@(小于?所有者GT; \ W +)+
@\ S ++ //#空白\ñ
@(小于?组> \ W +)+
@\ S ++ //#空白\ñ
@(LT;大小> \ D +)+
@\ S ++ //#空白\ñ
@(小于?一个月> \ w {3})+ //#月(3个字母)\ñ
@\ S ++ //#空白\ñ
@(小于?日> \ D {1,2})+ //#日(1或2位数字)\ñ
@\ S ++ //#空白\ñ
(?< timeyear> [\ D:] {4,5})@ + //#时间或年\ñ
@\ S ++ //#空白\ñ
@(LT;文件名>(*))+ //#文件名\ñ
@$; //#行末


VAR分裂=新的正则表达式(正则表达式).Match(线);
字符串DIR = split.Groups [目录]的ToString()。
。字符串文件名= split.Groups [文件名]的ToString();
布尔isDirectory = string.IsNullOrWhiteSpace(DIR)及!&安培; dir.Equals(D,StringComparison.OrdinalIgnoreCase);
 

由于:http://blogs.msdn.com/b/adarshk/archive/2004/09/15/sample-$c$c-for-parsing-ftpwebrequest-response-for-listdirectorydetails.aspx提供正则表达式。

I have an application that does the following: directory listing, download file, download all.

I have an issue with getting the file names from WebRequestMethods.Ftp.ListDirectoryDetails. It seems that it is not possible to do so for every scenario.

WebRequestMethods.Ftp.ListDirectoryDetails returns a lineItem in the following manner:

"-rw-r--r-- 1 ftp ftp 39979 Aug 01 16:02 db to pc 2014-08-05 07-30-00.csv"

I am using the first character to determine if it is a file or directory. Then I split the file on space and get the file name after a fixed index amount within the split. The issue in my implementation is that if a file has multiple spaces then it would be incorrectly referenced with less spaces and the file would not be found when attempting to download it.

I am not able use the split.last() since a file name can have spaces nor WebRequestMethods.Ftp.ListDirectory since it does not allow us to distinguish between a Directory and a file without an extension. Nor a regex since a file name can have a date in it. Any assistance in finding a solution that completely covers all cases would be great.

bool isDirectory = line.Substring(0,1).Equals("d", System.StringComparison.OrdinalIgnoreCase);

string[] itemNames = line.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries).Select((value, index) => new { value, index }).Where(i => i.index > 7).Select(i => i.value).ToArray();
string val = string.Join(" ", itemNames);

解决方案

The final solution was to use regex and split it using groups. This resolved all issues and allowed me to get the file / directory name and whether it was a directory or file.

string regex =
@"^" +                          //# Start of line
@"(?<dir>[\-ld])" +             //# File size          
@"(?<permission>[\-rwx]{9})" +  //# Whitespace          \n
@"\s+" +                        //# Whitespace          \n
@"(?<filecode>\d+)" +
@"\s+" +                        //# Whitespace          \n
@"(?<owner>\w+)" +
@"\s+" +                        //# Whitespace          \n
@"(?<group>\w+)" +
@"\s+" +                        //# Whitespace          \n
@"(?<size>\d+)" +
@"\s+" +                        //# Whitespace          \n
@"(?<month>\w{3})" +            //# Month (3 letters)   \n
@"\s+" +                        //# Whitespace          \n
@"(?<day>\d{1,2})" +            //# Day (1 or 2 digits) \n
@"\s+" +                        //# Whitespace          \n
@"(?<timeyear>[\d:]{4,5})" +    //# Time or year        \n
@"\s+" +                        //# Whitespace          \n
@"(?<filename>(.*))" +          //# Filename            \n
@"$";                           //# End of line


var split = new Regex(regex).Match(line);
string dir = split.Groups["dir"].ToString();
string filename = split.Groups["filename"].ToString();
bool isDirectory = !string.IsNullOrWhiteSpace(dir) && dir.Equals("d", StringComparison.OrdinalIgnoreCase);

Thanks to: http://blogs.msdn.com/b/adarshk/archive/2004/09/15/sample-code-for-parsing-ftpwebrequest-response-for-listdirectorydetails.aspx for providing the regex.