473,408 Members | 2,477 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,408 software developers and data experts.

Regular Expression: how to get Filename from Path

ano
Hi,

I tried to use to code to get a file name from a file path but it's working
in all case.

string t12 = "\"C:\\Program Files\\Microsoft SQL
Server\\MSSQL.1\\MSSQL\\Binn\\sqlservr.exe";
string p12 = @"^.*\\";
string r12 = Regex.Replace(t12, p12, "");

I got a correct answer for this case, which is sqlservr.exe
but when my path name changed to

string t12 = "\"C:\\Program Files\\Microsoft SQL
Server\\MSSQL.1\\MSSQL\\Binn\\sqlservr.exe\" -sSQLEXPRESS";

I received a wrong answer: sqlservr.exe" -sSQLEXPRESS

Any idea?
Jul 6 '06 #1
4 17777
Hi

Did you try Path.GetFileName ?
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"ano" <an*@discussions.microsoft.comwrote in message
news:E5**********************************@microsof t.com...
Hi,

I tried to use to code to get a file name from a file path but it's
working
in all case.

string t12 = "\"C:\\Program Files\\Microsoft SQL
Server\\MSSQL.1\\MSSQL\\Binn\\sqlservr.exe";
string p12 = @"^.*\\";
string r12 = Regex.Replace(t12, p12, "");

I got a correct answer for this case, which is sqlservr.exe
but when my path name changed to

string t12 = "\"C:\\Program Files\\Microsoft SQL
Server\\MSSQL.1\\MSSQL\\Binn\\sqlservr.exe\" -sSQLEXPRESS";

I received a wrong answer: sqlservr.exe" -sSQLEXPRESS

Any idea?

Jul 6 '06 #2
Hello!
Did you try Path.GetFileName ?
Yeah, this is what I'd have asked, too. Regular expressions sounds like the
wrong tool for this.

Ano, try this code instead:

string t12 = "\"C:\\Program Files\\Microsoft SQL " +
"Server\\MSSQL.1\\MSSQL\\Binn\\sqlservr.exe\" -sSQLEXPRESS";
string[] parts = t12.Split('\"');
string filename = System.IO.Path.GetFileName(parts[1]);
MessageBox.Show(filename);

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
ja***@removethis.dystopia.fi
http://www.saunalahti.fi/janij/
Jul 6 '06 #3
System.IO.Path.GetFileName(string path)

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

Big thicks are made up of lots of little thins.
"ano" <an*@discussions.microsoft.comwrote in message
news:E5**********************************@microsof t.com...
Hi,

I tried to use to code to get a file name from a file path but it's
working
in all case.

string t12 = "\"C:\\Program Files\\Microsoft SQL
Server\\MSSQL.1\\MSSQL\\Binn\\sqlservr.exe";
string p12 = @"^.*\\";
string r12 = Regex.Replace(t12, p12, "");

I got a correct answer for this case, which is sqlservr.exe
but when my path name changed to

string t12 = "\"C:\\Program Files\\Microsoft SQL
Server\\MSSQL.1\\MSSQL\\Binn\\sqlservr.exe\" -sSQLEXPRESS";

I received a wrong answer: sqlservr.exe" -sSQLEXPRESS

Any idea?

Jul 6 '06 #4
ano wrote:
Hi,

I tried to use to code to get a file name from a file path but it's
working in all case.

string t12 = "\"C:\\Program Files\\Microsoft SQL
Server\\MSSQL.1\\MSSQL\\Binn\\sqlservr.exe";
string p12 = @"^.*\\";
string r12 = Regex.Replace(t12, p12, "");

I got a correct answer for this case, which is sqlservr.exe
but when my path name changed to

string t12 = "\"C:\\Program Files\\Microsoft SQL
Server\\MSSQL.1\\MSSQL\\Binn\\sqlservr.exe\" -sSQLEXPRESS";

I received a wrong answer: sqlservr.exe" -sSQLEXPRESS
Well, your second example is not a path - it's a command line, including
arguments. First you have to remove the arguments. There's no standard way
to do that, but generally it'll be everything after the first whitespace, or
if the path portion begins with a quotation mark, then everything after the
second quotation mark.

Once you've separated the path from the arguments, use System.IO.Path to
split out the parts of the path, as others have suggested.

-cd
Jul 6 '06 #5

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

Similar topics

7
by: MichaelPyles | last post by:
Using IIS 5.1, XP, dev studio 2003, ASP I've discovered an insidious bug with the VBScript Regular Expression object. I set the pattern to consist of the set of invalid filename characters plus...
8
by: Rajeev Soni | last post by:
Hi I am looking for the regular expression for validating the allowed file types to upload like files like "zip,pdf,doc,rtf,gif,jpg,png,txt"; and the expression should not be case sensitive like...
3
by: ksr | last post by:
Hi, I am looking for a regular expression that would extract UNC paths from a given string and place that inside a href. Currently the expression fails if there is a space in the path.. eg....
3
by: rodchar | last post by:
hey all, what would my expression look like if i wanted to make sure that the input matched the following pattern. c:\filename.ext it doesn't have to be the c drive just a letter, colon,...
1
by: vtxr1300 | last post by:
I'm having a problem with a regular expression in conjunction with the regular expression validator. I am trying to make sure that when a user browses for a file to upload, it ends in gif, jpeg or...
3
by: TOXiC | last post by:
Hi everyone, First I say that I serched and tryed everything but I cannot figure out how I can do it. I want to open a a file (not necessary a txt) and find and replace a string. I can do it...
4
by: bizt | last post by:
Hi, Im looking for a regular expression to check that a entered filename is valid. I have a cms Im building where the user can enter the name of a file, enter content and then click Save where...
1
by: satpo | last post by:
I have a file upload control in which I want to allow only pdf files. I am using the following validation expression "^((^$)|(((:)|(\\{2}\w+)\$?)(\\(\w.*))(.pdf|.PDF)$))". It works fine except...
47
by: Henning_Thornblad | last post by:
What can be the cause of the large difference between re.search and grep? This script takes about 5 min to run on my computer: #!/usr/bin/env python import re row="" for a in range(156000):...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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...
0
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...

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.