Solved

String manipulation

Posted on 2012-04-04
4
203 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Great.  Thanks for the responses.  wdosanjos is exactly what i was looking for.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now