mikevanhoff has asked for the wisdom of the Perl Monks concerning the following question:

First - thanks for the help and comments. Second - after reviewing the responses, suggestions, and doing some more studying, I think I am on the right track with my array. However, I am receiving "or die" message as my script executes. I have included a cleaned up version below for your information. It appears to be correct, and ran before I added the array. The error message states the correct array element so, I know that part is working. The directories do exist and contain other files and directories. Please take a look and point me in the right direction. Thanks in advance.
#!/usr/local/bin/perl open (FILE, "/Scripts/rotlog"); my @lines = <FILE>; close FILE; foreach $element (@lines) { # open the directory for reading opendir(REP, "$element") || die "Cannot open the directory $element $ +! "; open(REMLOG,">/Scripts/dir-removed.log"); # open a file to keep as a +log of directories removed. print REMLOG "Directories removed this date : ",`date`, "\n"; # Place +a heading in the file. # list the contents of the directory. These should be directories. while ($name = readdir(REP)) { if (-M $name >= 10) { print "$name\n" unless($name eq "images"); # print REMLOG "$name\n" unless($name eq "images"); #do not remove t +he images directory `rm -r $name` unless($name eq "images"); # THIS WILL REMOVE THE D +IRECTORY AND ALL OF ITS subdirectories and Files. } } close remlog; closedir(REP) }

Replies are listed 'Best First'.
Re: Execute error on opendir
by no_slogan (Deacon) on Aug 14, 2002 at 14:45 UTC
    Your filenames have end-of-lines in them.
    chomp @lines;
      no_slogan - You were right on!! I missed that in testing to see if the array was populating correctly. THANKS.
Re: Execute error on opendir
by stajich (Chaplain) on Aug 14, 2002 at 13:59 UTC
    Are you sure the $element is the full path to the directory you want to open - or else you are executing this script from the correct place s.t. $element is a relative directory from your cwd? You can confirm this with: die "no dir named $element\n" unless ( -d $element); just before your opendir stmt.
      stajich I added the line of code you provided, and it appears that $element is trying to use a relative path. Any suggestions on how to correct this?
        It's always safest(not sure if it's required) to give opendir an absolute directory name - that is, a directory name with a fully qualified path. Do the "element"s in /Scripts/rotlog have absolute directory names in them?

        And, the $name that readdir returns is *NOT* an absolute filename. Therefore, before you try to test $name with a -M filetest operator, you must provide the absolute path to $name - something like this:
        my $current_abs_dir = "/path/to/opendir/directory"; while ($name = readdir(REP)) { $abs_name = "$current_abs_dir/$name"; if (-M $abs_name >= 10) {
        If the elements in /Scripts/rotlog do not include the absolute path to the directory, you'll need to get the absolute path to the directory from somewhere.

        Read the perldocs for opendir and readdir by doing
        perldoc -f opendir perldoc -f readdir
        HTH.
        Add a $prepath = '/some/path/to/that/makes/elementfullyqual' right below the shabang, then rewrite the open and -d test with "$prepath$element"...

        -Waswas