Ideas for creating a subset of the string 'pathname' ?

Hi all,

I am working on a VB-Script that uses the pathname of a given set of files and extracts a subset of that pathname string only.

This is the string I have:
"H:\Images\2006\04_02 CanyonLands\converted\IMG12034.tif"

And this is what I want (a subset of the string above: pathname)
"04_02 CanyonLands"

I am a Photographer working with lots of pictures. My images are sorted by Year/Date/Event. The idea now is to copy a subset of the pathname to a specifc field called EVENT of my images database (iView Media Pro v3.1.).

But this may not be so important as I have no problem putting the whole string of the pathname into the field EVENT.

The problem is to extract the subset.
Not only that, the pathname and the subset are variable in lenght. So I would need to cut characaters off from the left and from the right. But this cannot be the solution as the lengths vary.

To me it seems as if I had to work with the occurring numbers of backslashes "\".
From the left side they are always 3, form the right side they may vary. So I would need to extract the string between Backslash No. 3 and No. 4.

This is the code where data is extracted. From that I'd like to create a subset as described above.
The code provided below does simply cut-off the first 15 characters (boldfaced), but if one day the numbers of characters change, then this won't work anymore. So I opt for something like using the backslashes.

Hope I find someone with a great idea.
Cheers, Hans.

VB Code:
For Each mediaItem In app.ActiveCatalog.Selection
strText1 = "x"
If (mediaItem.Name <> "") Then strText1 = mediaItem.Path End If
strText2 =
mid(strText1,15)
If (strText2 <> "") Then mediaItem.annotations.event = strText2
End If






Answer this question

Ideas for creating a subset of the string 'pathname' ?

  • Thilakavathy

    Thanks, Navajo

    this makes a lot of sense. But I am an absolute beginner coding in VB, so here is my next questions.

    How do I join the substrings Probably with a Join-command, but how.
    I would need the substrings 2 and 3 joined into one new string.

    Another question, I guess I have to declare the array and it's substrings at the beginning of the script, right How is this done.

    I certainly appreciate your help.
    Cheers, Hans.

    Below is the code that I've got so far:
    (The missing parts are marekd boldfaced)

    Option Explicit

    Const kMsgBoxTitle = "iView MediaPro"
    'declare array and substrings
    Dim app, mediaItems, mediaItem, path, strText1, strImage, substrings()

    ' display an about box
    If (MsgBox("This command writes the Pathname into the Field Event.", 70, kMsgBoxTitle) = vbOk) Then

    Main()
    End If

    Sub Main()
    Set app = CreateObject("iView.Application")
    ' get the active catalog
    If (app.Catalogs.count = 0) Then
    MsgBox "Please launch iView MediaPro.", vbCritical, kMsgBoxTitle
    Elseif (app.ActiveCatalog.Selection.Count = 0) Then
    MsgBox "You need to select at least one media item in the active catalog in order to use this script.", vbCritical, kMsgBoxTitle
    Else
    For Each mediaItem In app.ActiveCatalog.Selection
    strText1 = "x"
    If (mediaItem.Name <> "") Then strText1 = mediaItem.Path End If
    strImage = strText1
    substrings = Split(strImage, "\")
    ' strText2 = join substrings 2 & 3
    mediaItem.annotations.event = strText2
    End If
    Next
    End If
    End Sub



  • Lawrex

    Hi Hans

    There is a far easier way, just use the Split function.

    strImage = "H:\Images\2006\04_02 CanyonLands\converted\IMG12034.tif"
    substrings = Split(strImage, "\")

    This returns a zero-based, one-dimensional array containing a specified number of substrings.

    substrings(0) = "H:"
    substrings(1) = "Images"
    substrings(2) = "2006"
    substrings(3) = "04_02 CanyonLands"
    substrings(4) = "converted"
    substrings(5) = "IMG12034.tif"


  • Dan Imbrogno

    Hi,

    You can join strings using the & character. Like this....

    strText2 = substring(2) & substring(3)

    You can add your own text as well like this....

    strText2 = substring(2) & " - " & substring(3)

    And you declare the array like this.

    Dim substrings() as String



  • Ideas for creating a subset of the string 'pathname' ?