如果文件不包含提供的指定文件列表,请从目录中删除文件? [英] How to remove files from a directory if they don't contain the list of specified files provided?

查看:168
本文介绍了如果文件不包含提供的指定文件列表,请从目录中删除文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个项目,我需要在特定的目录中搜索文件。如果找到的文件不是批准的扩展名,则文件必须移动到归档文件夹。我需要允许用户删除和添加扩展名,因此列表不是一个设置的大小,并且最有可能每周更改。

I'm currently doing a project where I need to search through a specific directory for files. If the files found are not the approved extension then the files must be moved to an archive folder. I need to user to be allowed to remove and add extensions so the list is not a set size and will most likely change weekly.

到目前为止,我可以循环通过目录并将其中的所有文件列入列表框。我的问题出现在尝试区分批准的列表和当前列表,我无法缩小文件并将其显示在列表框中。

So far I'm able to loop through the directories and list all the files in there into a listbox. My problem comes when trying to differentiate between the approved list and the current list and I can't narrow down the files and display them in the list box.

我的错误是:'mscorlib.dll'中出现'System.ArgumentOutOfRangeException'类型的未处理异常,其中我的'list1'变量计数为0,因为即使有匹配的批准数据和当前数据,也没有找到子进程。

My error is : 'An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll' where my 'list1' variable count is 0 because no children were found even when there are matching approved data and current data.

任何帮助将从堆栈溢出社区欣赏!谢谢!!

Any help would be appreciate from stack overflow community! Thanks!!

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim list1 As List(Of String)
    list1 = (From item As String In Me.ListBox1.Items Select item Where Me.ListBox1.Items.Contains(Me.lstApprovedItems.Items)).ToList()

    ListBox1.Items.Add(list1(0))

End Sub

Dim fri As FileInfo

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Dim FileName1 As String = ""        

    Dim dir2 As New DirectoryInfo("D:\Administrator\Desktop\File Management System v0.2\File Management System\File Directories\File Directories\00000000.00F")   
    Dim dir1 As DirectoryInfo() = dir2.GetDirectories()

    Dim fri2 As DirectoryInfo
    For Each fri2 In dir1

        FileName1 = Convert.ToString(fri2.Name)     

        Dim dir As New DirectoryInfo("D:\Administrator\Desktop\File Management System v0.2\File Management System\File Directories\File Directories\00000000.00F\" + FileName1) 
        Dim fiArr As FileInfo() = dir.GetFiles()     

        For Each Me.fri In fiArr

            ListBox1.Items.Add(fri.Name)

        Next fri
    Next
End Sub
End Class


推荐答案

有几种方法可以解决这个问题,这种方法类似于你所拥有的。

There are several ways to go about this, this way is similar to what you have.

' first get OK extension into an array (or list)
Dim Auth(lbAuthExt.Items.Count - 1) As String
lbAuthExt.Items.CopyTo(Auth, 0)

' storage for the result
' only save the Authorized ones if that is all you care about
Dim AuthList As New List(Of FileInfo)
Dim NoAuthList As New List(Of FileInfo)

Dim dir = New DirectoryInfo("C:\Temp")
For Each fi As FileInfo In dir.GetFiles

    If Auth.Contains(fi.Extension) Then
        AuthList.Add(fi)
    Else
        NoAuthList.Add(fi)
    End If
Next

我正在保存FileInfo,因为(可能)您可能会在没有所有冗余路径的情况下向用户显示名称信息。但是,您的代码将需要完整路径名以备以后复制/移动操作。

I am saving FileInfo because (presumably) you might display the names to the user without all the redundant path info. But your code will need the full path name for later Copy/Move ops.

' set the list as the datasource
lbResult.DataSource = AuthList
' display file name
lbResult.DisplayMember = "Name"
' return full name to code
lbResult.ValueMember = "FullName"

代码当然可以简单地循环执行 AuthList 来完成工作。如果您需要首先向用户显示结果,则无需复制2份。 DisplayMember 用于告诉ListBox要显示的属性值,同时保留列表中的所有其他信息。

The code can of course simply loop on AuthList to do its job. If you need to show the results to the user first, there is no need to make 2 copies. DisplayMember is used to tell the ListBox which property value to display while retaining all the other info in the list.

最后,一个linq版本只需保存完整的文件名:

Finally, a linq version which simply saves the full file name:

Dim myAuthList = (From func In Directory.EnumerateFiles("C:\Temp", "*.*", 
            System.IO.SearchOption.AllDirectories)
            Where Auth.Contains(Path.GetExtension(func), 
                StringComparer.InvariantCultureIgnoreCase)).ToList

这篇关于如果文件不包含提供的指定文件列表,请从目录中删除文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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