473,698 Members | 2,376 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 25587
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.Get CommandLineArgs ();

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

ls_Command_Line = ls_Command_Line .Trim();

// Find the beginning of the /module parameter value

li_Position_Sta rt = ls_Command_Line .IndexOf(MODULE _LABEL);

if (li_Position_St art == -1)

return -2011;

li_Position_Sta rt += MODULE_LABEL.Le ngth;

// Find end of the /module parameter value

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

if (li_Position_En d == -1)

{

// use remainder of string

li_Position_End = ls_Command_Line .Length;

}

else

{

li_Position_End --;

}

// Get Module parameter value

ls_Parameter_Va lue = ls_Command_Line .Substring(li_P osition_Start,
li_Position_End - li_Position_Sta rt);

ls_Parameter_Va lue = ls_Parameter_Va lue.Trim();

"Netmonster " <ne***********@ yahoo.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.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(L astIndex+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.goo glegroups.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.WriteLi ne(System.IO.Pa th.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
7743
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 email posts and been advised to use the email Module and mailbox Module. The blurb from a memeber of this list . Im not at work at the moment So I cant test this out , but if someone could take a look and check that im on the write track as this...
1
2395
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 entire line from the file , headers as well > > >as just the desired body messages . > > > > > >fp = file("/home/chuck/pythonScript/testbox") > > >mb = mailbox.UnixMailbox(fp, > > >email.message_from_file)
0
5103
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 that i wanted outside - public void fromXML(Node element) { if (element.hasAttribute("fileName")){ String fileName= element.getAttribute("fileName"); element = getDOMParser().parse(fileName).getDocumentElement(); } ... continue as if it were the...
19
3213
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 first position is the info (the product) I want to retreive for the corresponding code. Assuming that the codes are unique for each product and all code data is on one line.
1
2421
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 string could change like: FillerText%Filename% - %Date%-%Number%.zip
2
3493
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 a filename or file-like object. So I tried creating a StringIO object from the original string and then giving that to parse(). But that does not seem to work. Any pointers to getting ElementTree to parse from a string would be appreciated (of...
2
5429
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
1942
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 in, but that didn't work. #include <fstream> #include <iostream> #include <string> #include <cstdlib>
9
3527
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 line 400 Someone please help me I contacted the owner of the script where I purchased he is not replying me I bought it from http://www.scubadivingcalculators.com/ and this is the url where I have uploaded the script...
0
8608
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
9161
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...
0
9029
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8897
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
7732
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
6522
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
5860
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();...
0
4370
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2332
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.