查找特定子文件夹

时间:2017-10-01 15:21:53

标签: c# c

所以目前我在共享网络上有一个区域,其中包含每个产品的子文件夹。

所以现在我的文件夹层次结构如下:

Test Root Folder <--- This is my master folder
    Category 1
        Product 1 <--- this is the folder I'm trying to find
        Product 2
        Product 3
    Category 2
        Product 6
        Product 7
    Category 3
    Category 4
        Product 12

产品文件夹总是采用这种格式&#34; 1234 - 产品1&#34;,通常当我搜索它时,我知道开头所以我知道&#39; 1234&#39;在这种情况下,我不知道它在哪个类别,也不知道标题&#39;产品1&#39;只是1234年。

如何自动执行此搜索?

到目前为止这是我的代码

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.IO;

namespace HCA
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent(); // For WindowsDesigner DO NOT REMOVE
        }

        void Button1Click(object sender, EventArgs e)
        {
        string folderPath = @"C:\Users\Mike\Desktop\YBA Test";
        string searchPattern = textBox1.Text & " - *";
        DirectoryInfo dir= new DirectoryInfo(folderPath);
        DirectoryInfo[] directories =
            dir.GetDirectories(searchPattern, SearchOption.AllDirectories);
        foreach (DirectoryInfo dir in directories)
        {
            listBox1.Items.Add(dir.Path);
        }           
        }

        void Button2Click(object sender, EventArgs e)
        {
            listBox2.Items.Clear;
        }
    }
}

现在我希望用户在textBox1中输入一个数字,例如&#34; 1234&#34;当他们点击按钮1时,新项目将被添加到listBox1作为目录的超链接&#34; C:\ Users \ Mike \ Desktop \ YBA Test \ CCA ** 1234 - 测试** \&#34;

2 个答案:

答案 0 :(得分:1)

如果是VBA,以下可能会做到这一点:

Option Compare Database
Option Explicit

Public Sub MyFind()
' basis for this module coming from this URL :
' https://stackoverflow.com/questions/22645347/loop-through-all-subfolders-using-vba
Dim FileSystem As Object
Dim HostFolder As String
Dim ProductToSearchFor As String

    HostFolder = "B:\"                  ' "W:\xtodel"
    ProductToSearchFor = "Product 7"    ' "1234"
    Set FileSystem = CreateObject("Scripting.FileSystemObject")
    DoFolder FileSystem.GetFolder(HostFolder), ProductToSearchFor

End Sub

Private Sub DoFolder(Folder, ProductToSearchFor As String)
Dim SubFolder As Object
    Debug.Print "'" & Folder & Space(50 - Len(Folder)) & " // Name-Part = " & Folder.Name
    For Each SubFolder In Folder.SubFolders
        DoFolder SubFolder, ProductToSearchFor
    Next
    If Left(Folder.Name, Len(ProductToSearchFor)) = ProductToSearchFor Then
        Debug.Print "' ==>> FOUND !"
        MsgBox "Found !", vbInformation
    End If
End Sub

这可能会在您的VBA-Direct窗口中显示以下结果:

'B:\                                                   // Name-Part =
'B:\Test Root Folder                                   // Name-Part = Test Root Folder
'B:\Test Root Folder\Category 1                        // Name-Part = Category 1
'B:\Test Root Folder\Category 1\Product 1              // Name-Part = Product 1
'B:\Test Root Folder\Category 1\Product 2              // Name-Part = Product 2
'B:\Test Root Folder\Category 1\Product 3              // Name-Part = Product 3
'B:\Test Root Folder\Category 2                        // Name-Part = Category 2
'B:\Test Root Folder\Category 2\Product 6              // Name-Part = Product 6
'B:\Test Root Folder\Category 2\Product 7              // Name-Part = Product 7
' ==>> FOUND !
'B:\Test Root Folder\Category 3                        // Name-Part = Category 3
'B:\Test Root Folder\Category 4                        // Name-Part = Category 4
'B:\Test Root Folder\Category 4\Product 12             // Name-Part = Product 12

答案 1 :(得分:0)

这是对2017年10月1日答案的补充。我试图使递归更加不言自明,最后,它演变为此。一个很好的例子,一些事情会变得复杂。

Option Compare Database
Option Explicit
'
' two variables at module level in order to exit the recursion if needed
'
Dim ProductFolderIsFound As Boolean     ' keep track if product-folder is found
Dim TheFoundProductFolder As String     ' return the full-path if product-folder is found
Dim FolderCounter As Long               ' trying to keep track of the recursion
'
' check for multiple occurrences, switch the comment before the line with 'true' and 'false'
'
Const StopWhenFound As Boolean = False              ' yes or no
'Const StopWhenFound As Boolean = True              ' yes or no
Const UncPathDivider As String = ";" & vbCrLf       ' if found, separate them by this string


'##################'
Public Sub MyFind()
'
' basis for this module coming from this URL :
' https://stackoverflow.com/questions/22645347/loop-through-all-subfolders-using-vba
'
Dim FileSystem As Object                ' will become 'Scripting.FileSystemObject'
Dim HostFolder As String                ' base-location for the search
Dim ProductToSearchFor As String
'----------------
' Initialisation
'----------------
    HostFolder = "B:\"                  ' "Z:\path\to\search"
    'ProductToSearchFor = "1234"
    'ProductToSearchFor = "Product 7"
    'ProductToSearchFor = "Category 2"
    ProductToSearchFor = "Product 1"

    FolderCounter = 1
    ' parameters for the recursion
    ProductFolderIsFound = False        ' in the start, productfolder is not yet found ;-)
    TheFoundProductFolder = ""          ' in the start, productfolder is not yet found ;-)

