Solved

String manipulation

Posted on 2012-04-04
4
206 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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

856 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