获取具有多个扩展名的文件名,通过字符串列表

huangapple go评论79阅读模式
英文:

Get files names with multiple extensions by a list of strings

问题

在一个VSTO解决方案中我有一个字符串列表

AA000017
FF100218
P0100217
DF100216
CODICE - CODE
A010AA77
B0000017

在一个文件夹中我有许多不同扩展名的文件例如

DF100216.DXF
DF100216.DWG
DF100216.PDF
DF100216.STP
FF100218.PDF
FF100218_00-001.PDF
FF100218_00-002.PDF
FF100218_00-003.PDF
FF100218.STP

对于列表中的每个值如果我需要例如带有代码FF100218的PDF文件我需要返回一个列表

FF100218.PDF
FF100218_00-001.PDF
FF100218_00-002.PDF
FF100218_00-003.PDF

我有一个类似这样的代码

Dim SelectedtList As New List(Of String) {"DF100216","FF100218","DF100216"}
Dim DrawingExtensionList() As String = {"*.PDF","*.TIF"} 
For Each ext In DrawingExtensionList
    '基于所选择的目录仅返回文件名
    Dim fi = From f In New IO.DirectoryInfo(DrowingPath_Internal).GetFiles(ext).Cast(Of IO.FileInfo)()
    Where fi.Name Like SelectedtList
    Select f
    fileList.AddRange(fi)
Next

有没有一种方法可以做到这一点
英文:

In a VSTO solution I have a List of(Strings):

AA000017
FF100218
P0100217
DF100216
CODICE - CODE
A010AA77
B0000017

In a folder I have many file with different extensions, for example I have:

DF100216.DXF
DF100216.DWG
DF100216.PDF
DF100216.STP
FF100218.PDF
FF100218_00-001.PDF
FF100218_00-002.PDF
FF100218_00-003.PDF
FF100218.STP
ecc 

This for each value of the list. If I need for example the pdf files with code FF100218, I need back a lsit whit :

FF100218.PDF
FF100218_00-001.PDF
FF100218_00-002.PDF
FF100218_00-003.PDF

I have a code like this:

Dim SelectedtList As New List(Of String) {"DF100216","FF100218","DF100216"}
Dim DrawingExtensionList() As String = {"*.PDF","*.TIF"} 
 For Each ext In DrawingExtensionList
                            'Returns only the filenames based on the directory that is selected
                            Dim fi = From f In New IO.DirectoryInfo(DrowingPath_Internal).GetFiles(ext).Cast(Of IO.FileInfo)()
                                     Where fi.Name Like SelectedtList
                                     Select f
                            fileList.AddRange(fi)
                        Next

There is a way to do this?

答案1

得分: 1

提及 [在C#中如何使用另一个列表的StartsWith()条件过滤列表](https://stackoverflow.com/q/29577639/1115360)你可以这样做

```vbnet
Imports System.IO

Module Module1

    Sub Main()
        Dim files = {"DF100216.DXF", "DF100216.DWG", "DF100216.PDF", "DF100216.STP", "FF100218.PDF", "FF100218_00-001.PDF", "FF100218_00-002.PDF", "FF100218_00-003.PDF", "FF100218.STP", "ecc"}
        Dim selectedList = {"DF100216", "FF100218", "DF100216"}
        Dim drawingExtensionList() As String = {".pdf", ".TIF"}

        Dim fileList = files.Where(Function(f) selectedList.Any(Function(s) Path.GetFileNameWithoutExtension(f).StartsWith(s, StringComparison.InvariantCultureIgnoreCase) AndAlso
                                               drawingExtensionList.Any(Function(w) String.Compare(Path.GetExtension(f), w, StringComparison.InvariantCultureIgnoreCase) = 0))).ToList()

        Console.WriteLine(String.Join(vbCrLf, fileList))

        Console.ReadLine()

    End Sub

End Module

注:它使用文件扩展名,如“.pdf”,而不是通配符样式的“*.pdf”。

要获得:

DF100216.PDF
FF100218.PDF
FF100218_00-001.PDF
FF100218_00-002.PDF
FF100218_00-003.PDF

要使其从目录中运行,而不是像上面的示例中设置为数组,你可以使用:

Dim files = Directory.EnumerateFiles("你的\完整\路径\在这里")
英文:

Referring to In C#, how can I filter a list using a StartsWith() condition of another list?, you could do this:

Imports System.IO

Module Module1

    Sub Main()
        Dim files = {"DF100216.DXF", "DF100216.DWG", "DF100216.PDF", "DF100216.STP", "FF100218.PDF", "FF100218_00-001.PDF", "FF100218_00-002.PDF", "FF100218_00-003.PDF", "FF100218.STP", "ecc"}
        Dim selectedList = {"DF100216", "FF100218", "DF100216"}
        Dim drawingExtensionList() As String = {".pdf", ".TIF"}

        Dim fileList = files.Where(Function(f) selectedList.Any(Function(s) Path.GetFileNameWithoutExtension(f).StartsWith(s, StringComparison.InvariantCultureIgnoreCase) AndAlso
                                               drawingExtensionList.Any(Function(w) String.Compare(Path.GetExtension(f), w, StringComparison.InvariantCultureIgnoreCase) = 0))).ToList()

        Console.WriteLine(String.Join(vbCrLf, fileList))

        Console.ReadLine()

    End Sub

End Module

N.B.: It uses the file extension such as ".pdf", not the wildcard-style "*.pdf".

To get:

DF100216.PDF
FF100218.PDF
FF100218_00-001.PDF
FF100218_00-002.PDF
FF100218_00-003.PDF

To make it work from the directory instead of setting it to an array like in the above example, you can use:

Dim files = Directory.EnumerateFiles("your\full\path\here")

huangapple
  • 本文由 发表于 2023年7月3日 21:30:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76605228.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定