grabbing file name from URL 
Author Message
 grabbing file name from URL

Hi,

I'm trying to write a perl program that will grab the filename from a URL.

ex. I want to grab the string "image1.jpg" out of the following URL:
http://www.*-*-*.com/

The URL will not always have the same directory structure or file type. I'm
not really sure how to go about doing this, as i'm new to Perl. I was
thinking that it would be easiest using a regular expression?

Any help or pointers to examples/faqs would be appreciated!
Thanks a bunch!
Pritesh



Thu, 23 Sep 2004 14:07:03 GMT  
 grabbing file name from URL

Quote:
>I'm trying to write a perl program that will grab the filename from a URL.
>ex. I want to grab the string "image1.jpg" out of the following URL:
>http://www.mywebsite.com/images/art/image1.jpg
>The URL will not always have the same directory structure or file type. I'm
>not really sure how to go about doing this, as i'm new to Perl. I was
>thinking that it would be easiest using a regular expression?

This should do what you want:

$url = "http://www.mywebsite.com/images/art/image1.jpg";
if ($url =~ /.*\/(.*)$/){    # Match anything between the last slash "/" and
end of the line.
print $1;                         # Print the match

Quote:
}

Regards,
Chris


Thu, 23 Sep 2004 16:04:37 GMT  
 grabbing file name from URL

Quote:
> I'm trying to write a perl program that will grab the filename from a URL.
> ex. I want to grab the string "image1.jpg" out of the following URL:
> http://www.mywebsite.com/images/art/image1.jpg

There may be holes in the following but it worked for
a recent URL grabbing project that I was doing informally...

This can be done in one 'grab' but I was doing some other
stuff at the same time.

# If it's an URL grab it "()" as $1 and put it into $localFile
    if (/"(http:\/\/[^"\/]+\/[^"]+)"/) {
      $localFile = $1;
## The above matched the URL between quotes -- if you don't have
## quotes then just match the URL in your source starting with http://

#  Then substitute out everything up to the final "/"
      $localFile =~ s/http:\/\/.+\/([^\/]+)$/$1/;
   }

Another trick (that I forgot when writing the above) is to use a
DIFFERENT regex 'bracket' instead of the slashes.  As long
as you plane 'm' or 's' in front of the regex you can pick a different
character -- presumably one that is NOT in the match expression
and therefore will require no quoting.

      $localFile =~ s#http://.+/([^/]+)$#$1#;

Much easier to read.

Herb Martin
Try ADDS for great Weather too:
http://adds.aviationweather.noaa.gov/projects/adds



Thu, 23 Sep 2004 16:00:36 GMT  
 grabbing file name from URL
Thanks Herb, and Chris! Exactly what i was looking for. I'll give this a
try.

Thanks again
Pritesh


Quote:
> >I'm trying to write a perl program that will grab the filename from a
URL.

> >ex. I want to grab the string "image1.jpg" out of the following URL:
> >http://www.mywebsite.com/images/art/image1.jpg

> >The URL will not always have the same directory structure or file type.
I'm
> >not really sure how to go about doing this, as i'm new to Perl. I was
> >thinking that it would be easiest using a regular expression?

> This should do what you want:

> $url = "http://www.mywebsite.com/images/art/image1.jpg";
> if ($url =~ /.*\/(.*)$/){    # Match anything between the last slash "/"
and
> end of the line.
> print $1;                         # Print the match
> }

> Regards,
> Chris



Fri, 24 Sep 2004 02:28:59 GMT  
 grabbing file name from URL

(snipped)

Quote:
> > I'm trying to write a perl program that will grab the filename from a URL.
> > ex. I want to grab the string "image1.jpg" out of the following URL:
> > http://www.mywebsite.com/images/art/image1.jpg
> There may be holes in the following but it worked for
> a recent URL grabbing project that I was doing informally...

It would be prudent of you to test your code before posting.

Godzilla!

--

-----------== Posted via Newsfeeds.Com, Uncensored Usenet News ==----------
   http://www.newsfeeds.com       The Largest Usenet Servers in the World!
------== Over 100,000 Newsgroups - Ulimited downloads - 19 servers ==-----



Fri, 24 Sep 2004 22:17:26 GMT  
 grabbing file name from URL
I did.  If there is an error, then point it out.

Perhaps it was mangled when I cut and pasted it
into the news message but it worked when I ran
it -- and such a short segment was to show the idea
in any case.

Herb Martin, PP-SEL
(...and aerobatic student)
Try ADDS for great Weather too:
http://adds.aviationweather.noaa.gov/projects/adds


