-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFtpFile.vb
167 lines (155 loc) · 6.09 KB
/
FtpFile.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
Imports System.Collections.Generic
Imports System.Net
Imports System.IO
Imports System.Text.RegularExpressions
Namespace Utilities.FTP
#Region "FTP file info class"
''' <summary>
''' Represents a file or directory entry from an FTP listing
''' </summary>
''' <remarks>
''' This class is used to parse the results from a detailed
''' directory list from FTP. It supports most formats of
'''
''' v1.1 fixed bug in Fullname/path
''' </remarks>
Public Class FTPfileInfo
'Stores extended info about FTP file
#Region "Properties"
Public ReadOnly Property FullName() As String
Get
Return Path & Filename
End Get
End Property
Public ReadOnly Property Filename() As String
Get
Return _filename
End Get
End Property
''' <summary>
''' Path of the file (always ends in /)
''' </summary>
''' <remarks>
''' 1.1: Modifed to ensure always ends in / - with thanks to jfransella for pointing this out
''' </remarks>
Public ReadOnly Property Path() As String
Get
Return _path & IIf(_path.EndsWith("/"), "", "/")
End Get
End Property
Public ReadOnly Property FileType() As DirectoryEntryTypes
Get
Return _fileType
End Get
End Property
Public ReadOnly Property Size() As Long
Get
Return _size
End Get
End Property
Public Property FileDateTime() As Date
Get
Return _fileDateTime
End Get
Friend Set(ByVal value As Date)
_fileDateTime = value
End Set
End Property
Public ReadOnly Property Permission() As String
Get
Return _permission
End Get
End Property
Public ReadOnly Property Extension() As String
Get
Dim i As Integer = Me.Filename.LastIndexOf(".")
If i >= 0 And i < (Me.Filename.Length - 1) Then
Return Me.Filename.Substring(i + 1)
Else
Return ""
End If
End Get
End Property
Public ReadOnly Property NameOnly() As String
Get
Dim i As Integer = Me.Filename.LastIndexOf(".")
If i > 0 Then
Return Me.Filename.Substring(0, i)
Else
Return Me.Filename
End If
End Get
End Property
Private _filename As String
Private _path As String
Private _fileType As DirectoryEntryTypes
Private _size As Long
Private _fileDateTime As Date
Private _permission As String
#End Region
''' <summary>
''' Identifies entry as either File or Directory
''' </summary>
Public Enum DirectoryEntryTypes
File
Directory
End Enum
''' <summary>
''' Constructor taking a directory listing line and path
''' </summary>
''' <param name="line">The line returned from the detailed directory list</param>
''' <param name="path">Path of the directory</param>
''' <remarks></remarks>
Sub New(ByVal line As String, ByVal path As String)
'parse line
Dim m As Match = GetMatchingRegex(line)
If m Is Nothing Then
'failed
Throw New ApplicationException("Unable to parse line: " & line)
Else
_filename = m.Groups("name").Value
_path = path
_size = CLng(m.Groups("size").Value)
_permission = m.Groups("permission").Value
Dim _dir As String = m.Groups("dir").Value
If (_dir <> "" And _dir <> "-") Then
_fileType = DirectoryEntryTypes.Directory
Else
_fileType = DirectoryEntryTypes.File
End If
Try
_fileDateTime = Date.Parse(m.Groups("timestamp").Value)
Catch ex As Exception
_fileDateTime = Nothing
End Try
End If
End Sub
Private Function GetMatchingRegex(ByVal line As String) As Match
Dim rx As Regex, m As Match
For i As Integer = 0 To _ParseFormats.Length - 1
rx = New Regex(_ParseFormats(i))
m = rx.Match(line)
If m.Success Then Return m
Next
Return Nothing
End Function
#Region "Regular expressions for parsing LIST results"
''' <summary>
''' List of REGEX formats for different FTP server listing formats
''' </summary>
''' <remarks>
''' The first three are various UNIX/LINUX formats, fourth is for MS FTP
''' in detailed mode and the last for MS FTP in 'DOS' mode.
''' I wish VB.NET had support for Const arrays like C# but there you go
''' </remarks>
Private Shared _ParseFormats As String() = { _
"(?<dir>[\-d])(?<permission>([\-r][\-w][\-xs]){3})\s+\d+\s+\w+\s+\w+\s+(?<size>\d+)\s+(?<timestamp>\w+\s+\d+\s+\d{4})\s+(?<name>.+)", _
"(?<dir>[\-d])(?<permission>([\-r][\-w][\-xs]){3})\s+\d+\s+\d+\s+(?<size>\d+)\s+(?<timestamp>\w+\s+\d+\s+\d{4})\s+(?<name>.+)", _
"(?<dir>[\-d])(?<permission>([\-r][\-w][\-xs]){3})\s+\d+\s+\d+\s+(?<size>\d+)\s+(?<timestamp>\w+\s+\d+\s+\d{1,2}:\d{2})\s+(?<name>.+)", _
"(?<dir>[\-d])(?<permission>([\-r][\-w][\-xs]){3})\s+\d+\s+\w+\s+\w+\s+(?<size>\d+)\s+(?<timestamp>\w+\s+\d+\s+\d{1,2}:\d{2})\s+(?<name>.+)", _
"(?<dir>[\-d])(?<permission>([\-r][\-w][\-xs]){3})(\s+)(?<size>(\d+))(\s+)(?<ctbit>(\w+\s\w+))(\s+)(?<size2>(\d+))\s+(?<timestamp>\w+\s+\d+\s+\d{2}:\d{2})\s+(?<name>.+)", _
"(?<timestamp>\d{2}\-\d{2}\-\d{2}\s+\d{2}:\d{2}[Aa|Pp][mM])\s+(?<dir>\<\w+\>){0,1}(?<size>\d+){0,1}\s+(?<name>.+)"}
#End Region
End Class
#End Region
End Namespace