473,396 Members | 1,760 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,396 software developers and data experts.

Trimming a string for a filename

38
Situation : Need to get a list of files in a directory, but I am currently getting the whole path of the file (Example: "C:\A Folder\Another Folder\file.txt"). What I want is just the filename and extension. So from above example, all I actually want is "file.txt".

Language: VB.NET 2005

Current Code:

Expand|Select|Wrap|Line Numbers
  1. Imports System.Collections.ObjectModel
  2. ...
  3. Dim files As ReadOnlyCollection(Of String)
  4. files = My.Computer.FileSystem.GetFiles(strCurDir)
  5.  
Obviously, the GetFiles function returns a ReadOnlyCollection(Of String), so I cannot simply change it to below...

Expand|Select|Wrap|Line Numbers
  1. Imports System.Collections.ObjectModel
  2. ...
  3. Dim files As ReadOnlyCollection(Of File)
  4. files = My.Computer.FileSystem.GetFiles(strCurDir)
  5.  
With just a string of something like "C:\Folder\Folder\Folder\somefile.txt", what would be the easiest method of getting everything after the last "\"?
Jul 12 '06 #1
3 1941
intStartPos=strFullFileName.LastIndexOf("\\")+1;
strFileName=strFullFileName.Substring(intStartPos, strFullFileName.Length-intStartPos);

the above code would do. Remove the ; if you are using VB.NET
Jul 13 '06 #2
Enyi
38
Ah ok, Thank you
Jul 14 '06 #3
zamkam
3
There's an easier way: System.IO.Path.GetFileName(strFilePath).
Jul 14 '06 #4

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

Similar topics

2
by: Simon | last post by:
Hi there. I seem to be a flurry of questions here recently, thanks for you help all, but ... got another question here. I am storing URL in a database for a 'sort of' directory type website. ...
6
by: Generic Usenet Account | last post by:
I have worked out the following implementation for trimming the leading and trailing whitespace characters of a string (I am surprised not to see these as member functions). Am I doing it...
12
by: Stefan Weiss | last post by:
Hi. (this is somewhat similar to yesterday's thread about empty links) I noticed that Tidy issues warnings whenever it encounters empty tags, and strips those tags if cleanup was requested....
7
by: ColinWard | last post by:
Hello. I use the following line which returns the path of a file and adds it to a listbox. How can I modify this line so that it only returns the filename and not the entire path? -->...
13
by: john_g83 | last post by:
have a bit of c code that is ment to take a string (that may or may not have spaces before or after the string) i.e. " stuff ", and trims off the whitespace before and after. Code: char *trim...
0
by: Paul | last post by:
On my local site, I have a folder that is security trimmed, so that only members of a Role can see it after they register and log on (I set the memberships). All works fine locally. However,...
8
by: PvtPile | last post by:
I've got a form where i need to pull both the directory of a filename and the directory with the filename of an .inf file... I figured the easiest way to do this was to have an <input id=test...
3
by: Fir5tSight | last post by:
Hi All, I have an interface class defined as follows: class FileName : IComparable { public FileName(string fileName, string packageName) { // // TODO: Add constructor logic here
5
by: vinod allapu | last post by:
Hi all I have a string like this "2/6/1986" which is a date..Remember that it is a date actually..so day size and month size are variant..It is not always single digit or single character... I...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.