Quote:

> (snipped)

> > > I'm trying to write a perl program that will grab the filename from a
URL.
> > > ex. I want to grab the string "image1.jpg" out of the following URL:
> > > http://www.mywebsite.com/images/art/image1.jpg

> > There may be holes in the following but it worked for
> > a recent URL grabbing project that I was doing informally...

> It would be prudent of you to test your code before posting.

> Godzilla!

> --

> -----------== Posted via Newsfeeds.Com, Uncensored Usenet News
==----------
>    http://www.newsfeeds.com       The Largest Usenet Servers in the World!
> ------== Over 100,000 Newsgroups - Ulimited downloads - 19 servers ==-----



Fri, 24 Sep 2004 23:53:24 GMT  
 grabbing file name from URL

(snipped)

Quote:
> > > There may be holes in the following but it worked for
> > > a recent URL grabbing project that I was doing informally...
> > It would be prudent of you to test your code before posting.
> I did.  If there is an error, then point it out.
> Perhaps it was mangled when I cut and pasted it

I carefully examined my test procedure. I mangled a variable
name. I did not notice your capital "F" in $localFile and
used $localfile instead within my if / else conditionals
for methods two and three.

There is only one error instead of three errors. Another
error, a semantic error, is indicating a lack of quote
marks and matching for quotes although correct results
are produced for methods two and three. This is of no
real significance, matter of writing error only, unless
those quote marks are missing. In this case of missing
quote marks for the original URL string, all three
methods will produce incorrect results.

My fault not noticing your capitalization of a single letter.

Godzilla!
--

#!perl

print "Content-type: text/plain\n\n";

print "Expected Results:\n";

$url = "http://www.mywebsite.com/images/art/image1.jpg";

$tail = substr ($url, rindex ($url, "/") + 1);

print "  $tail";

print "\n\n";

print "Your Results:\n";

$url = "\"http://www.mywebsite.com/images/art/image1.jpg\"";

if ($url =~ /.*\/(.*)$/)
 { print "  First Method: $1\n"; }
else
 { print "  First Method: Matching Failed\n"; }

$url = "\"http://www.mywebsite.com/images/art/image1.jpg\"";

    if ($url =~/"(http:\/\/[^"\/]+\/[^"]+)"/) {
      $localFile = $1;
      $localFile =~ s/http:\/\/.+\/([^\/]+)$/$1/;
   }

if ($localFile)
 { print "  Second Method: $localFile\n"; }
else
 { print "  Second Method: Matching Failed\n"; }

$url = "\"http://www.mywebsite.com/images/art/image1.jpg\"";

    if ($url =~/"(http:\/\/[^"\/]+\/[^"]+)"/) {
      $localFile = $1;
      $localFile =~ s#http://.+/([^/]+)$#$1#;
   }

if ($localFile)
 { print "  Third Method: $localFile\n"; }
else
 { print "  Third Method: Matching Failed\n"; }

exit;

PRINTED RESULTS:
________________

Expected Results:
  image1.jpg

Your Results:
  First Method: image1.jpg"
  Second Method: image1.jpg
  Third Method: image1.jpg

--

-----------== Posted via Newsfeeds.Com, Uncensored Usenet News ==----------
   http://www.newsfeeds.com       The Largest Usenet Servers in the World!
------== Over 100,000 Newsgroups - Ulimited downloads - 19 servers ==-----



Sat, 25 Sep 2004 02:44:35 GMT  
 grabbing file name from URL

Quote:
> I'm trying to write a perl program that will grab the filename from a URL.
> ex. I want to grab the string "image1.jpg" out of the following URL:
> http://www.mywebsite.com/images/art/image1.jpg

> The URL will not always have the same directory structure or file type.
I'm
> not really sure how to go about doing this, as i'm new to Perl. I was
> thinking that it would be easiest using a regular expression?

You could possibly use REs, but File::Basename does the job already.

jue



Sun, 26 Sep 2004 01:13:45 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. perl and grabbing HTML files using a URL via the web

2. grabbing name of http upload file

3. Parsing file name out of URL pathname

4. Change URL to directory and file name

5. How do you grab a URL in perl?

6. How do you grab a URL in perl?

7. Grabbing the title of a URL

8. newbie: Grab URL into PERL?

9. Why can't I grab this URL?

10. How do you grab a URL in perl?

11. Grabbing module name on the fly

12. Grab the user name under apache?

 

 
Powered by phpBB® Forum Software