Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > How to trunicate string from right to left


Reply
 
Thread Tools Display Modes
  #1  
Old 06-27-2005, 07:19 AM
drumgod drumgod is offline
Regular
 
Join Date: Nov 2003
Posts: 69
Default How to trunicate string from right to left


All,

I need to know how to trunicate a string from right to left, until I hit a specified character.

Example:

I have a program that looks at a specific server, and then reports the installed directory from where the service runs. It returns a value of something like this:

C:\Service\Servicename.exe

I need to be able to remove the Servicename.exe (basically walk the string backward until i Hit the "\".
This is so I can backup files located in that specific directory which the service is located. The installation directory can be different, so I cannot hard code this string..

Does anyone know how to do this?

TIA

Drum on .. .. . . .
Reply With Quote
  #2  
Old 06-27-2005, 07:27 AM
ProfEich ProfEich is offline
Newcomer
 
Join Date: Jan 2004
Posts: 9
Default

Hi!

Try using the InStrRev function:

Dim Index As Integer
Dim Path As String

Index = InStrRev( FileName, "\" )
if Index=0 then
Path = ""
else
Path = Left( FileName, Index )
end if
Reply With Quote
  #3  
Old 06-27-2005, 07:36 AM
RoofRabbit's Avatar
RoofRabbit RoofRabbit is offline
Contributor
 
Join Date: Sep 2003
Location: Lenoir, NC - USA
Posts: 731
Default

There's an easier way of doing it but just to show the logic:
Code:
'GetFileName("C:\Service\Servicename.exe") returns "Servicename.exe" ' Function GetFileName(str As String) As String Dim i As Integer Dim j As Integer Dim ts As String If Len(str) > 0 Then 'Make sure it's not zero length j = 1 'Start a count For i = Len(str) To 1 Step -1 If Mid$(str, i, 1) = "\" Then 'Found what we're looking for GetFileName = Right$(str, j - 1) 'j-1 = don't include the "\" Exit Function End If j = j + 1 Next i 'Assume entire string given is filename only GetFileName = str Else 'Was given a null string to work with, return with null string GetFileName = "" End If End Function
__________________
Website http://roofrabbit.com/
Reply With Quote
  #4  
Old 06-27-2005, 07:42 AM
drumgod drumgod is offline
Regular
 
Join Date: Nov 2003
Posts: 69
Default

You are just awesome! Works great!
Reply With Quote
  #5  
Old 06-27-2005, 10:50 AM
DubbleClick's Avatar
DubbleClick DubbleClick is offline
Contributor
 
Join Date: Mar 2005
Location: Tennessee
Posts: 511
Default

Or
Code:
StringPath = GetPath("C:\Service\Servicename.exe") Private Function GetPath(ByVal as_path As String) As String GetPath = Mid$(as_path ,1 , InStrRev(as_Path, "\")) End Function
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->