473,378 Members | 1,412 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Return folder name from file location path

6
Hi,
How would I retrive the folder name a file is in?
In this example I just want the highlighted folder name:

C:\Program Files\FolderNameIWant\Application.exe

I can get the path excluding the file using various methods such as:

Expand|Select|Wrap|Line Numbers
  1. Dim intPos As Integer
  2. intPos = FileFullPath.LastIndexOfAny("\")
  3. intPos += 1
  4. Messagebox.show(FileFullPath.Substring(0, intPos))
  5.  
But cannot figure out how to return just the top level folder!

Thanks.
Jun 4 '10 #1
6 18517
tlhintoq
3,525 Expert 2GB
Use string.split to separate on the '\' character
The folder name will be the last element of the returned array.

There are a number of methods in the System.IO.Path namespace that might make things easier on you as well.
There are existing methods for GetFileName, RootPath, GetFileNameWithoutExtension and so on
Jun 4 '10 #2
madlan
6
I tried using Split but it seems to return the string in it's original format?

strCustomerName.Split(New Char() {"\"c})
MessageBox.Show(strCustomerName)

I could not find anything in the System.Io.Path that would be of any use.
Jun 4 '10 #3
tlhintoq
3,525 Expert 2GB
Then you didn't bother to read about how string.split works.

Here is some general good advice. Don't just assume you know how a function will work based on its name and your guesswork. You could spend a lot of time dinking around with it, tons of trial and error and eventually figure out what it does. Or you could just go to the MSDN and read about and look at the explanation and samples.
http://msdn.microsoft.com/en-us/libr...v=VS.100).aspx

String.split does not affect or alter your original string. It is a method that returns an array of strings.

string[] Yogi = string.split("c:\one\two\three", '\');

Will assign elements to the new array[] Yogi as follows
[0] = c:
[1] = one
[2] = two
[3] = three

So if you take your total GetPathWithoutFileName
.split() it on the '\'
the last element will be the folder name.
Jun 4 '10 #4
madlan
6
Directory.Name does the job:

Expand|Select|Wrap|Line Numbers
  1. Dim filepath As String = "C:\Program Files\FolderNameIWant\cats\Application.exe"
  2.             Dim fi As New IO.FileInfo(filepath)
  3.             MessageBox.Show(fi.Directory.Name)
Thanks for your advice.
Jun 4 '10 #5
Expand|Select|Wrap|Line Numbers
  1. 1.Dim filepath As String = "C:\Program Files\FolderNameIWant\cats\Application.exe"
  2. 2.             Dim fi As New IO.FileInfo(filepath)
  3. 3.             MessageBox.Show(fi.Name)
  4.  
fi.name returns the Folder
Apr 15 '14 #6
Name = Name.Substring(InStrRev(Name, "\"))
Oct 9 '14 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: davidgordon | last post by:
Hi, I'm using the FileOpenDialog API that everyone has told me about which is posted everywhere on this forum.....works like a dream. When I select a file I get the full path such as : ...
4
by: Patrick Olurotimi Ige | last post by:
I have a Virtual directory FORUMS and the a folder CORP under that Directory. And i want to add a location path to this folder in web.config but after trying the below code it doesn't seem to...
1
by: Patrick Olurotimi Ige | last post by:
Using this location path below it relates only to the ADMINSITE folder/Directory only. Which allows only Admin Groups to the "AdminSite" directory. But if i try using ADMIN\AdminSite in the...
3
by: Able | last post by:
Dear friends A string is either a foldername or a name of a single file. Do somebody know a proper way to qualify whether the string is the one or the another? Regards Able
4
by: John Dann | last post by:
Is there any method in the File, Directory, FileInfo, Path etc classes that makes it easy to retrieve the folder name of the immediate parent folder of a file? I can't see one, but maybe I've...
5
by: _DG | last post by:
At least when you've got any docs in .CHM format in the folder tree. If you drop a file called CSharpDocs.chm into a folder: \toplevel\C#\docs, you can open it, but it won't display. The #...
0
by: Alex | last post by:
Hi, I made a deployment package that needs to install its files to a folder which name contains a single quote (it's in french). I changed the Application folder's default location to point to...
1
by: Luke Storer | last post by:
I have been making an .exe to empty the OLK folder created by outlook at the minute the folder location is hard written into the code e.g C:\Documents And Settings\Luke\Local Settings Is it...
11
by: =?Utf-8?B?UGF1bA==?= | last post by:
I did a google search but could find what I was looking for. I am creating a temporary folder and placing files in it with a web application. The temporary folder name needs to be changed...
4
by: sunilkds | last post by:
I want code for how to copy folder,delete folder from one location to another. Plz give example I tried in this code but i couldnt move folder but i moved files(coy), code, path =...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.