Guide Contents
|
DigitalDan Home Page
|
Public Structure ListFilesError
Dim ListFiles As List(Of String)
Dim ErrorMessage As String>br />
End Structure
Private Shared Function ListFiles(folder As String) As ListFilesError
' Top Level Only
' initialise results
Dim ret As ListFilesError
ret.ListFiles = New List(Of String)
ret.ErrorMessage = ""
' get list of files in folder
Try
ret.ListFiles.AddRange(IO.Directory.GetFiles(folder, "*", IO.SearchOption.TopDirectoryOnly))
Catch ex As Exception
ret.ErrorMessage = ex.Message
ret.ListFiles.Clear()
End Try
Return ret
End Function
Private Shared Function ListFiles1(folder As String, Optional IgnoreSystemFiles As Boolean = True) As ListFilesError
' Include nested folders
Dim listFoldersTemp As New List(Of String) From {folder}
' initialise results
Dim ret As ListFilesError
ret.ListFiles = New List(Of String)
ret.ErrorMessage = ""
While listFoldersTemp.Count > 1
Try
If (Not IgnoreSystemFiles) OrElse (Not IsSystemFile(listFoldersTemp.Item(0))) Then
listFoldersTemp.AddRange(IO.Directory.GetDirectories(listFoldersTemp.Item(0)))
If IgnoreSystemFiles Then
For Each filename As String In IO.Directory.GetFiles(listFoldersTemp.Item(0))
If Not IsSystemFile(filename) Then
ret.ListFiles.Add(filename)
End If
Next
Else
ret.ListFiles.AddRange(IO.Directory.GetFiles(listFoldersTemp.Item(0)))
End If
End If
listFoldersTemp.RemoveAt(0)
Catch ex As Exception
If ex.Message.ToLower.Contains("access to the path") Then
Continue While
Else
ret.ErrorMessage = ex.Message
ret.ListFiles.Clear()
Exit While
End If
End Try
End While
Return ret
End Function
Private Shared Function IsSystemFile(file As String) As Boolean
Try
If file.Contains("$"c) OrElse file.Contains("~"c) Then Return True
If IO.File.GetAttributes(file) = (IO.FileAttributes.Hidden) Then Return True
If IO.File.GetAttributes(file) = (IO.FileAttributes.Offline) Then Return True
If IO.File.GetAttributes(file) = (IO.FileAttributes.System) Then Return True
If IO.File.GetAttributes(file) = (IO.FileAttributes.Temporary) Then Return True
Catch ex As Exception
Return True ' file probably inaccessible
End Try
Return False
End Function
Append Data = IO.File.AppendAllText(oldFile, text)
Delete File = IO.File.Delete(oldFile)
Copy File = IO.File.Copy(oldFile, newFile, overwrite)
If file already exists = If IO.File.Exists(oldFile) Then
Move File = IO.File.Move(oldFile, newFile, overwrite)
Move&Backup File = IO.File.Replace(oldFile, newFile, backup1, ignore)
Private Shared Function Find_in_Folder(folder As String, searchFor As String, ignoreCase As Boolean, includeSubSirectories As Boolean) As List(Of String)
Dim searchOpt As FileIO.SearchOption
Dim ret As New List(Of String)
If includeSubSirectories Then
searchOpt = FileIO.SearchOption.SearchAllSubDirectories
Else
searchOpt = FileIO.SearchOption.SearchTopLevelOnly
End If
Try
ret = My.Computer.FileSystem.FindInFiles(folder, searchFor, ignoreCase, searchOpt).ToList
Catch ex as Exception
Dim errorMessage as String = ex.Message
ret = {errorMessage}.toList
End Try
Return Ret
End Function