logo VB.Net - Working With Files
Guide Contents
DigitalDan Home Page

Notes
Some of these examples require the following structure
Public Structure ListFilesError
Dim ListFiles As List(Of String)
Dim ErrorMessage As String>br /> End Structure

 
List Files in Folder (ignore subfolders)
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

 
List Files in Folder and Sub Folders
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

 
Commands For Manipulating Files
Dont forget to use Try...Catch...Blocks. You may need a backgroundworker - especially if user has any control over the selected files.
 
In these examples, the variables should be assigned as follows....
oldFile = string containing name of existing directory to be copied
newFile = string conatining the directory name of the directory that will be created
overwrite = boolean. If true and file already exists,it will be overwritten. If false an exiting file would cause an error
text string = New text to be added to end of file
ignore boolean - True=Ignore specific MetaData errors False=stop on any error
backup1 string = file name to for the backup

 
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)
 
List Files in Folder that Contain String
This function will search an entire directory (or directory with sub directories) and list every file which contains a text string. However, it is likely to be unsuitable for your application due to one or more of the following considerations... Having explained why it is usually best to avoid FileSystem.FindInFiles based functions, there may be an exception - when you can tightly control the entire contents of the searched directory/sub-directories, the function should be able to perform the task far faster than a manually coded alternative.
 
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

 

DigitalDan.co.uk ... Hits = 342