I am writing a program that copies certain files needed from one folder to another. However, I only want it to copy files that fit certain criteria, More specifically, it should only copy files that are not in the banned files, banned extensions, or banned folders list as stored in an array like this:
Public BannedExtensions As String() = {".bak", ".bak2", ".cry", ".max", ".psd",
"log", ".lib", "pdb", ".exp"}
Public BannedFolders As String() = {"Tools", "Editor", "Code", "LogBackups",
"statoscope", "BinTemp", "USER", "rc"}
Public BannedFiles As String() = {"Editor.exe", "error.bmp", "error.dmp",
"luac.out", "tags.txt"}
The code should then move them to a temporary directory and then zip them up and save them to the location stored in the File_Name
variable.
This is my code as a whole:
Option Strict On
Public Class Form1
'define the two variables used for tracking file name and where to save'
Private Property Root_Folder As String
Private Property File_Name As String
'define the variable which dictates wether we copy the file
Private Property copy As Boolean
'define which files we need and do not need'
Public BannedExtensions As String() = {".bak", ".bak2", ".cry", ".max", ".psd", "log", ".lib", "pdb", ".exp"}
Public BannedFolders As String() = {"Tools", "Editor", "Code", "LogBackups", "statoscope", "BinTemp", "USER", "rc"}
Public BannedFiles As String() = {"Editor.exe", "error.bmp", "error.dmp", "luac.out", "tags.txt"}
Function Copy_RequiredFiles() As Integer
Dim i As Integer = 0
'Searches directory and it's subdirectories for all files, which "*" stands for
'Say for example you only want to search for jpeg files... then change "*" to "*.jpg"
For Each filename As String In IO.Directory.GetFiles(Root_Folder, "*", IO.SearchOption.AllDirectories)
copy = True
Dim fName As String = IO.Path.GetFileName(filename)
Dim fExtension As String = IO.Path.GetExtension(filename)
Dim fFolder As String = IO.Path.GetDirectoryName(filename)
For i = 0 To BannedExtensions.Length - 1
If fExtension = BannedExtensions(i) Then
RichTextBox1.Text = RichTextBox1.Text & vbNewLine & "Extension: " & filename
copy = False
End If
Next
If copy = True Then
i = 0
For i = 0 To BannedFolders.Length - 1
If filename = BannedFolders(i) Or BannedFolders(i).Contains(filename) Then
RichTextBox1.Text = CStr(CBool(RichTextBox1.Text & vbNewLine & "Folder: " &
copy) = False)
End If
Next
If copy = True Then
i = 0
For i = 0 To BannedFiles.Length - 1
If filename = BannedFolders(i) Or BannedFolders(i).Contains(filename) Then
RichTextBox1.Text = RichTextBox1.Text & vbNewLine & "Folder: " & filename
copy = False
End If
Next
End If
End If
If copy = True Then
'copy the file into the selected folder'
End If
Next
Return 0
End Function
'when the browse button for selecting the folder is selected'
Private Sub Browse_Root_Click(sender As Object, e As EventArgs) Handles Browse_Root.Click
'check if the folder dialogue has been opened successfully and something has been selected'
If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then '
'set the textbox and the previously defined variable to the folder selected'
TextBox1.Text = FolderBrowserDialog1.SelectedPath
Root_Folder = FolderBrowserDialog1.SelectedPath
End If
End Sub
'when the browse button for where to save the file is selected'
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'set the options to limit the save type to a zip file'
SaveFileDialog1.Filter = "zip files (*.zip)|*.zip"
'check that the dialogue box has opened succesfully'
If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
'set the text box and the previously defined variable to the file selected'
TextBox2.Text = SaveFileDialog1.FileName()
File_Name = SaveFileDialog1.FileName()
End If
End Sub
'when the user changes the value of the textbox update the folder to select from'
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Root_Folder = TextBox1.Text
End Sub
'when the user changes the value of the textbox update the file to save to'
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
File_Name = TextBox2.Text
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Copy_RequiredFiles()
End Sub
End Class
'when the browse button for selecting the folder is selected'
Private Sub Browse_Root_Click(sender As Object, e As EventArgs) Handles Browse_Root.Click
'check if the folder dialogue has been opened successfully and something has been selected'
If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then '
'set the textbox and the previously defined variable to the folder selected'
TextBox1.Text = FolderBrowserDialog1.SelectedPath
Root_Folder = FolderBrowserDialog1.SelectedPath
End If
End Sub
'when the browse button for where to save the file is selected'
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'set the options to limit the save type to a zip file'
SaveFileDialog1.Filter = "zip files (*.zip)|*.zip"
'check that the dialogue box has opened succesfully'
If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
'set the text box and the previously defined variable to the file selected'
TextBox2.Text = SaveFileDialog1.FileName()
File_Name = SaveFileDialog1.FileName()
End If
End Sub
'when the user changes the value of the textbox update the folder to select from'
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Root_Folder = TextBox1.Text
End Sub
'when the user changes the value of the textbox update the file to save to'
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
File_Name = TextBox2.Text
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Copy_RequiredFiles()
End Sub
End Class
I have a couple of questions,
first of which, is why when I run the program and print the files it found that it shouldn't copy, it does not show anything from the second or third if statements in the loop.
Second of which is how would I preserve the folder structure when copying to the temporary file.
And third is, what is the best way to zip up this temporary folder.
Thanks in advance, Marcus