2014-05-16 74 views
0

我正在編寫一個程序,將某個文件夾所需的某些文件複製到另一個文件夾中。但是,我只希望它複製符合某些條件的文件,更具體地說,它應該只複製不在禁止文件中的文件,禁止的擴展名或禁止的文件夾列表,如下所示:循環瀏覽文件夾,只複製符合特定條件的文件

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語句的任何內容。

其中的第二個問題是如何在複製到臨時文件時保留文件夾結構。

第三是壓縮這個臨時文件夾的最佳方法是什麼。

由於提前, 馬庫斯

+0

'它沒有顯示循環中第二個或第三個if語句的任何內容'變量'copy'開始爲false並且在第一個If塊中沒有更改,所以第二個,第三個永遠不會執行。 – Plutonix

+0

是的,其實複製從來沒有設置爲真的,我可以看到。 – thetimmer

+0

感謝您指出,我會更新我的帖子,我開始沒有任何檢查,它仍然沒有工作,然後 – marcus

回答

1

一種方法是使用自定義類的列表,以保存信息:

Class myFile 
    ' to do all the things you want ("preserve folder struct"), 
    ' you need more than a name in a textbox: 
    Public Property FileName As String 
    Public Property FileExt As String 
    Public Property PathName As String 

    ' cant create a new obj without the essential info 
    Public Sub New(fName As String, fExt as String, pName As String) 
     FileName = fName 
     FileExt = fExt 
     PathName = pName 
    End SUb 

End Class 

或者,而不是重新發明輪子,用System.IO.FileInfo和變化你如何迭代文件。然後,一個列表來存放這些東西:

Friend myFileList As List(of myFile) 
' or 
Friend myFileList As List(of FileInfo) 

要遍歷保存到(MYFILE的)的列表:

Dim myF As MyFile 
For Each filename As String In IO.Directory.GetFiles 

    ' if BannedFiles is a List(Of String) instead of an array: 
    If BannedFiles.Contains(f.Extension) = False Then 
     ' create NEW file object with the info 
     myF = New MyFile(IO.Path.GetFileName(filename), 
          IO.Path.GetExtension(filename), 
          IO.Path.GetDirectoryName(filename)) 
     ' add to the list of files to do 
     myFileList.Add(f) 
    End If 

    If myFileList.Contains(f) = False Then   ' better version of Copy=true 
     For n As Integer = 0 To BannedFoldersLIST.Count - 1 
      If f.FullPath.Contains(BannedFoldersLIST(n)) = False Then 

       ' create a new file object as above 
       myFileList.Add(f)    ' add for folder ban 
      End If 
     Next n 
    End If 

    ' repeat for banned extensions (watch out for "." vs no dot) 

    Return myFileList    ' mine is a function returning 
           ' a List(Of T) to be processed elsewhere 

這是一個比代碼複製並粘貼多個概念。既然你想用結果做幾件事情,最好把它們保存在除TextBox之外的東西上。 VS會很高興地用鼠標向你展示列表中的內容。

根據禁用字​​符串是完整名稱還是部分名稱,文件夾測試將需要工作。更大的一點是,List對象會告訴您是否已經使用.Contains而不是全局標誌存儲了一個項目。 List(Of FileInfo)List(Of myFileClass)都可以使用。後者將保留更多的代碼。

相關問題