-
Oct 12th, 2020, 09:58 PM
#1
Thread Starter
Fanatic Member
Finding a subfolder with the highest number that matches a pattern
I need to find the last subfolder that fits a certain pattern under a given folder, and use it for a process.
And if no such subfolder exists then I have to create one.
Let's say the given folder is:
And I need to find the subfolder with the highest value of "i" (under the given folder) based on the following logic:
Code:
Dim i As Long
Sub_Fldr_Name = "List_" & Format(i, "000")
Please note that I am talking about subfolders that are DIRECTLY (and not recursively) under the given folder.
Reading the list of all subfolders directly under the given folder is not a problem.
I have a procedure that does it very well:
Code:
Private Sub Get_Subfolders_Under_Folder(ByRef ParentFldr As String, ByRef SubFldrs() As String)
......
End Sub
The above Sub works fine and gives me the list of all subfolders directly under the parent folder in the SubFldrs() array.
The challenge is not reading the subfolder names from the disk (The above Sub does that properly).
The challenge is the actual looping and pattern matching.
For example there may be only one subfolder in there named "List_7563"
So, all the numbers 1 to 7562 being missing.
In this case, my program should return "List_7563" as that is the folder name with highest value of i that fits that pattern.
There could be all kinds of unrelated subfolders in there too, that is subfolders whose names do not fit that pattern.
For example there can be a subfolder named "Details", or any other name
What is the best way of doing this?
Any help on this would be appreciated.
Thanks.
-
Oct 13th, 2020, 01:21 AM
#2
Re: Finding a subfolder with the highest number that matches a pattern
Check the Dir function for collecting all file names in a given directory.
Use the Instr function to check for a _
Then IsNumeric for checking if a string is a numeric value, if it is convert it to a number
-
Oct 13th, 2020, 02:01 AM
#3
Re: Finding a subfolder with the highest number that matches a pattern
Agree with Arnout.
Collect the Foldernames with Dir (or its API-Sisters) matching that particular pattern,
comparing Foldernames to your Pattern you can use the "Like"-Operator
in a hidden ( ! ) Listbox with Sorted=True add only the the "i"-part of the Folder-Name (should actually work with the full foldername, too)
Jump to last Item in the Listbox, should be the "highest" Folder-Name
If ListBox.ListCount = 0 (no Folders matching the Pattern found), use "CreateDirectoryEx" to create the Folder (and with it any intermediate Folders, too)
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Oct 13th, 2020, 02:27 AM
#4
Re: Finding a subfolder with the highest number that matches a pattern
Finding the highest value (maximum) of a sequence can be done with a single temporary variable, no need to sort the whole sequence.
Something like this
Code:
Dim vElem As Variant
Dim lIdx As Long
Dim lMaxIdx As Long
Dim vMaxElem As Variant
For Each vElem In EnumFiles(Environ$("TEMP"), "List_*")
vElem = Mid(vElem, InStrRev(vElem, "\") + 1)
lIdx = Val(Mid(vElem, 6))
If lMaxIdx < lIdx Then
lMaxIdx = lIdx
vMaxElem = vElem
End If
Next
Debug.Print vMaxElem
cheers,
</wqw>
-
Oct 13th, 2020, 05:03 AM
#5
Re: Finding a subfolder with the highest number that matches a pattern
here is another option
I found the answer I was looking for here:
http://www.loresoft.com/Applications...d/default.aspx
Paul Welter has created "FileSearch is a simple and fast file searching component that uses the Windows file search API. Source code and examples included."
The FileSearch.dll makes Windows API File Searching capabilities far more accessible through ActiveX.
Usage Example:
In Excel's Visual Basic Editor, include a reference to the FileSearch.dll file referenced above.
The below function will return the file Path of a file based on the file's name and a Search Directory. Its is similar to the SearchTreeForFile Windows API, but permits Wildcards.
Code:
----------------------------------------------------------
Public Function GetPath(strFileName As String, strDirectory As String) As String
Dim i As Long
Dim objSearch As Object
Dim objFile As Object
Set objSearch = CreateObject("FileSearch.Search")
Call objSearch.SearchFiles(strDirectory, strFileName, True)
If objSearch.Files.Count > 0 Then
GetPath = objSearch.Files.Item(1).FilePath & objSearch.Files.Item(1).FileName
Else: GetPath = "Not Found"
End If
Set objSearch = Nothing
Set objFile = Nothing
End Function
from https://www.tek-tips.com/viewthread.cfm?qid=1356727
the link in the post seems to be dead now, but enough posted to give the idea
Last edited by westconn1; Oct 13th, 2020 at 05:09 AM.
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|