|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
My, Oh My
For starters, move the "my" declaration of $ftype out of the for loop.
Code:
my $ftype = '';
....
for $dir(@dir) {
$ftype = "";
....
|
|
#3
|
|||
|
|||
|
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 |
|
#4
|
|||
|
|||
|
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 |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Perl Programming > Walking through directories |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|