2017-10-01 3 views
-3

So habe ich derzeit einen Bereich in einem freigegebenen Netzwerk mit Unterordnern von jedem Produkt.Finden Sie bestimmte Unterordner

jetzt also meine Ordnerhierarchie sieht wie folgt aus:

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 

Die Produkt Ordner immer in diesem Format sind „1234 - Produkt 1“ Normalerweise, wenn ich es bin auf der Suche ich den Anfang wissen, so ich weiß, die aber in diesem Fall ‚1234‘ ich bin nicht sicher, welche Kategorie es in ist noch der Titel, was Artikel 1 'ist nur die 1234.

Wie kann ich diese Suche automatisieren?

dies ist mein Code so weit

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

Gerade jetzt Benutzer erwarte ich eine Zahl in textBox1 zum Beispiel „1234“ zu setzen und, wenn sie getroffen Taste 1 ein neues Element hinzugefügt werden soll listBox1 als Hyperlink in das Verzeichnis "C: \ Benutzer \ Mike \ Desktop \ YBA Test \ CCA ** 1234 - Test ** \"

+3

Entscheiden Sie zuerst, welche Sprache Sie verwenden. VB.NET ist nicht vba noch VB6. Es steht so auf den Tags – Plutonix

+2

Das Implementieren einer Datenbank mit Dateien ist nie ein Fehler. Treibe das zuerst an. So wie es ist, können Sie nichts anderes tun, als Directory.GetDirectories() zu verwenden, um die Verzeichnisse "category *" zu nummerieren und für jedes Verzeichnis die Verzeichnisse "1234 *" aufzulisten. –

Antwort

0

Dies ist eine Ergänzung zu der Antwort von 1. Oktober 2017. Ich habe versucht, die Rekursion mehr selbsterklärend, und am Ende zu machen, es hat sich dazu entwickelt. Ein schönes Beispiel, wie etwas kompliziert werden kann.

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 

Dies könnte die folgenden Ergebnisse in Ihrem VBA-Direct-Fenster geben:

'##################################' 
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 

In der ersten Spalte sind die Zahlen eine Abkürzung für die verschiedenen Unterordner: 1=B:, 2=Test Root Folder, 3=Category 1 und so auf.

1

Wenn VBA, die folgenden vielleicht den Trick tun könnte:

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 

Dies könnte die folgenden Ergebnisse in Ihrem VBA-Direct-Fenster geben:

'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 
+0

Wäre nett, wenn Sie Ihren Code mit ein paar Wörtern erklären, besonders die Rekursion. – BitAccesser

+0

@BitAccessor Auch nach ein paar Tagen habe ich Mühe, eine kurze Erklärung zu finden. Es gibt auch zwei schwerwiegende Fehler, der 'If Left' sollte vor dem' For Each' stehen, und es sollte einen Trick geben, um der Rekursion zu entkommen, wenn der Ordner gefunden wurde. Vielleicht sollte ich das alles in einer separaten Antwort angehen. – JonRo

Verwandte Themen