Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

String manipulation

Posted on 2012-04-04
4
Medium Priority
?
214 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 1600 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
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…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

773 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