Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > moving unused of a website

Reply
Thread Tools

moving unused of a website

 
 
Marek
Guest
Posts: n/a
 
      04-15-2008


Hello all,


I am a perl beginner. So patience please!


I wrote a perl-script to find all unused pix of a web site. Now I
would like to move all those 168! pix from the folder pix to the
folder pix_out keeping the same hierarchy as in the original folder.
That means:

/Users/xxx/Documents/webpages/www.myproject.de/pix/fotos/thumbnails/
tn_munich28.jpg

should be renamed to:

/Users/xxx/Documents/webpages/www.myproject.de/pix_out/fotos/
thumbnails/tn_munich28.jpg

Only one folder is changing his name: pix -> pix_out And this with all
my 168 unused pix in different folders.

I was hoping, that the rename function would create the missing
folders, doing:

rename "/Users/xxx/Documents/webpages/www.myproject.de/pix/fotos/
thumbnails/tn_munich28.jpg", "/Users/xxx/Documents/webpages/
www.myproject.de/pix_out/fotos/thumbnails/tn_munich28.jpg";

But there is even no error message, trying to do it like this with one
test file. So probably I have to grep out the folders starting from
"pix"-folder until the file:

this would give in this example the folders fotos -> thumbnails which
are missing and which I have to create in pix_out - folder

My questions are:

How to grep these missing folders? Starting from pix, my hierarchy is
maximum 2 folders deep. But if there would be more folders?

how to test whether a folder is already created? I tried with:

if (-e ! $dir_pix_out)
#wrong!!
{
mkdir
$dir_pix_out;
}



thank you for your help



marek
 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      04-15-2008
Marek <(E-Mail Removed)> wrote in
news:3547d8bf-791e-4854-b2c8-
http://www.velocityreviews.com/forums/(E-Mail Removed)
m:

> I am a perl beginner.


You are a Perl beginner. Perl is the language, perl is the binary.

perldoc -q difference

> So patience please!
>
>
> I wrote a perl-script to find all unused pix of a web site. Now I
> would like to move all those 168! pix from the folder pix to the
> folder pix_out keeping the same hierarchy as in the original
> folder. That means:
>
> /Users/xxx/Documents/webpages/www.myproject.de/pix/fotos/
> thumbnails/tn_munich28.jpg
>
> should be renamed to:
>
> /Users/xxx/Documents/webpages/www.myproject.de/pix_out/fotos/
> thumbnails/tn_munich28.jpg


Learn to use the right tool for the job. The command line tool move
is perfect for this.

move /Users/xxx/Documents/webpages/www.myproject.de/pix
/Users/xxx/Documents/webpages/www.myproject.de/pix_out

> Only one folder is changing his name: pix -> pix_out And this with
> all my 168 unused pix in different folders.


I am not sure what you mean by this.


> I was hoping, that the rename function would create the missing
> folders, doing:
>
> rename "/Users/xxx/Documents/webpages/www.myproject.de/pix/fotos/
> thumbnails/tn_munich28.jpg", "/Users/xxx/Documents/webpages/
> www.myproject.de/pix_out/fotos/thumbnails/tn_munich28.jpg";
>
> But there is even no error message, trying to do it like this with
> one test file.


Why would there be an error message if you don't ask for it?

#!/usr/bin/perl

use strict;
use warnings;

my ($old, $new) = ( 'does not exist', 'miracle' );

rename $old => $new
or die "Cannot rename '$old' to '$new': $!";

__END__

C:\Temp> t9
Cannot rename 'does not exist' to 'miracle': No such file or
directory at C:\Temp\t9.pl line 8.

> how to test whether a folder is already created? I tried with:
>
> if (-e ! $dir_pix_out)


WTF?

mkdir $dir unless -d $dir;

or

mkdir $dir unless ! -d $dir;

or

if ( ! -d $dir ) {

}

C:\Temp\t> cat t0.pl
#!/usr/bin/perl

