473,396 Members | 1,892 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.

Parse string url to get filename

Hello,

How do I parse a string that contains a URL and get the filename only?
i.e.

string tmp = "http://test.com/downloads/test.exe";
I just want the test.exe..

Thanks in advance.

KC

Nov 16 '05 #1
5 25565
On 10 Dec 2004 11:52:22 -0800, Netmonster wrote:
Hello,

How do I parse a string that contains a URL and get the filename only?
i.e.

string tmp = "http://test.com/downloads/test.exe";
I just want the test.exe..


Use a combination of Substring and LastIndexOf.
--
Tom Porterfield
Nov 16 '05 #2
The following is some code that searches an existing string for two other
strings (start marker and end marker), then gets only the required piece out
of the middle.
const string MODULE_LABEL = "/module";

// Get the command line parameters/arguments

ls_Argument = Environment.GetCommandLineArgs();

ls_Command_Line = String.Join(" ", ls_Argument);

ls_Command_Line = ls_Command_Line.Trim();

// Find the beginning of the /module parameter value

li_Position_Start = ls_Command_Line.IndexOf(MODULE_LABEL);

if (li_Position_Start == -1)

return -2011;

li_Position_Start += MODULE_LABEL.Length;

// Find end of the /module parameter value

li_Position_End = ls_Command_Line.IndexOf("/", li_Position_Start + 1);

if (li_Position_End == -1)

{

// use remainder of string

li_Position_End = ls_Command_Line.Length;

}

else

{

li_Position_End --;

}

// Get Module parameter value

ls_Parameter_Value = ls_Command_Line.Substring(li_Position_Start,
li_Position_End - li_Position_Start);

ls_Parameter_Value = ls_Parameter_Value.Trim();

"Netmonster" <ne***********@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hello,

How do I parse a string that contains a URL and get the filename only?
i.e.

string tmp = "http://test.com/downloads/test.exe";
I just want the test.exe..

Thanks in advance.

KC

Nov 16 '05 #3
Tom Porterfield wrote:
On 10 Dec 2004 11:52:22 -0800, Netmonster wrote:

Hello,

How do I parse a string that contains a URL and get the filename only?
i.e.

string tmp = "http://test.com/downloads/test.exe";
I just want the test.exe..

Use a combination of Substring and LastIndexOf.


Something like:

string tmp = "http://test.com/downloads/test.exe";
int LastIndex = tmp.LastIndexOf("/");
tmp = tmp.Substring(LastIndex+1,(tmp.Length - LastIndex -1));
MessageBox.Show(tmp);

That works.

There are other ways to do it as well, such as using the String.Split()
function to split the string on the "/" char and then take the last item
in the array.

Lowell
Nov 16 '05 #4

"Netmonster" <ne***********@yahoo.com> ha scritto nel messaggio
news:11**********************@f14g2000cwb.googlegr oups.com...
Hello,

How do I parse a string that contains a URL and get the filename only?
i.e.

string tmp = "http://test.com/downloads/test.exe";
I just want the test.exe..

using System;
using System.IO;

class RemovePath
{
public RemovePath()
{
}
public static void Main(string[] args)
{
Console.WriteLine(System.IO.Path.GetFileName(args[0]));
}
}

Nov 16 '05 #5
Lowell,

Thanks.... thats exactly what I was looking for.

KC

Nov 16 '05 #6

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

Similar topics

6
by: chuck amadi | last post by:
Hi , Im trying to parse a specific users mailbox (testwwws) and output the body of the messages to a file ,that file will then be loaded into a PostGresql DB at some point . I have read the...
1
by: chuck amadi | last post by:
By the way list is there a better way than using the readlines() to > > >parse the mail data into a file , because Im using > > >email.message_from_file it returns > > >all the data i.e reads one...
0
by: precious | last post by:
i'm trying to split my xml configuration file to files implementing the parsing using xerces in java (DOMparser). my project uses a "fromXML" parsing design and here's what i tried in the node...
19
by: Johnny Google | last post by:
Here is an example of the type of data from a file I will have: Apple,4322,3435,4653,6543,4652 Banana,6934,5423,6753,6531 Carrot,3454,4534,3434,1111,9120,5453 Cheese,4411,5522,6622,6641 The...
1
by: Lucas Tam | last post by:
Hello, Does anyone know of a VB.NET class that can parse a string based on an input mask? I would like to parse a string like: %Filename% - %Date%-%Number% Of course the format of the...
2
by: rajarshi.guha | last post by:
Hi, recently having discovered ElementTree I'm stumped by a very simple problem, which I can't find the answer to. I have some XML in a string object. Now the parse() method of ElementTree takes...
2
by: muralibala68 | last post by:
Hi, How do I parse an xml message that is in std::string using sax2 parse method that does not seem to take a string and expects a filename? Thanks.
4
by: noobles | last post by:
I'm trying to make a command line parser, but I'm not exactly sure where to put my file.txt file so that this thing can read it. The most logical place seemed to be the directory the project was...
9
akohistani
by: akohistani | last post by:
I am having Parse error problem with my newly purchased Php upload script I have uploaded the script and I get the error below Parse error: syntax error, unexpected $end in URL/functions.php on...
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: 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
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
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,...
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
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
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.