Solved

Url To Filename

Posted on 2000-05-11
9
295 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

762 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