Folder Parsing

I think I’ll talk about Work today.

Let’s consider the current problem I’m facing. First, imagine that you’ve got a folder hierarchy set up, with files stored in only the “bottom” folders. Thus, a typical folder path might look something like this:

C:\Main Category\Sub Category\Individual Section\File Name.doc

Just for fun, let’s say that those are all the folders – that is to say, any file will always be in 3 folders deep. Here’s the trick, though: you want to programatically examine the “Individual Section” folder name for any given file. For the sake of argument, let’s say you want to do this because you only want to affect files in one particular “Individual Section.”

Oh, and just to make it more specific, let’s remember that we’re doing this in Visual Basic 6.0.

So, how do we find the “Individual Section?” Here’s what I’m thinking currently: the file path comes to you as a string, of course, so we can do fun Instr and Instrrev searches on it. Looking at the path, we can say that the “Individual Section” will always be between the first and second backslashes (“”), as if you start at the end of the string. (Or, to put it another way, it’ll always be between the 2nd to last and last backslash in the string.)

So here’s what I came up with (I used “firstSlash” and “secondSlash” because they are easier to type than “2ndToLastSlash,” if you follow me):

Dim tmpStr As String
Dim firstSlash As Integer
Dim secondSlash As Integer

tmpStr = "C:\Main Category\Sub Category\Individual Section\Document Name.doc"

firstSlash = InStrRev(tmpStr, "")
secondSlash = InStrRev(tmpStr, "", firstSlash - 1)

debug.print Mid(tmpStr, secondSlash + 1, (firstSlash - secondSlash) - 1)

We need a -1 in the line to find the secondSlash because we don’t want to include the first slash in our search. Similarly, we have to add +1 to secondSlash, and -1 to the length when we use the MID function to chop off those nasty backslashes (we use them for positioning, but we don’t want them in our output).

Not very exciting, I know, but hey – this is what I do all day. And before I started this post, I didn’t have the specific algorithm done – and now I do. So yay for me!

Published
Categorized as tech

By Keith Survell

Geek, professional programmer, amateur photographer, crazy rabbit guy, only slightly obsessed with cute things.