我正在編寫一個程序,將某個文件夾所需的某些文件複製到另一個文件夾中。但是,我只希望它複製符合某些條件的文件,更具體地說,它應該只複製不在禁止文件中的文件,禁止的擴展名或禁止的文件夾列表,如下所示:循環瀏覽文件夾,只複製符合特定條件的文件
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"}
代碼應該將它們移動到一個臨時目錄,然後將它們壓縮並保存到File_Name
變量中存儲的位置。
這是我的代碼作爲一個整體:
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
我有幾個問題,
其中第一的,這就是爲什麼當我運行該程序,並打印發現它不該該文件不復制,它不會顯示循環中第二個或第三個if語句的任何內容。
其中的第二個問題是如何在複製到臨時文件時保留文件夾結構。
第三是壓縮這個臨時文件夾的最佳方法是什麼。
由於提前, 馬庫斯
'它沒有顯示循環中第二個或第三個if語句的任何內容'變量'copy'開始爲false並且在第一個If塊中沒有更改,所以第二個,第三個永遠不會執行。 – Plutonix
是的,其實複製從來沒有設置爲真的,我可以看到。 – thetimmer
感謝您指出,我會更新我的帖子,我開始沒有任何檢查,它仍然沒有工作,然後 – marcus