-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFtpDirectory.vb
93 lines (78 loc) · 3.01 KB
/
FtpDirectory.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
Imports System.Collections.Generic
Imports System.Net
Imports System.IO
Imports System.Text.RegularExpressions
Namespace Utilities.FTP
#Region "FTP Directory class"
''' <summary>
''' Stores a list of files and directories from an FTP result
''' </summary>
''' <remarks></remarks>
Public Class FTPdirectory
Inherits List(Of FTPfileInfo)
Sub New()
'creates a blank directory listing
End Sub
''' <summary>
''' Constructor: create list from a (detailed) directory string
''' </summary>
''' <param name="dir">directory listing string</param>
''' <param name="path"></param>
''' <remarks></remarks>
Sub New(ByVal dir As String, ByVal path As String)
For Each line As String In dir.Replace(vbLf, "").Split(CChar(vbCr))
'parse
If line <> "" Then Me.Add(New FTPfileInfo(line, path))
Next
End Sub
''' <summary>
''' Filter out only files from directory listing
''' </summary>
''' <param name="ext">optional file extension filter</param>
''' <returns>FTPdirectory listing</returns>
Public Function GetFiles(Optional ByVal ext As String = "") As FTPdirectory
Return Me.GetFileOrDir(FTPfileInfo.DirectoryEntryTypes.File, ext)
End Function
''' <summary>
''' Returns a list of only subdirectories
''' </summary>
''' <returns>FTPDirectory list</returns>
''' <remarks></remarks>
Public Function GetDirectories() As FTPdirectory
Return Me.GetFileOrDir(FTPfileInfo.DirectoryEntryTypes.Directory)
End Function
'internal: share use function for GetDirectories/Files
Private Function GetFileOrDir(ByVal type As FTPfileInfo.DirectoryEntryTypes, Optional ByVal ext As String = "") As FTPdirectory
Dim result As New FTPdirectory()
For Each fi As FTPfileInfo In Me
If fi.FileType = type Then
If ext = "" Then
result.Add(fi)
ElseIf ext = fi.Extension Then
result.Add(fi)
End If
End If
Next
Return result
End Function
Public Function FileExists(ByVal filename As String) As Boolean
For Each ftpfile As FTPfileInfo In Me
If ftpfile.Filename = filename Then
Return True
End If
Next
Return False
End Function
Private Const slash As Char = "/"
Public Shared Function GetParentDirectory(ByVal dir As String) As String
Dim tmp As String = dir.TrimEnd(slash)
Dim i As Integer = tmp.LastIndexOf(slash)
If i > 0 Then
Return tmp.Substring(0, i - 1)
Else
Throw New ApplicationException("No parent for root")
End If
End Function
End Class
#End Region
End Namespace