Solved

String manipulation

Posted on 2012-04-04
4
207 Views
Last Modified: 2012-04-04
Hi,

hopefully simple question for vb.net experts.  If i have string variable that looks something like this:

Dim s as String = "C:\Folder1\Folder2\Folder3\File1.txt"

or the value of s could have somethign like this:

Dim s as String = "C:\a\b\c\File1.txt"

I want to basically navigate to the second folder so I end up with this:

Dim result as String = "C:\Folder1\Folder2\"

or

Dim result as String = "C:\a\b\"

thanks
0
Comment
Question by:yechan
  • 2
4 Comments
 
LVL 25

Expert Comment

by:Luis Pérez
ID: 37806022
If they will always be three folders, then you can do this:

Imports System.IO

Dim f As FileInfo = New FileInfo(s) 's will be C:\anyfolder1\anyfolder2\anyfolder3\anyfile.txt
Dim result As String = f.Directory.Parent.FullName 'this will give us the full path to the second directory

Open in new window


Hope that helps.
0
 
LVL 25

Expert Comment

by:Luis Pérez
ID: 37806035
If you want your final result be slash terminated ("c:\folder1\folder2\"), then you must check for it:

If (Not result.EndsWith("\")) Then result += "\"

Open in new window

0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 400 total points
ID: 37806183
Here is another option with simple string manipulation and without the need of file IO:
Dim s as String = "C:\Folder1\Folder2\Folder3\File1.txt"

Dim result as String = s.Substring(0, s.IndexOf("\", s.IndexOf("\", s.IndexOf("\") + 1) + 1) + 1)

Open in new window

0
 
LVL 1

Author Comment

by:yechan
ID: 37806202
Great.  Thanks for the responses.  wdosanjos is exactly what i was looking for.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question