循环浏览文件夹,仅复制符合特定条件的文件 [英] Loop through folders and only copy files that match certain criteria

查看:153
本文介绍了循环浏览文件夹,仅复制符合特定条件的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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"}

然后将它们移动到临时目录,然后将它们压缩并保存到 File_Name 变量中存储的位置。

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

我有几个问题,

首先是为什么当我运行程序并打印它发现它不应该复制的文件,它不会显示循环中第二个或第三个if语句的任何内容。

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.

其次是如何

第三,压缩此临时文件夹的最佳方法是什么。

And third is, what is the best way to zip up this temporary folder.

提前感谢,
Marcus

Thanks in advance, Marcus

推荐答案

一种方法是使用自定义类保存信息:

One way would be to use a custom class in a List to save the info:

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 并更改如何迭代文件。然后是一个列表来存储这些东西:

Or, rather than recreate the wheel, use System.IO.FileInfo and change how you iterate the files. Then a list to store these things:

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

迭代保存到列表(myFile) p>

To iterate saving to a List(of 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将很乐意用鼠标显示列表中的内容。

This is more conceptual than code to copy and paste. Since you want to do several things with the result, it is better to save them in something other than a TextBox. VS will happily show you what is in the list with the mouse.

文件夹测试将需要工作,具体取决于禁止字符串是完整名称还是部分名称。更大的一点是,List对象会告诉你是否已经使用 .Contains 存储了一个项目,而不是全局标志。 List(Of FileInfo) List(Of myFileClass)将工作。后者将保留更多的代码。

The folder test will need work depending on whether the Banned string is a full or partial name. The larger point is that a List object will tell you if you have already stored an item using .Contains rather than a global flag. Either a List(Of FileInfo) or List(Of myFileClass) will work. The latter will preserve more of the code you have.

这篇关于循环浏览文件夹,仅复制符合特定条件的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