Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 24th, 2001, 09:43 AM
petey petey is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2001
Posts: 2 petey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question Walking through directories

I have a member picture page that has multiple directories, in fact one for each member. I have a script that needs to modify the image files within each directory so I need to start at the parent members directory and walk through each members directory and modify each image file.

I have the part of modifying every file in a directory working correctly but I must specify the directory I'm working in. b/c I have 3000+ member directories, I just want to automate this, but what I have now isn't working

The code is below :
what I have in italics is what works when it's in it's own package and a single directory is specified.

#!/usr/bin/perl -w
use Image::Magick;


# open all the files/directories in the parent directory
opendir (DIR, "/usr2/mywebsite.com/members") or die "Sorry cannot open";
@dir = readdir(DIR);
closedir(DIR);
for $dir(@dir){

my $ftype = "";

# get the information of the file/directory
($device,$inode,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
$blksize,$blocks) = stat($dir);


# Make sure it's a directory
$ftype = ($mode & 0170000)>>12;
if ($ftype !=8) {

# open the files in the current directory
#NOTE.. from here down works when $dir is specified
opendir(FILES,$dir) or die "Sorry cannot open $dir";
my @allfiles = grep(!/^\.\.?$/,readdir(FILES));
closedir(FILES);
@allfiles = sort(@allfiles);
my $count = "1";
for $file(@allfiles){
$new_file = "$file";

# set the permission on the image
chmod(0777,$new_file);
my($image, $x);
$image = Image::Magick->new;

#read the image
$x = $image->Read($new_file);
warn "$x" if "$x";

# crunch the image
$x = $image->Set(quality=>50);

#write the image
$x = $image->Write($new_file);
warn "$x" if "$x";
}
}

}


I was originally thinking about reading all of the directory names into an array then for each directory do the stuff in italics, but went this route. Anyone have a solution? Thanks a bunch.

Petey

Reply With Quote
  #2  
Old May 24th, 2001, 12:58 PM
Pressly Pressly is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2001
Posts: 48 Pressly User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
My, Oh My

For starters, move the "my" declaration of $ftype out of the for loop.
Code:
my $ftype = '';
....
for $dir(@dir) {
    $ftype = "";
....

Reply With Quote
  #3  
Old May 27th, 2001, 07:44 PM
dkode dkode is offline
PHP/PERL/.NET Coder
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2001
Location: Daytona Beach, Florida
Posts: 36 dkode User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 41 sec
Reputation Power: 8
Send a message via AIM to dkode
i've had to do similar things to multiple directories at once and found the easiest way to do what your're trying is to use glob();

go to www.perldoc.com and search for glob

it grabs all the directory names in the cwd and dumps them into an array, then simple use a foreach statement.

Another way to do it would be to use the Cwd.pm as well.

Hope that was of help

DKode

Reply With Quote
  #4  
Old May 30th, 2001, 07:22 AM
petey petey is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2001
Posts: 2 petey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Ok,

So I've written a small glob program:

@dir = glob "/usr/mywebpage.com/members/*";
print </usr/mywebpage.com/members/*>;



#at the command line I type this
> perl -MFile::DosGlob=glob -e "print </usr/mywebpage.com/members/*>"

and I get a list of all the member directories that I want!

No how do I capture this in a loop instead of going to the command line.

for instance my outer loop looks something like this, (but of course isn't working)

for $dir(@dir){

#rest of the body
#check that is a direcotry
if {
# open the files in the current directory
opendir(FILES,$dir) or die "Sorry cannot open $dir";
my @allfiles = grep(!/^\.\.?$/,readdir(FILES));
closedir(FILES);
@allfiles = sort(@allfiles);
my $count = "1";
#inner loop to do the stuff!
for $file(@allfiles){
# do all the stuff to the files
}}}


for each directory in the glob I need to open and manipulate the files. How do I set up the outer loop to capture this?

Thanks

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Walking through directories


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway