473,883 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Extract file name from path

It's probably very simple... I would like to know if there is a function
similar to ExtractFileName () (I've used it in Delphi), which returns only
the file name from the full path, e.g. "C:\Temp\MyFile .txt" would return
"MyFile.txt ".

Thanks!

- Kristoffer -
Nov 15 '05 #1
5 55050
L#
On Fri, 6 Feb 2004 13:47:57 +0100, "Kristoffer Persson" <hidden>
wrote:
It's probably very simple... I would like to know if there is a function
similar to ExtractFileName () (I've used it in Delphi), which returns only
the file name from the full path, e.g. "C:\Temp\MyFile .txt" would return
"MyFile.txt" .

Thanks!

- Kristoffer -


System.IO.Path. GetFileName(str ing path);
--
Ludwig
mailto:ludwig_( nospamplease)st uyck@pandora(no spamplease).be
Nov 15 '05 #2
hm, I found no function that performs this. so I suppose this will help
you...

string GetFileName(str ing szPath)

{

string szRetVal = string.Empty;

string szTmp = string.Empty;

int j = szPath.Length - 1;

while (szPath[j] != '\\')

{

szTmp += szPath[j];

j--;

}
// you have the reversed file name

// replace characters...
int i = szTmp.Length;

while (i != 0)

{

i--;

szRetVal += szTmp[i];

}

szTmp = null;

return szRetVal;

}
--
/*
Vladimir Scherbina,
Ukraine, Kiev.
*/

"Kristoffer Persson" <hidden> wrote in message
news:#e******** ******@TK2MSFTN GP10.phx.gbl...
It's probably very simple... I would like to know if there is a function
similar to ExtractFileName () (I've used it in Delphi), which returns only
the file name from the full path, e.g. "C:\Temp\MyFile .txt" would return
"MyFile.txt ".

Thanks!

- Kristoffer -

Nov 15 '05 #3

Hi, Vladimir Scherbina!
hm, I found no function that performs this. so I suppose this will help
you...

string GetFileName(str ing szPath)

{

string szRetVal = string.Empty;

string szTmp = string.Empty;

int j = szPath.Length - 1;

while (szPath[j] != '\\')

{


I optimize your code ;-)

string GetFileName(str ing szPath)
{
string[] s_arr = szPath.Split(ne w char[]{'\\'});
return s_arr[s_arr.Length-1];
}

or

string GetFileName(str ing szPath)
{
Regex r = new Regex(@"\w+[.]\w+$+");
return r.Match(szPath) .Value;
}
--
Roman S. Golubin
ICQ UIN 63253392
go************* ****@arhcity.ru
Nov 15 '05 #4

Hi, Kristoffer Persson!
It's probably very simple... I would like to know if there is a function
similar to ExtractFileName () (I've used it in Delphi), which returns only
the file name from the full path, e.g. "C:\Temp\MyFile .txt" would return
"MyFile.txt ".


string ExtractFileName (string szPath)
{
Regex r = new Regex(@"\w+[.]\w+$+");
return r.Match(szPath) .Value;
}
--
Roman S. Golubin
ICQ UIN 63253392
go************* ****@arhcity.ru
Nov 15 '05 #5
The static methods of Path give you a myriad of options to deal with file
names that include the path.

For example:

string myFile = "C:\Temp\MyFile .txt";
string filename = Path.GetFileNam e(myFile);

You could also get the name without extension:

filename = Path.GetFileNam eWithoutExtensi on(myFile);

Just browse through all the different methods that Path gives you to see
what else you can do.

- Robert Sentgerath -
Path.GetFileNam eWithoutExtensi on
"Kristoffer Persson" <hidden> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
It's probably very simple... I would like to know if there is a function
similar to ExtractFileName () (I've used it in Delphi), which returns only
the file name from the full path, e.g. "C:\Temp\MyFile .txt" would return
"MyFile.txt ".

Thanks!

- Kristoffer -

Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4801
by: mtech1 | last post by:
Access 2000 I need to extract just the filename from a path. Ex: path is- c:\MyApp\Sub\MyApp.exe I need to capture just the file name regardless of the size of it's name. For the above path it would be "MyApp.exe" As Always
39
2672
by: Joe Laughlin | last post by:
If I have a character array with "/some/file/directory/file_name", are there any functions / libraries that I could use to separate the directory ("some/file/directory") from the file name ("file_name"). I looked at sscanf(), but that didn't seem to do what I wanted. Thanks, Joe
3
3629
by: Bill | last post by:
Is there a method that will take a full path and filename as a parameter and return just the filename? I am currently using the following code but it is extremely slow for files on a DVD. for (int I = 0; I < openVobDialog.FileNames.Length; I++) { FileInfo vobInfo = new FileInfo(openVobDialog.FileNames); vobListBox.Items.Add(vobInfo.Name); }
1
2262
by: Miki Peric | last post by:
Which .Net function I should use to extract file name from URL/path?
5
22044
by: steve | last post by:
How can someone extract the file name and file path from a complete path? ex: c:\mydir\subdir\temp\myfile.txt will give: path = c:\mydir\subdir\temp filename = myfile.txt TIA
4
18640
by: Alan T | last post by:
I got a string. eg. C:\Temp\Documents\MyDoc.doc How do I get the name of the document only? ie. MyDoc.doc
3
1566
by: aGSMITH | last post by:
Hello All, I have an exportfile .txt from robocopy utility and I would need to get file name path from the file and save it to file1.txt after get the filename value and save it to file2.txt example of the export is: c:\test\test.txt D:\test\test.txt ..
3
1891
by: selvialagar | last post by:
Hi.............. I need to extract a textfile (.txt) file alone from the application startup path. That is I stored a text file in d:\text\inputfile.txt. The folder contains a text file.but i don't know the file name . Now i have to find the folder whiich contains a text file. If the folder contains a text file, i will take that for processing.Suppose if the folder does not contains the text file, i will show the openfiledialog to the user....
0
1441
by: maheen khan | last post by:
basically i extracted some attributes of file(name, file type, path etc) present in a directory and listed them in a c++ file, now i want to extract the file path only so that i can use it as an argument to open the respective file. thanks
0
9932
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9777
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11109
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10833
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10405
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9558
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7957
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7114
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4602
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.