use strict;
use warnings;

mkdir 's' unless -d 's';

mkdir 't' if ! -d 't';

if ( ! -d 'u' ) {
mkdir 'u';
}

unless ( -d 'v' ) {
mkdir 'v';
}

__END__

C:\Temp\t> t0

2008/04/15 10:14 AM <DIR> s
2008/04/15 10:14 AM <DIR> t
2008/04/15 10:14 AM <DIR> u
2008/04/15 10:14 AM <DIR> v


However, testing and creation of the directory is not done in a
single atomic transaction. Therefore, testing the existence of the
directory before attempting to create it is of dubious use. Just do

mkdir $dir;

and make sure to check for errors in subsequent file operations
because a directory that existed a few statements back can disappear
once you reach the file operations.

Sinan

--
A. Sinan Unur <(E-Mail Removed)>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 
Reply With Quote
 
 
 
 
Marek
Guest
Posts: n/a
 
      04-15-2008




Thank you Sinan and Peter for you prompt answers!


Meanwhile I made some progress myself. I am reading in the file name
of the unused pix, and then a grep starting with my "pix" folder, and
then splitting over '/'. The result is three array elements, and the
file name. I am testing, whether the two folders are existing, and if
not, create them. Is this a good approach? (My test script follows on
the bottom).

But I will think over your hints and try to integrate your advices!


marek


#! /usr/local/bin/perl

use strict;
use warnings;
use Data:umper;

my $start_dir = "/Users/xyz/Documents/webpages/www.myproject.de";

my $pix_unused_pix =
"/Users/xyz/Documents/webpages/www.munich-taxis.de/pix/fotos/
thumbnails/tn_munich17.jpg"; # an example of an unused pix

