Vb.net 循环浏览文件夹,仅复制符合特定条件的文件

Vb.net 循环浏览文件夹,仅复制符合特定条件的文件,vb.net,loops,file-io,copy,preserve,Vb.net,Loops,File Io,Copy,Preserve,我正在编写一个程序,将所需的某些文件从一个文件夹复制到另一个文件夹。但是,我只希望它复制符合某些条件的文件,更具体地说,它应该只复制存储在如下数组中的不在禁用文件、禁用扩展名或禁用文件夹列表中的文件: Public BannedExtensions As String() = {".bak", ".bak2", ".cry", ".max", ".psd", "log", ".lib", "pdb", ".exp"} Public BannedFolders As String(

我正在编写一个程序,将所需的某些文件从一个文件夹复制到另一个文件夹。但是,我只希望它复制符合某些条件的文件,更具体地说,它应该只复制存储在如下数组中的不在禁用文件、禁用扩展名或禁用文件夹列表中的文件:

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\u 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语句中的任何内容

第二个问题是复制到临时文件时如何保留文件夹结构

第三个问题是,压缩这个临时文件夹的最佳方式是什么

提前感谢,,
Marcus

一种方法是在列表中使用自定义类来保存信息:

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的)列表,请执行以下操作:

这比要复制和粘贴的代码更具概念性。由于您希望对结果执行多项操作,因此最好将它们保存在文本框以外的其他位置。VS会很高兴地用鼠标显示列表中的内容


根据被禁止的字符串是完整名称还是部分名称,文件夹测试需要进行。更重要的一点是,列表对象将告诉您是否已经使用
。Contains
而不是全局标志存储了项目。一个
列表(FileInfo)
列表(myFileClass)
将起作用。后者将保留更多的代码。

它不会显示循环中第二个或第三个if语句的任何内容
变量
copy
以false开头,在第一个if块中不会更改,因此第二个、第三个if块永远不会执行。是的,事实上,copy从未设置为true,我可以看到。感谢您指出这一点,我将更新我的帖子,我没有进行任何检查就开始了,但它仍然不起作用。事实上,在再次查看之后,我这样做的原因是,如果它与第一个if语句中的某个内容匹配,那么它就不需要检查其余的if语句,因为它无论如何都不应该被复制(如果我错了,请纠正我)。然后对每个语句的第一个文件重复此过程(
每个文件名作为IO.Directory.GetFiles(根文件夹“*”,IO.SearchOption.AllDirectory)中的字符串)
)a)启用选项Strict并修复错误b)UI控件生成可怕的变量。将符合条件的文件保存在列表中,以便在其他地方处理c)如果要保留文件结构,还需要保存路径(或FileInfo对象)d)进行压缩,首先修复其余文件,然后作为单独的问题提问。e) 如果文件在您创建的列表中,则跳过测试2和3
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