Solved

String manipulation

Posted on 2012-04-04
4
208 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

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 …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

752 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