VB string stuff

mic_y

Expert Member
Joined
Dec 23, 2004
Messages
1,646
Reaction score
10
Location
Slaapstad
hey guys, sorry about the lame question, but I am really feeling braindead right now.

Basically, I have a DB, which is storing a path to an image. The path looks as follows:
\Storage Card\DCIM\100MEDIA\IMAG****.jpg (stored on a WinMo device in a SQL CE DB), where **** is the image's number. This path is pulled from the DB and placed into a string variable

Now, these files are copied to a local drive, and then uploaded into a SQL DB as an image. To upload I need the path to represent the actual path on the local HDD.

Being braindead right now, I woulds like to change the path and have no real idea on how to do it. The Final path should be along the lines of :
D:\Visual Studio Projects\IS3\Project\DeviceData\A\IMAG****.jpg

How would I get rid of the \Storage Card\DCIM\100MEDIA\ so I can then add the correct path...???

You will have my eternal gratitude...
 
Well what i'd typically do with Python coding is to SPLIT the string into an array, the split operator being "\" . Then you take the last value in the array , which will then only be the IMAGE.JPG .

Alternately in VB6 i'd imagine you need to use the INDEXOF command and find the index of the last "\" in the string and then use SUBSTRING with the start = index value and end = end of the string .

Now i'm not sure if VB6 have a SPLITTER, but Python literally have 1 command like this : array = mystr.split("/") and done...that's why Python is such a fun language :)

EDIT in VB6 probably something like this:
Dim arr() As String
arr = Split("a;b;c", ";")

theimagestring = arr[ubound(arr) - 1]

and then get the last element in the array.
 
Last edited:
Hey diabolus...

thanks for the reply... would probably work perfectly, if I could find a similar function in VB, but right now, don't have the time or the patience (damn international finance test tomorrow). Just went about it the quick and dirty way (since the files are deleted every day, and not kept on the phone) each day the numbering will start from IMAG0001, and will therefore never exceed the 9999 so I just used the str = right(path,12) basically truncating the whole left part of the path, and then adding the required one...
 
is the image name a fixed length? if ex IMAG0345.jpg ?

If so, you could go:

Dim tempStr as String
Dim newpath as String

tempStr = right(filepath,8)
newpath = "D:\Visual Studio Projects\IS3\Project\DeviceData\A\" & tempStr
 
argh, nm, jus saw ure post and saw u did the exact thing i recommended.
 
Well, if the name of the file can be any length, why not do something as follows:

Code:
Dim fileName as String
Dim path as String

// Set the value of the "path" string...

fileName = path.Substring(path.LastIndexOf("\\") + 1)

Might not be proper VB.NET, I come from a C# background, but the idea is still the same...
 
Remove the escape character for VB.

Code:
Dim fileName as String
Dim path as String

'Set the value of the "path" string - preferably from a config file, or other storage area.

fileName = path.Substring(path.LastIndexOf("\") + 1)
 
Private Function findSlash(strPath As String) As Long
Dim lngCount As Long
For lngCount = Len(strPath) To 1 Step -1
If Mid(strPath, lngCount, 1) = "\" Then
findSlash = lngCount
Exit Function
End If
Next
End Function
Public Function createNew(strOldPath As String, strNewPath As String) As String
createNew = strNewPath & Mid(strOldPath, findSlash(strOldPath) + 1, Len(strOldPath) - findSlash(strOldPath) - 1)
End Function
 
Private Function findSlash(strPath As String) As Long
Dim lngCount As Long
For lngCount = Len(strPath) To 1 Step -1
If Mid(strPath, lngCount, 1) = "\" Then
findSlash = lngCount
Exit Function
End If
Next
End Function
Public Function createNew(strOldPath As String, strNewPath As String) As String
createNew = strNewPath & Mid(strOldPath, findSlash(strOldPath) + 1, Len(strOldPath) - findSlash(strOldPath) - 1)
End Function

WTH? My solution (with fine-tuning from Raithlin) is a LOT better... I'm assuming this is a sarcastic post, right? :p
 
Some arbitrary variations:
1) FileName = Mid(txt, InStrRev(txt, "\") + 1)
2) FileName = Split(txt, "\")(UBound(Split(txt, "\")))
3) FileName = StrReverse(Left(StrReverse(txt), InStr(StrReverse(txt), "\") - 1))
 
Last edited:
I'm reminded of how fortunate I am to work in .Net.

VB6 compatible code (found this on Google)
Code:
Public Function NameFromFullPath(FullPath As String) As String
'Input: Name/Full Path of a file
'Returns: Name of file

    Dim sPath As String
    Dim sList() As String
    Dim sAns As String
    Dim iArrayLen As Integer

    If Len(FullPath) = 0 Then Exit Function
    sList = Split(FullPath, "\")
    iArrayLen = UBound(sList)
    sAns = IIf(iArrayLen = 0, "", sList(iArrayLen))
    
    NameFromFullPath = sAns

End Function
:erm:
 
Top
Sign up to the MyBroadband newsletter
X