'-------------------
' get the work done
'-------------------
    ' debug message
    Debug.Print "' ==>> searching for : '" & ProductToSearchFor & _
                "' , starting in location : '" & HostFolder & "' <<=="

    ' give FileSystem his necessary type, in order to call the function '.GetFolder'
    ' ==>> debug.print VarType(FileSystem) will still return '9', like type 'Object'
    Set FileSystem = CreateObject("Scripting.FileSystemObject")
    ' start of recursion, by passing the base location as a 'Scripting.FileSystemObject'
    DoFolder FileSystem.GetFolder(HostFolder), ProductToSearchFor, FolderCounter, ""

'--------
' result
'--------
    '
    ' What to do when yes or no the product-folder is found
    '
    If ProductFolderIsFound Then
        Debug.Print "' ==>> Found : " & vbCrLf & TheFoundProductFolder & "' <<=="
    Else
        Debug.Print "' ==>> NOT found : '" & ProductToSearchFor & _
                "' in location : '" & HostFolder & "' <<=="
    End If
End Sub


'###################'
Private Sub DoFolder(ByVal Folder As Object, _
                        ByVal ProductToSearchFor As String, _
                        ByVal FolderID As Long, _
                        ByVal PreviousFolderIDs As String) ' important : ByVal instead of ByRef
'
' the parameter 'Folder' [will be/needs to be] compatable type 'Scripting.FileSystemObject'
' because we use the function '.GetFolders' and the property '.Name'
'
Dim SubFolder As Object
Dim MessageLine As String
    '
    ' create a debug-message-line : "'" & Folder & (some spaces) & "//Name-Part = " & Folder.Name
    ' remember, 'Folder' returns the full path and 'Folder.Name' only the last part of the full path
    '
    MessageLine = "'" & PreviousFolderIDs & "__" & FolderID & ""
    If Len(MessageLine) <= 15 Then
        ' make the message at least 50 characters, so the 'name-part' can be in a second column
        MessageLine = MessageLine & Space(15 - Len(MessageLine))
    End If
    MessageLine = MessageLine & Folder
    If Len(MessageLine) <= 60 Then
        ' make the message at least 50 characters, so the 'name-part' can be in a second column
        MessageLine = MessageLine & Space(60 - Len(MessageLine))
    End If
    MessageLine = MessageLine & "//Name-Part = " & Folder.Name
    Debug.Print MessageLine

'-------------
' try to find
'-------------
    '
    ' if the 'Folder' is the desired product-folder, stop searching
    ' if desired switch the function 'Mid' with the 'Left'
    '
    'If Left(Folder.Name, Len(ProductToSearchFor)) = ProductToSearchFor Then
    If InStr(Folder.Name, ProductToSearchFor) > 0 Then
        Debug.Print "'" & Space(14) & "searching for : " & ProductToSearchFor & " ==>> FOUND !"
        ' let the rercursive stack know that the folder is found
        ProductFolderIsFound = True
        ' return the full location of the desired product-folder
        TheFoundProductFolder = TheFoundProductFolder & Folder & UncPathDivider
        If ProductFolderIsFound And StopWhenFound Then
            'MsgBox "Found !", vbInformation
            Exit Sub
        End If
    End If

'---------------------------------
' recursive call for this funtion
'---------------------------------
    '
    ' if product-folder not yet found, check all the subfolders of the current 'Folder'
    '
    For Each SubFolder In Folder.SubFolders
        FolderCounter = FolderCounter + 1
        DoFolder SubFolder, ProductToSearchFor, FolderCounter, PreviousFolderIDs & FolderID & ";"
        ' if product-folder has been found, no need to scan further thru the folder-structure
        If ProductFolderIsFound And StopWhenFound Then
            Exit For
        End If
    Next
End Sub

这可能会在您的VBA-Direct窗口中显示以下结果:

'##################################'
Private Sub VBA_Window_Direct_003()
'
'myfind
' ==>> searching for : 'Product 1' , starting in location : 'B:\' <<==
'__1           B:\                                          //Name-Part =
'1;__2         B:\Test Root Folder                          //Name-Part = Test Root Folder
'1;2;__3       B:\Test Root Folder\Category 1               //Name-Part = Category 1
'1;2;3;__4     B:\Test Root Folder\Category 1\Product 1     //Name-Part = Product 1
'              searching for : Product 1 ==>> FOUND !
'1;2;3;__5     B:\Test Root Folder\Category 1\Product 2     //Name-Part = Product 2
'1;2;3;__6     B:\Test Root Folder\Category 1\Product 3     //Name-Part = Product 3
'1;2;__7       B:\Test Root Folder\Category 2               //Name-Part = Category 2
'1;2;7;__8     B:\Test Root Folder\Category 2\Product 6     //Name-Part = Product 6
'1;2;7;__9     B:\Test Root Folder\Category 2\Product 7     //Name-Part = Product 7
'1;2;__10      B:\Test Root Folder\Category 3               //Name-Part = Category 3
'1;2;__11      B:\Test Root Folder\Category 4               //Name-Part = Category 4
'1;2;11;__12   B:\Test Root Folder\Category 4\Product 12    //Name-Part = Product 12
'              searching for : Product 1 ==>> FOUND !
' ==>> Found :
B:\Test Root Folder\Category 1\Product 1;
B:\Test Root Folder\Category 4\Product 12;
' <<==
End Sub

在第一列中,数字是不同子文件夹的缩写:1=B:2=Test Root Folder3=Category 1等等。

相关问题