Solved

Url To Filename

Posted on 2000-05-11
9
301 Views
Last Modified: 2010-04-04
I need a way to take a full url, and get just the end filename into a string.

For Example, convert:
http://www.microsoft.com/windows/win2k/index.html

Into:
index.html

Thanks!
0
Comment
Question by:Psylord
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 3

Expert Comment

by:sburck
ID: 2800102
function GetFileNameFromURL(URL : string);
var
    i : integer;
begin
    if length(URL) = 0 then
    begin
        result := '';
        exit;
    end;
    i := length(URL);
    while ((i > 0) and (URL[i] <> '\')) do
        dec(i);
    if (URL[i] <> '\') then
        result := '';
    else
          if i = length(URL) then
               result := '';
          else
                result := Copy(URL,i+1,length(URL));
end;
0
 
LVL 3

Expert Comment

by:sburck
ID: 2800109
Whoops - make the first line
 
function GetFileNameFromURL(URL : string) :  string;

of course
0
 
LVL 3

Expert Comment

by:sburck
ID: 2800117
And, yet another obvious fix: reverse the slashes (use '/', not '\').  This is what happens when you're in a hurry to answer....
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Accepted Solution

by:
ITugay earned 10 total points
ID: 2800160
Why so difficult?

function ExtractURLFileName(aPath : string) : string;
var I : integer;
begin
   I:=length(aPath);
   while (I>0) and (aPath[I] <> '/') do dec(I);
   result:=Copy(aPath,I+1,length(aPath)-I);
end;
0
 
LVL 9

Expert Comment

by:ITugay
ID: 2800205
And again

function ExtractURLFileName(aPath : string) : string;
begin
   result:=ExtractFileName(StringReplace(aPath,'/','\',[rfReplaceAll]));
end;
0
 
LVL 3

Expert Comment

by:sburck
ID: 2800217
If you pass an empty string to the accepted answer,you'll get an exception. However, no arguments with the second answer, which is a nice idea.
0
 
LVL 9

Expert Comment

by:ITugay
ID: 2800219
Psylord,
do not forget "uses ...,sysutils,..." in last sample.

Cheers,
Igor
0
 
LVL 9

Expert Comment

by:ITugay
ID: 2800227
to sbruck,
did you try to pass empty string?
----
Igor
0
 
LVL 3

Expert Comment

by:sburck
ID: 2800263
Nope, and my apologies - I learned something new about the Copy function.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question