Solved

Url To Filename

Posted on 2000-05-11
9
298 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
Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
Intraweb download file link ? 1 180
Strange behavior when a form is closed 6 74
firemonkey keyboard covers the controls 1 78
delphi popmenu non latine charcters 3 45
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 Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

738 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