my ($path) = $pix_unused_pix =~ m~/pix/([^"]+?)$~;

my @folders = split("/",$path);


print "The missing folders are:\n\n";
print Dumper(@folders);
print "\n\n";

chdir($start_dir);

if (-e $folders[1]) {
print "\nYes, your folder /$folders[1] exists!\n\n";
}

if (!-e $folders[1]) {
print "\nNO! Your folder /$folders[1] does not exists!\n\n";
mkdir $folders[1];
}

if (-e $folders[1].'/'.$folders[2]) {
print "\nYes, your folder /$folders[1]/$folders[2] exists!\n\n";
}

if (!-e $folders[1].'/'.$folders[2]) {
print "\nNO! Your folder /$folders[1]/$folders[2] does not exists!
\n\n";
mkdir $folders[1] . '/' . $folders[2];
}

 
Reply With Quote
 
nolo contendere
Guest
Posts: n/a
 
      04-15-2008
On Apr 15, 10:18*am, "A. Sinan Unur" <(E-Mail Removed)> wrote:

> mkdir $dir unless -d $dir;
>
> or
>
> mkdir $dir unless ! -d $dir;
>


I think here, you meant:

mkdir $dir if ! -d $dir;

 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      04-15-2008
Marek <(E-Mail Removed)> wrote:
>Meanwhile I made some progress myself. I am reading in the file name
>of the unused pix, and then a grep starting with my "pix" folder, and
>then splitting over '/'. The result is three array elements, and the
>file name.


You may want to look at File::Basename which has functions to split a
file path into its components.

>I am testing, whether the two folders are existing, and if
>not, create them. Is this a good approach? (My test script follows on
>the bottom).


You may want to look at File:ath which provides mkpath() to create
file paths of any depth.

>print Dumper(@folders);


Why not a simple
print @folders;

jue
 
Reply With Quote
 
Marek
Guest
Posts: n/a
 
      04-15-2008


Thank you all! Yes I will look into all your suggestions. Meanwhile I
am progressing on my own way but I will seriously look into all your
suggestions tomorrow.


marek


*******I am here now! I will make a subroutine in my perl script out
of it. If you have any suggestions directly to this script ...

#! /usr/local/bin/perl

use strict;
use warnings;


my $start_dir = "/Users/xyz/Documents/webpages/www.myproject.de";
my $pix_out_folder = $start_dir . "/pix_out";
my $pix_unused_pix =
"/Users/xyz/Documents/webpages/www.myproject.de/pix/fotos/thumbnails/
tn_munich17.jpg";

my ($path) = $pix_unused_pix =~ m~/pix/([^"]+?)$~;

my @folders = split( "/", $path );

if ( !-e $pix_out_folder ) {
mkdir $pix_out_folder
or die "could not create your folder: $pix_out_folder! $!";
}

my $first_sub = shift @folders unless $folders[0] =~ m~\.(jpe?g|gif|
png)$~;
$first_sub = $pix_out_folder . '/' . $first_sub if $first_sub;

if ( $first_sub and !-e $first_sub ) {
print "\nNO! Your folder /$folders[1] does not exists!\n\n";
mkdir $first_sub or die "could not create your folder: \"$first_sub
\"! $!";
}

my $second_sub = shift @folders unless $folders[0] =~ m~\.(jpe?g|gif|
png)$~;
$second_sub = $first_sub . '/' . $second_sub if $second_sub;

if ( $second_sub and !-e $second_sub ) {
print "\nNO! Your folder $second_sub does not exists!\n\n";
mkdir $second_sub
or die "could not create your folder: \"$second_sub\"! $!";
}

my $third_sub = shift @folders unless $folders[0] =~ m~\.(jpe?g|gif|
png)$~;
$third_sub = $second_sub . '/' . $third_sub if $third_sub;

if ( $third_sub and !-e $third_sub ) {
print "\nNO! Your folder $third_sub does not exists!\n\n";
mkdir $third_sub or die "could not create your folder: \"$third_sub
\"! $!";
}


 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      04-15-2008
Peter Ludikovsky wrote:
> Marek wrote:
>>
>> I wrote a perl-script to find all unused pix of a web site. Now I
>> would like to move all those 168! pix from the folder pix to the
>> folder pix_out keeping the same hierarchy as in the original folder.
>> That means:
>>
>> /Users/xxx/Documents/webpages/www.myproject.de/pix/fotos/thumbnails/
>> tn_munich28.jpg
>>
>> should be renamed to:
>>
>> /Users/xxx/Documents/webpages/www.myproject.de/pix_out/fotos/
>> thumbnails/tn_munich28.jpg

>
> BEWARE, UNTESTED PSEUDOCODE
> ---SNIP---
> use strict;
> use warnings;
>
> use File:ath;
> use File::Basename;
> use File::Copy;
>
> my @moved=map{s/pix/pix_old/} @filelist;


Now @moved contains a list of either '1' or '' depending of whether
/pix/ matched or not. And all the elements of @filelist have been
modified so that 'pix' is replaced with 'pix_old'.


> for(my $i=0;$i<$#filelist;$i++){


You have an off-by-one error. You are iterating through the first
element of @filelist through the second-to-last element of @filelist.


> mkpath(dirname($filelist[$i]));
> move($filelist[$i],$moved[$i]);
> }
> ---SNIP---
>
> as said, that code is neither tested nor beautiful, but it should give
> you an idea on how to start...



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      04-15-2008

Quoth Marek <(E-Mail Removed)>:
>
> I wrote a perl-script to find all unused pix of a web site. Now I
> would like to move all those 168! pix from the folder pix to the
> folder pix_out keeping the same hierarchy as in the original folder.


168! is approximately 10^302. I'm impressed you have a filesystem with
that many inodes...

> That means:
>
> /Users/xxx/Documents/webpages/www.myproject.de/pix/fotos/thumbnails/
> tn_munich28.jpg
>
> should be renamed to:
>
> /Users/xxx/Documents/webpages/www.myproject.de/pix_out/fotos/
> thumbnails/tn_munich28.jpg
>
> Only one folder is changing his name: pix -> pix_out And this with all
> my 168 unused pix in different folders.
>
> I was hoping, that the rename function would create the missing
> folders, doing:
>
> rename "/Users/xxx/Documents/webpages/www.myproject.de/pix/fotos/
> thumbnails/tn_munich28.jpg", "/Users/xxx/Documents/webpages/
> www.myproject.de/pix_out/fotos/thumbnails/tn_munich28.jpg";


No, rename doesn't do that.

> But there is even no error message, trying to do it like this with one
> test file.


There is no error message because you didn't ask for one. Always check
the return value of system calls.

my $src = "/Users/...";
(my $dst = $src) =~ s,/pix/,/pix_out/,;

rename $src, $dst or die "can't rename $src -> $dst: $!";

> So probably I have to grep out the folders starting from "pix"-folder
> until the file: this would give in this example the folders fotos ->
> thumbnails which are missing and which I have to create in pix_out -
> folder


You can use File::Basename or File::Spec to find the directory you are
trying to rename into, and File:ath to create the whole directory tree
required in one step.

> how to test whether a folder is already created? I tried with:
>
> if (-e ! $dir_pix_out) #wrong!! { mkdir $dir_pix_out; }


Huh? What made you think that would work? Assuming $dir_pix_out contains
a filename, it is a true value. This means ! $die_pix_out is undef, and
you are trying to test for the existance of a file with an empty name.
You will also get a warning: do you have warnings switched on?

The test for a directory is -d, so you want

if (! -d $dir_pix_out) {

or

unless (-d $dor_pix_out) {

Note that mkdir doesn't create multiple levels of directory, either;
again, you need to use File:ath.

Ben

 
Reply With Quote
 
Ben Bullock
Guest
Posts: n/a
 
      04-15-2008
On Tue, 15 Apr 2008 14:18:38 +0000, A. Sinan Unur wrote:

>> I wrote a perl-script to find all unused pix of a web site. Now I would
>> like to move all those 168! pix from the folder pix to the folder
>> pix_out keeping the same hierarchy as in the original folder. That
>> means:
>>
>> /Users/xxx/Documents/webpages/www.myproject.de/pix/fotos/
>> thumbnails/tn_munich28.jpg
>>
>> should be renamed to:
>>
>> /Users/xxx/Documents/webpages/www.myproject.de/pix_out/fotos/
>> thumbnails/tn_munich28.jpg

>
> Learn to use the right tool for the job. The command line tool move is
> perfect for this.
>
> move /Users/xxx/Documents/webpages/www.myproject.de/pix
> /Users/xxx/Documents/webpages/www.myproject.de/pix_out


As far as I can understand the original poster has a list of pictures he
wants to move to directories with the name changed in the middle of the
list. What you're suggesting is going to move everything in the
directories, not just the pictures he wants to move.

>> Only one folder is changing his name: pix -> pix_out And this with all
>> my 168 unused pix in different folders.

>
> I am not sure what you mean by this.


Well, why post a reply to a question, if you don't understand it?

>
>> I was hoping, that the rename function would create the missing
>> folders, doing:
>>
>> rename "/Users/xxx/Documents/webpages/www.myproject.de/pix/fotos/
>> thumbnails/tn_munich28.jpg", "/Users/xxx/Documents/webpages/
>> www.myproject.de/pix_out/fotos/thumbnails/tn_munich28.jpg";
>>
>> But there is even no error message, trying to do it like this with one
>> test file.

>
> Why would there be an error message if you don't ask for it?


Whether there's an error message depends on the detailed behaviour of the
function in question.

>> how to test whether a folder is already created? I tried with:
>>
>> if (-e ! $dir_pix_out)


Here you need to say

if (!-d $dir_pix_out)

> However, testing and creation of the directory is not done in a single
> atomic transaction. Therefore, testing the existence of the directory
> before attempting to create it is of dubious use.


Really?

> Just do
>
> mkdir $dir;
>
> and make sure to check for errors in subsequent file operations because
> a directory that existed a few statements back can disappear
> once you reach the file operations.


Really? I've never experienced that.
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      04-16-2008
Ben Bullock <(E-Mail Removed)> wrote in
news:fu3akp$pl5$(E-Mail Removed):

> On Tue, 15 Apr 2008 14:18:38 +0000, A. Sinan Unur wrote:
>
>>> I wrote a perl-script to find all unused pix of a web site. Now
>>> I would like to move all those 168! pix from the folder pix to
>>> the folder pix_out keeping the same hierarchy as in the original
>>> folder. That means:
>>>
>>> /Users/xxx/Documents/webpages/www.myproject.de/pix/fotos/
>>> thumbnails/tn_munich28.jpg
>>>
>>> should be renamed to:
>>>
>>> /Users/xxx/Documents/webpages/www.myproject.de/pix_out/fotos/
>>> thumbnails/tn_munich28.jpg

>>
>> Learn to use the right tool for the job. The command line tool
>> move is perfect for this.
>>
>> move /Users/xxx/Documents/webpages/www.myproject.de/pix
>> /Users/xxx/Documents/webpages/www.myproject.de/pix_out

>
> As far as I can understand the original poster has a list of
> pictures he wants to move to directories with the name changed in
> the middle of the list. What you're suggesting is going to move
> everything in the directories, not just the pictures he wants to
> move.


Well, I understood differently but your explanation makes more
sense.

>>> Only one folder is changing his name: pix -> pix_out And this
>>> with all my 168 unused pix in different folders.

>>
>> I am not sure what you mean by this.

>
> Well, why post a reply to a question, if you don't understand it?


So that the OP can clarify it if he feels like it.

>>> I was hoping, that the rename function would create the missing
>>> folders, doing:
>>>
>>> rename
>>> "/Users/xxx/Documents/webpages/www.myproject.de/pix/fotos/
>>> thumbnails/tn_munich28.jpg", "/Users/xxx/Documents/webpages/
>>> www.myproject.de/pix_out/fotos/thumbnails/tn_munich28.jpg";
>>>
>>> But there is even no error message, trying to do it like this
>>> with one test file.

>>
>> Why would there be an error message if you don't ask for it?

>
> Whether there's an error message depends on the detailed behaviour
> of the function in question.


The caller should check if the function returned an error and
display the error message if necessary. I also showed how to do
that.

>> However, testing and creation of the directory is not done in a
>> single atomic transaction. Therefore, testing the existence of
>> the directory before attempting to create it is of dubious use.

>
> Really?


IMHO, yes. If the directory already exists, nothing will happen. If
it does not it will created (assuming no permissions issues).

>> Just do
>>
>> mkdir $dir;
>>
>> and make sure to check for errors in subsequent file operations


This is the important part. Especially in a scenario which I have
seen occasionally in others' code where the existence of the target
directory is checked before a lengthy loop of file copy operations.

>> because a directory that existed a few statements back can
>> disappear once you reach the file operations.

>
> Really? I've never experienced that.


Ahem, I hate to admit it, but I am not sure exactly what I was
thinking of. I seem to remember such a caution but I am not sure
now.

Sinan
--
A. Sinan Unur <(E-Mail Removed)>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Moving Guide Moving Companies Movers Storage linkswanted ASP .Net 0 01-23-2008 01:08 AM
Free Moving Estimate, Local Movers, Long Distance Moving, PackingSupplies, Storage Rental, Home Moving, Apartment Moving, Office Moving,Commercial Moving linkswanted ASP .Net 0 01-06-2008 04:45 AM
Moving to California Moving to L.A linkswanted Digital Photography 3 01-05-2008 04:22 PM
movers los angeles moving storage services movers in L.A companymoving companies moving in calfornia los angeles linkswanted HTML 0 12-21-2007 10:55 PM



Advertisments