Ark68
Well-known Member
- Joined
- Mar 23, 2004
- Messages
- 4,665
- Office Version
- 365
- 2016
- Platform
- Windows
I found this code that helps me (once corrected) extract certain string subsets from a string (path).
Consider this path example, the components relate to the the variables of the code. "O:\Products\A\hostFolder\preTitle - ftitle.fileExtension" The code below is meant to extact:
hostFolder = {hostFolder}
pretitle = {preTitle}
ftitle = {ftitle}
fileExtension = {fileExtension}
Because I really don't have a full understanding how this works, let me ask for support for the following:
1) The line in blue is giving me unexpected results. Using the above as an example, hostFolder is returning "A" instead of "hostFolder".
2) The line is red is returning a 'Type mismatch' error.
Consider this path example, the components relate to the the variables of the code. "O:\Products\A\hostFolder\preTitle - ftitle.fileExtension" The code below is meant to extact:
hostFolder = {hostFolder}
pretitle = {preTitle}
ftitle = {ftitle}
fileExtension = {fileExtension}
Rich (BB code):
Sub SearchDirectories(folderPath As String, searchPattern As String, ByRef outputRow As Long)
Dim fileSystem As Object
Dim folder As Object
Dim subFolder As Object
Dim file As Object
Dim matchFound As Boolean
Dim cntFile As Integer
Dim folderName As String, hostFolder As String
Dim preTitle As String
Dim ftitle As String, ftitle2 As String
Dim fileExtension As String
Dim parts() As String, parts2 As Variant
Dim startPos As Long
Dim endPos As Long
Dim ws_dump As Worksheet '######
' Create a FileSystemObject
Set fileSystem = CreateObject("Scripting.FileSystemObject")
Set folder = fileSystem.GetFolder(folderPath)
Set ws_dump = ThisWorkbook.Worksheets("DUMP")
ws_dump.Activate '######
ws_dump.Range("O2").Activate '######
' Initialize matchFound to False for the current folder
matchFound = False
' Search for files with the partial name in the current folder
For Each file In folder.Files
If InStr(1, file.Name, searchPattern, vbTextCompare) > 0 Then
matchFound = True
If matchFound Then ' If a matching file is found, add the folder path to the worksheet
ws_dump.Cells(outputRow, "S").Value = folderPath
hostFolder = Split(Split(folderPath, "\")(UBound(Split(folderPath, "\")) - 1), "\")(0) 'host folder
ws_dump.Cells(outputRow, "R").Value = hostFolder
parts = Split(Split(folderPath, "\")(UBound(Split(folderPath, "\")))(0), " - ")
ftitle = Split(parts(1), ".")(0)
fileExtension = Split(Split(folderPath, ".")(UBound(Split(folderPath, "."))), ".")(1)
ws_dump.Cells(outputRow, "P").Value = ftitle
ws_dump.Cells(outputRow, "Q").Value = fileExtension
ftitle2 = Mid(folderPath, InStrRev(folderPath, "\") + 1)
parts2 = Split(ftitle2, " - ")
If UBound(parts2) >= 0 Then
preTitle = Trim(parts2(0))
Else
preTitle = ""
End If
ws_dump.Cells(outputRow, "Q").Value = preTitle
outputRow = outputRow + 1
End If
cntFile = cntFile + 1
'Exit For
End If
Next file
' Recursively search subfolders
For Each subFolder In folder.SubFolders
SearchDirectories subFolder.Path, searchPattern, outputRow
Next subFolder
End Sub
Because I really don't have a full understanding how this works, let me ask for support for the following:
1) The line in blue is giving me unexpected results. Using the above as an example, hostFolder is returning "A" instead of "hostFolder".
2) The line is red is returning a 'Type mismatch' error.