When you ask the question like that, no, but I've made strides:
$ perl mpj4.pl
file: /home/dan/Desktop/upload/382432_10150873103536275_1105470601_n.j
+pg
ext: jpg
filename: /home/dan/Desktop/upload/image_1.jpg
file: /home/dan/Desktop/upload/542274_470102786337441_1974797173_n.jpg
ext: jpg
filename: /home/dan/Desktop/upload/image_2.jpg
file: /home/dan/Desktop/upload/safe_image.php.jpeg
ext: jpeg
filename: /home/dan/Desktop/upload/image_3.jpeg
$ cat mpj4.pl
#!/usr/bin/perl -w
use strict;
my $path = '/home/dan/Desktop/upload/';
my @files = <$path*>;
my $counter = 0;
for my $img (@files) {
$counter++;
print "file: $img\n";
my $ext = ($img =~ m/([^.]+)$/)[0];
print "ext: $ext\n";
my $filename = "$path" . "image_". "$counter" . '.' . "$ext";
print "filename: $filename\n";
rename ($img, $filename) or warn "couldn't rename $img to $filename:
+ $!\n";
}
$
The situation that was really throwing me for a loop was that I'd write something to rename things, and then they'd all disappear. Until I put the asterisk in the diamond brackets, the directory did too! From what I read last night, this is something that happens to every perl learner at some point. I'm sure there's other ways of doing this, and I'd love to see them. I'll take a look a look at File::Copy when I get back to it.
I wanted to say a few words on where I'm going with this. I want to combine these scripts so as to have a directory that has renamed images, but with the correct extension:
$ perl lh1.pl
downloaded 4 images from https://sites.google.com/site/lutherhavennm/m
+ission
to folder site_8
$ cat lh1.pl
#!/usr/bin/perl -w
# creates a new directory and downloads images from url to it
# perlmonks node 965537; thx aaron and A.M.
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) {
my $url = $img->url_abs();
$counter++;
my $filename = $dir . "/image_" . $counter;
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!
}
}
}
$
After that, I want to create an html file that will have the stubouts for these images and their caption, but that has to be tomorrow. Cheers,
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.