Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: renaming all files in a directory

by Anonymous Monk
on Jun 11, 2012 at 08:48 UTC ( [id://975519]=note: print w/replies, xml ) Need Help??


in reply to renaming all files in a directory

use perl -MO=B::Deparse,-p myfile.pl to see how perl parses your program

read glob give glob a star (the metacharacter "*")

Also see Re: Renaming files in a directory in sequence

Replies are listed 'Best First'.
Re^2: renaming all files in a directory
by Aldebaran (Curate) on Jun 11, 2012 at 22:18 UTC

    I took a look at your thread at that link but don't necessarily follow it all yet. I don't think I want to glob for any of this. Does this look like a fairly robust way to download images and rename them uniformly but with correct filetypes?

    $ perl lh2.pl img: WWW::Mechanize::Image=HASH(0xa1fdc5c) https://sites.google.com/site/lutherhavennm/_/rsrc/1255408739014/missi +on/Attachment3.jpg?height=278&width=420 ext: jpg?height=278&width=420 ext: jpg img: WWW::Mechanize::Image=HASH(0xa207784) https://sites.google.com/site/lutherhavennm/_/rsrc/1255386973661/missi +on/Picture1.jpg?height=279&width=420 ext: jpg?height=279&width=420 ext: jpg img: WWW::Mechanize::Image=HASH(0xa2078ec) https://sites.google.com/site/lutherhavennm/_/rsrc/1255408642180/missi +on/Attachment10.jpg?height=280&width=420 ext: jpg?height=280&width=420 ext: jpg img: WWW::Mechanize::Image=HASH(0xa2074dc) https://sites.google.com/site/lutherhavennm/_/rsrc/1255387202014/missi +on/Looking%20up%20at%20the%20Bldg.JPG?height=315&width=420 ext: JPG?height=315&width=420 ext: JPG downloaded 4 images from https://sites.google.com/site/lutherhavennm/m +ission to folder site_17 $ cat lh2.pl #!/usr/bin/perl -w use strict; use feature ':5.10'; use WWW::Mechanize; use LWP::Simple; use Errno qw[ EEXIST ]; # get information about images my $domain = 'https://sites.google.com/site/lutherhavennm/mission'; my $m = WWW::Mechanize->new(); $m->get($domain); my @list = $m->images(); # create new folder and download images to it. my $counter = 0; my $dir = &mk_new_dir; for my $img (@list) { print "img: $img\n"; my $url = $img->url_abs(); print "$url \n"; my $ext = ($url =~ m/([^.]+)$/)[0]; print "ext: $ext\n"; $ext =~ s/\?.+//; print "ext: $ext\n"; $counter++; my $filename = $dir . "/image_" . $counter. '.' . $ext; getstore( $url, $filename ) or die "Can't download '$url': $@\n"; } # output print "downloaded ", $counter, " images from ", $domain, "\n"; print "to folder ", $dir, "\n"; sub mk_new_dir { my $counter2 = 1; while (1) { my $word = "site"; my $name = $word . '_' . $counter2++; if ( mkdir $name, 0755 ) { return $name; # success, return new dir name } else { next if $!{EEXIST}; # mkdir failed because file exists die sprintf "(%d) %s", $!, $!; # other failure; bail ou +t! } } } $

    Is this how all images on the net really look to servers, with the size information on them as well? Where do I find information about the url_abs() method here? I don't see it in either of the modules used.

      No.

      And the first few lines look as though you're trying to make something up as you go; don't know how to cut'n'paste; or are yanking our chains.

      Or, maybe, need this reminder:

      Please, separate your record of output from your code.

        Alright, well I previewed the terminal output as a normal paragraph, and that doesn't work at all because the newlines disappear, so I hope the best way is to use separate code blocks for terminal output and then a code listing.

        $ perl lh2.pl img: WWW::Mechanize::Image=HASH(0xa8acc5c) https://sites.google.com/site/lutherhavennm/_/rsrc/1255408739014/missi +on/Attachment3.jpg?height=278&width=420 ext: jpg?height=278&width=420 ext: jpg img: WWW::Mechanize::Image=HASH(0xa8b67cc) https://sites.google.com/site/lutherhavennm/_/rsrc/1255386973661/missi +on/Picture1.jpg?height=279&width=420 ext: jpg?height=279&width=420 ext: jpg img: WWW::Mechanize::Image=HASH(0xa8b6934) https://sites.google.com/site/lutherhavennm/_/rsrc/1255408642180/missi +on/Attachment10.jpg?height=280&width=420 ext: jpg?height=280&width=420 ext: jpg img: WWW::Mechanize::Image=HASH(0xa8b6524) https://sites.google.com/site/lutherhavennm/_/rsrc/1255387202014/missi +on/Looking%20up%20at%20the%20Bldg.JPG?height=315&width=420 ext: JPG?height=315&width=420 ext: JPG downloaded 4 images from https://sites.google.com/site/lutherhavennm/m +ission to folder site_18 $ cat lh2.pl
        #!/usr/bin/perl -w use strict; use feature ':5.10'; use WWW::Mechanize; use LWP::Simple; use Errno qw[ EEXIST ]; # get information about images my $domain = 'https://sites.google.com/site/lutherhavennm/mission'; my $m = WWW::Mechanize->new(); $m->get($domain); my @list = $m->images(); # create new folder and download images to it. my $counter = 0; my $dir = &mk_new_dir; for my $img (@list) { print "img: $img\n"; my $url = $img->url_abs(); print "$url \n"; my $ext = ($url =~ m/([^.]+)$/)[0]; print "ext: $ext\n"; $ext =~ s/\?.+//; print "ext: $ext\n"; $counter++; my $filename = $dir . "/image_" . $counter. '.' . $ext; getstore( $url, $filename ) or die "Can't download '$url': $@\n"; } # output print "downloaded ", $counter, " images from ", $domain, "\n"; print "to folder ", $dir, "\n"; sub mk_new_dir { my $counter2 = 1; while (1) { my $word = "site"; my $name = $word . '_' . $counter2++; if ( mkdir $name, 0755 ) { return $name; # success, return new dir name } else { next if $!{EEXIST}; # mkdir failed because file exists die sprintf "(%d) %s", $!, $!; # other failure; bail ou +t! } } } $

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://975519]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-24 07:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found