I think I’ve got the macro set up correctly, but for some reason the string split function doesn’t seem to be working correctly.
Dim oSFA as object : oSFA = createUnoService("com.sun.star.ucb.SimpleFileAccess")
Dim stPath as String : stPath = ConvertToUrl("C:/Users/Aaron/Desktop/Files/")
Dim AllFiles() as String : AllFiles = oSFA.getFolderContents(stPath, false)
Dim MatchingFiles() as String
Dim stBasename as String : stBasename = "a"
Dim iNumber as Integer ' Cast from second Element of stSplit
Dim FileName as String ' File name being examined.
Dim iLastNumber as Integer : iLastNumber = 0
Dim stSplit(2) as String 'array from file name split on " Corrected - "
Dim stBase as String 'first element of stSplit
Dim stLatest as String
Dim stFile as String
For i = 0 to Ubound(AllFiles)
msgBox AllFiles(i)
stFile = AllFiles(i)
If InStr(stFile," - Corrected ")Then ' It's corrected
stSplit() = split(stFile, " - Corrected ",2)
stBase = stSplit(0)
msgBox stBase
iNumber = Cint(stSplit(1))
If stBase = stBasename Then 'The base name matches.
If iNumber>iLastNumber Then
iLastNumber = iNumber
End If
End If
End If
Next
If iLastNumber = 0 Then
stLatest = stBasename
Else
stLatest = stBasename & " Corrected - " & Cstr(iLastNumber)
End If
msgBox stLatest
End Sub
I’m testing it on a directory of .txt files, which reminds me, I need to use another split command to peel off the file extension.
The first two msgBoxes return:
file:///C:/Users/Aaron/Desktop/Files/a%20-%20Corrected%201.txt
So, it’s not being split on the delimiter " Corrected - ". I guess it doesn’t like that delimiter for some reason.
I don’t see anything at Split Function to suggest that my delimiter would be disallowed. It fits the description “A string of one or more characters length”.
(Perhaps I should use a Replace command to replace that delimiter with a “.”, so that I can peel off the extension at the same time.)