Solved

Url To Filename

Posted on 2000-05-11
9
286 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
  • 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now