Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl Script

Status
Not open for further replies.

shezam

Programmer
Jan 30, 2009
11
IE
Im writing a perl script to delete data based on wether I find certain data. Let me explain. Inside the following dir "/spare/data/sample/" there are 5 levels of 6 directories from A-F like so

so inside /spare/data/sample is
A B C D E F and inside that is the same and so on.

At the very bottomest level will be some files. So they could be at

/spare/data/sample/A/E/E/F/123ghkty6ssdoklh/
sample1
sample2
sample3

The "123ghkty6ssdoklh" is randomized so its never fixed and there could be any number of these directories. There may be a zip file of the above files sample1 etc inside "/spare/data/sample/A/E/E/F/123ghkty6ssdoklh.zip". So what i want to do is check for the above files and if they exists go back one directory and check for the zip file and if it exists delete the directory "123ghkty6ssdoklh" and its contents.

So i have used finddepth to do this like so;

$dir = /spare/data/sample
finddepth(\&removeData, $dir);

sub removeData {
if( $_ eq "sample1" )
{
my $now = cwd;
my $file_dir = $File::Find::dir;
print "Found a sample1 at [$file_dir]\n";
chdir $file_dir;
$now = cwd;
print "Current directory $now\n";
my $zip = `find ../ -name *.zip`;
if ($zip)
{
print "Found a zipped data at $zip\n";
chdir ("..");
$now = cwd;
print "Removing old data at $file_dir\n";
`rm -rf $file_dir`;

}
else
{

print "We didn't find zipped data\n";

}
}
}

So this removes the directory ok but it seems to get lost as a result of deleting the directory;

Can't cd to (/spare/data/sample/) A: No such file or directory
Can't cd to (/spare/data/sample/) C: No such file or directory
Can't cd to (/spare/data/sample/) C: No such file or directory
Can't cd to /spare/data/sample/A/E/E/F/123ghkty6ssdoklh: No such file or directory

Can anyone spot anything wrong, well thats assuming u have made sense of all of above :)
 
Try perhaps removing the chdir() calls and use full paths instead.

Incidentally, do you use a "cwd" function from some module? It doesn't appear to work for me, just treated as an unquoted string.

Annihilannic.
 
Running shell commands and also using chdir() yourself while File::Find is also doing that on its own. Sounds like a recipe for disaster. I wouldn't try and do it all in one pass. Reads the directories, store them in a hash and then use some other logic to see if the files are in the directory below the one with the zip file and then delete the files/folders you want to delete using File::path.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
With finddepth the syntax says;

use File::Find;
finddepth(\&wanted, @directories_to_search);
sub wanted { ... }

%options

The first argument to "find()" is either a code reference to your
&wanted function, or a hash reference describing the operations to be
performed for each file. The code reference is described in "The
wanted function" below.


What if I want to use a code reference &wanted function and a hash reference like follow_skip==2. Can this be done?
 
Your answer is on the perldoc page 2 lines below the part you quoted. :)

You can reference the &wanted function in the the %options hash, e.g.

Code:
my %findoptions = (
    wanted => \&wanted,
    follow_skip => 2,
};

...or something like that (untested).

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top