Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Proper format to read FTP directory
Message
From
10/05/2012 05:39:43
 
 
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
VB 9.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01543372
Message ID:
01543398
Views:
23
>>>>In my FTP class, I have two formats to read FTP directory content. Here are the two scenarios:
>>>>
>>>>
>>>>                    ' This is the standard
>>>>                    ' total 4696
>>>>                    ' drw-rw-rw-   1 user     group           0 Feb  6 15:46 .
>>>>                    ' drw-rw-rw-   1 user     group           0 Feb  6 15:46 ..
>>>>                    ' -rw-rw-rw-   1 user     group        4825 May  3  2010 Account.png
>>>>                    ' -rw-rw-rw-   1 user     group        5242 May  3  2010 Administration.png
>>>>                    ' -rw-rw-rw-   1 user     group        4117 Apr 17  2011 Agent.png
>>>>                    ' -rw-rw-rw-   1 user     group        1402 Dec 30  2005 AmazonCA.gif
>>>>                    ' -rw-rw-r--   1 wliclus  weblogic   308121 Jun  1  2011 tlv_01_jackk_2011615315_1185655_001_23327701.zip
>>>>
>>>>                    ' This is also another format we have to support
>>>>                    ' -r-xr-xr-x   1 owner    group        78145990 Nov  4  2011 101.zip
>>>>                    ' -r-xr-xr-x   1 owner    group           35492 Apr 14  2011 20110414.40E
>>>>                    ' -r-xr-xr-x   1 owner    group           33583 Apr 14  2011 20110414.421
>>>>                    ' -r-xr-xr-x   1 owner    group           40815 Apr 14  2011 20110414.483
>>>>                    ' -r-xr-xr-x   1 owner    group           24836 Apr 14  2011 20110414.ARC
>>>>                    ' -r-xr-xr-x   1 owner    group            4737 May  5  2011 4A16PBE1.txt
>>>>                    ' -r-xr-xr-x   1 owner    group            4737 Jan 18 13:50 4A17A7B1.txt
>>>>
>>>>
>>>>The first one is the one that is commonly used. On one specific server, I am receiving the directly listing in the second scenario.
>>>>
>>>>So far, I have this code which parse the following component:
>>>>
>>>>
>>>>                    ' If this is a file
>>>>                    If lcLine.StartsWith("-") Then
>>>>
>>>>                        ' Get the character at position 42
>>>>                        lcCharacter = Mid(lcLine, 42, 1)
>>>>
>>>>                        ' If this is a space
>>>>                        If lcCharacter = " " Then
>>>>                            lcFile = Mid(lcLine, 56)
>>>>                            lnSize = Val(Mid(lcLine, 34, 8))
>>>>                            lcDate = Mid(lcLine, 43, 12)
>>>>                        Else
>>>>                            lcFile = Mid(lcLine, 60)
>>>>                            lnSize = Val(Mid(lcLine, 34, 14))
>>>>                            lcDate = Mid(lcLine, 47, 12)
>>>>                        End If
>>>>
>>>>
>>>>So, I need to know if taking the file size from character 34 will always be good. This assume scenario #1. It takes the next 8 digits. As the second scenario starts reading the file size from character 34 but takes the next 14 digits.
>>>>
>>>>Scenario #1 would then allow to pick list file size up to 99,999,999 bytes, thus 99.999999 MB.
>>>>
>>>>This is based on the assumption that the directly listing returned by FTP servers always include a space between each component. Can someone confirms me if this is correct?
>>>>
>>>>Also, I would like to get the feedback on scenario #2 as to know if starting at character 34 would do as well or if that type of directly listing has been adjusted to support longer group which would then mean I would need to start reading the file size at character 38 instead of character 34 for this scenario.
>>>>
>>>>Any feedback on this issue would be appreciated.
>>>
>>>Gregory's regex is good. Or use string.Split(). Something like:
Imports System.Collections.Generic
>>>Imports System.Linq
>>>
>>>Namespace ConsoleApplication5
>>>	Class Program
>>>		Private Shared Sub Main(args As String())
>>>			Dim typeA As New List(Of String)() From { _
>>>				" drw-rw-rw-   1 user     group           0 Feb  6 15:46 .", _
>>>				" drw-rw-rw-   1 user     group           0 Feb  6 15:46 ..", _
>>>				" -rw-rw-rw-   1 user     group        4825 May  3  2010 Account.png", _
>>>				" -rw-rw-rw-   1 user     group        5242 May  3  2010 Administration.png", _
>>>				" -rw-rw-rw-   1 user     group        4117 Apr 17  2011 Agent.png", _
>>>				" -rw-rw-rw-   1 user     group        1402 Dec 30  2005 AmazonCA.gif", _
>>>				" -rw-rw-r--   1 wliclus  weblogic   308121 Jun  1  2011 tlv_01_jackk_2011615315_1185655_001_23327701.zip", _
>>>				" -r-xr-xr-x   1 owner    group        78145990 Nov  4  2011 101.zip", _
>>>				" -r-xr-xr-x   1 owner    group           35492 Apr 14  2011 20110414.40E", _
>>>				" -r-xr-xr-x   1 owner    group           33583 Apr 14  2011 20110414.421", _
>>>				" -r-xr-xr-x   1 owner    group           40815 Apr 14  2011 20110414.483", _
>>>				" -r-xr-xr-x   1 owner    group           24836 Apr 14  2011 20110414.ARC", _
>>>				" -r-xr-xr-x   1 owner    group            4737 May  5  2011 4A16PBE1.txt", _
>>>				" -r-xr-xr-x   1 owner    group            4737 Jan 18 13:50 4A17A7B1.txt" _
>>>			}
>>>
>>>			Dim result As IEnumerable(Of FTPInfo) = typeA.[Select](Function(x) 
>>>			Dim line As String() = DirectCast(x, String).Split(DirectCast(Nothing, Char()), StringSplitOptions.RemoveEmptyEntries)
>>>			Return New FTPInfo() With { _
>>>				Key .File = line(8), _
>>>				Key .Size = line(4), _
>>>				Key .Month = line(5), _
>>>				Key .Day = line(6), _
>>>				Key .YearOrTime = line(7) _
>>>			}
>>>
>>>End Function)
>>>		End Sub
>>>	End Class
>>>	Public Structure FTPInfo
>>>		Public File As String
>>>		Public Size As String
>>>		Public Month As String
>>>		Public Day As String
>>>		Public YearOrTime As String
>>>	End Structure
>>>End Namespace
Disclaimer: VB auto-converted from C#.....
>>
>>
>>
>>It just occurred to me that file names can contain spaces
>>
>>So, I modified the pattern
>>
>>string pattern = @"^(?<Permissions>\S+)\s+(?<Links>\d+)\s+(?<User>\w+)\s+(?<Group>\w+)\s+(?<FileSize>\d+)\s+(?<Month>\w+)\s+(?<Day>\d+)\s+((?<Year>\d{4})|(?<Time>\d+:\d+(:\d+)?))\s+(?<FileName>.+)$";
>>
>>
>>It now parses the last line correctly
>>
>>string[] lines =
>>			{	@"drw-rw-rw-   1 user     group           0 Feb  6 15:46 .",
>>                @"drw-rw-rw-   1 user     group           0 Feb  6 15:46 ..",
>>                @"-rw-rw-rw-   1 user     group        4825 May  3  2010 Account.png",
>>                @"-rw-rw-rw-   1 user     group        5242 May  3  2010 Administration.png",
>>                @"-rw-rw-rw-   1 user     group        4117 Apr 17  2011 Agent.png",
>>                @"-rw-rw-rw-   1 user     group        1402 Dec 30  2005 AmazonCA.gif",
>>                @"-rw-rw-r--   1 wliclus  weblogic   308121 Jun  1  2011 tlv_01_jackk_2011615315_1185655_001_23327701.zip",
>>				@"-r-xr-xr-x   1 owner    group        78145990 Nov  4  2011 101.zip",
>>                @"-r-xr-xr-x   1 owner    group           35492 Apr 14  2011 20110414.40E",
>>                @"-r-xr-xr-x   1 owner    group           33583 Apr 14  2011 20110414.421",
>>                @"-r-xr-xr-x   1 owner    group           40815 Apr 14  2011 20110414.483",
>>                @"-r-xr-xr-x   1 owner    group           24836 Apr 14  2011 20110414.ARC",
>>                @"-r-xr-xr-x   1 owner    group            4737 May  5  2011 4A16PBE1.txt",
>>                @"-r-xr-xr-x   1 owner    group            4737 Jan 18 13:50 4A17A7B1.txt",
>>	  @"dr-xr-xr-x   1 owner    group            0 Jan 18 13:50 Program Files or something else"
>>			};
>>
>>
>>
>>
>>How would yo do that with string split ? (gdr)
>
>Spoilsport ! OK, just to prove it's not impossible:
           IEnumerable<FTPInfo> result = typeA.Select(x =>
>            {
>                string[] line = ((string)x).Split((char[])null, StringSplitOptions.RemoveEmptyEntries);
>                string filename = string.Empty;
>                line.Skip(8).Select(xx => filename = filename + " " + xx).Last();
>                return new FTPInfo
>                {
>                    File = filename,
>                    Size = line[4],
>                    Month = line[5],
>                    Day = line[6],
>                    YearOrTime = line[7]
>                };
>            });
I see two little problems remain - sorry

(1) There is an extra space before the filename
(2) What do you do when there are consecutive or trailing spaces in the filename, eg
@"dr-xr-xr-x   1 owner    group            0 Jan 18 13:50 Program Files or        something else         "
Gregory
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform