Reputation: 77
I've written an application in visual basic that copies directories from the local drive to a network share, and that portion works perfectly, but now I want to exclude certain file types from being copied in the directory and the sub-directories and I'm not quite sure how to go about doing that. Here is my code if it helps you answer my question. Thanks in advance for the help.
Public Class Choices
Private Sub Choices_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Public Sub btnDocuments_Click(sender As Object, e As EventArgs) Handles btnDocuments.Click
Dim docsDirectory, destdocsDirectory, userDirectory, userName, hDrive, mydocsDirectory, destmydocsDirectory, newdestdocsDirectory As String
'Function to pull user profile path
hDrive = Environment.GetEnvironmentVariable("homedrive")
userName = Environment.GetEnvironmentVariable("username")
userDirectory = Environment.GetEnvironmentVariable("userprofile")
docsDirectory = userDirectory + "\Documents"
destdocsDirectory = hDrive + userName + "\My Files"
mydocsDirectory = "C:\My Documents"
destmydocsDirectory = hDrive + userName + "\My Documents"
If (My.Computer.FileSystem.DirectoryExists(destdocsDirectory)) Then
My.Computer.FileSystem.CopyDirectory(docsDirectory, destdocsDirectory, showUI:=FileIO.UIOption.AllDialogs)
Else
My.Computer.FileSystem.CreateDirectory(destdocsDirectory)
My.Computer.FileSystem.CopyDirectory(docsDirectory, destdocsDirectory, showUI:=FileIO.UIOption.AllDialogs)
End If
If (My.Computer.FileSystem.DirectoryExists(mydocsDirectory)) Then
My.Computer.FileSystem.CopyDirectory(mydocsDirectory, destmydocsDirectory, showUI:=FileIO.UIOption.AllDialogs)
Else
MessageBox.Show(mydocsDirectory + "Does not exist")
End If
End Sub
Private Sub btnDesktop_Click(sender As Object, e As EventArgs) Handles btnDesktop.Click
Dim deskDirectory, destdeskDirectory, userDirectory, userName, hDrive As String
hDrive = Environment.GetEnvironmentVariable("homedrive")
userName = Environment.GetEnvironmentVariable("username")
userDirectory = Environment.GetEnvironmentVariable("userprofile")
deskDirectory = userDirectory + "\Desktop"
destdeskDirectory = hDrive + userName + "\Desktop"
If (My.Computer.FileSystem.DirectoryExists(deskDirectory)) Then
My.Computer.FileSystem.CopyDirectory(deskDirectory, destdeskDirectory, showUI:=FileIO.UIOption.AllDialogs)
End If
End Sub
End Class
Upvotes: 1
Views: 1152
Reputation: 320
Here is an idea (remember to import System.IO):
If (My.Computer.FileSystem.DirectoryExists(destdocsDirectory)) Then
For Each foundFile As String In My.Computer.FileSystem.GetFiles(docsDirectory, _
FileIO.SearchOption.SearchTopLevelOnly, "*.*")
Select Case LCase(Path.GetExtension(foundFile))
Case ".mks"
Case ".wav"
Case ".jpg"
Case ".wmv"
Case Else
My.Computer.FileSystem.CopyFile(foundFile, destdocsDirectory & "\" & Path.GetFileName(foundFile))
End Select
Next
Else
Upvotes: 1