LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-24-2011, 01:17 PM   #1
ktyson
LQ Newbie
 
Registered: Jan 2011
Posts: 2

Rep: Reputation: 0
Perl File::Find not taking in correct directory parameter


I wrote a perl script that mounts and unmounts various computers on my network and deletes files in a specific folder if they are older than 60 days. Each computer is mounted in the same folder and I am using Perl module File::Find to traverse the directory and remove the old files. When the different computers have identical directory paths find() works perfect but when the path's are slightly different find() is not using the new directory path. What is going on?

Here is my code

for my $target (keys(%computer))
{
print "$target\n";
.....mount the computer at /home/mnt/testTool...
......

#DIFFERENT PATHS
if ($target=~/^5.2.2/) {$dir = "/home/mnt/testTool/project"; }
else { $dir = "/home/mnt/testTool/projects"; }
print $dir."\n";

my $reel = "";
my $mtime;
my $age;
my $next = 1;
$reel = $dir. "/Reel";

print $reel."\n";

find(\&processR, $reel);
sub processR {
$mtime = stat($_)->mtime; #modification time
$age = (time-$mtime)/86400; #how many days old the file/folder is
if ($_ =~ /^\d\d\d/ && $age > $days_back)
{
print $reel."\n";
my $file = $reel . '/' . $_;
$cmd =`rm "$file"`;
}

}



Here is the output and clearly the proof of error

5.2.2
/home/mnt/testTool/project
/home/mnt/testTool/project/Reel
/home/mnt/testTool/project/Reel
5.3.2
/home/mnt/testTool/projects
/home/mnt/testTool/projects/Reel
/home/mnt/testTool/project/Reel

rm: cannot remove `/home/mnt/testTool/project/Reel/495178.txt': No such file or directory
rm: cannot remove `/home/mnt/testTool/project/Reel/498816.txt': No such file or directory

Last edited by ktyson; 01-24-2011 at 01:18 PM.
 
Old 01-24-2011, 03:07 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by ktyson View Post
I wrote a perl script that mounts and unmounts various computers on my network and deletes files in a specific folder if they are older than 60 days. Each computer is mounted in the same folder and I am using Perl module File::Find to traverse the directory and remove the old files. When the different computers have identical directory paths find() works perfect but when the path's are slightly different find() is not using the new directory path. What is going on?

Here is my code

Code:
for my $target (keys(%computer))
{
        print "$target\n";
        .....mount the computer at /home/mnt/testTool...
        ......

         #DIFFERENT PATHS
        if ($target=~/^5.2.2/) {$dir = "/home/mnt/testTool/project"; }
        else { $dir = "/home/mnt/testTool/projects"; }
        print $dir."\n";
       
        my $reel = "";
        my $mtime;
        my $age;
        my $next = 1;
       $reel = $dir. "/Reel";

        print $reel."\n";
        
        find(\&processR, $reel);
        sub processR {
             $mtime = stat($_)->mtime;  #modification time
             $age = (time-$mtime)/86400;  #how many days old the file/folder is
              if ($_ =~ /^\d\d\d/ && $age > $days_back)
                        {
                         print $reel."\n";
                         my $file = $reel . '/' . $_;
                         $cmd =`rm "$file"`;
                        }

                }

Here is the output and clearly the proof of error

5.2.2
/home/mnt/testTool/project
/home/mnt/testTool/project/Reel
/home/mnt/testTool/project/Reel
5.3.2
/home/mnt/testTool/projects
/home/mnt/testTool/projects/Reel
/home/mnt/testTool/project/Reel

rm: cannot remove `/home/mnt/testTool/project/Reel/495178.txt': No such file or directory
rm: cannot remove `/home/mnt/testTool/project/Reel/498816.txt': No such file or directory
Use 'code' tags - see '#' button in "go advanced".

Show the whole script. If the script doesn't have

Code:
use strict;
use warning;
in the very beginning, first put them and make sure there are no compilation and runtime warnings/errors.
...
It look like you don't have

Code:
use strict;
use warning;
.
 
Old 02-01-2011, 08:05 AM   #3
ktyson
LQ Newbie
 
Registered: Jan 2011
Posts: 2

Original Poster
Rep: Reputation: 0
So I solved my issue. I was trying to reference the global variable $reel inside of the File::Find method. I was trying to get the name of the directory that I inputed into the function as $reel, however, the File::Find module stores the directory name in $File::Find::dir. I have since replaced $reel with $File::Find::dir and the script is working fine
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell getopts: opt w/ optional parameter is taking next opt as its parameter! GrapefruiTgirl Programming 22 10-27-2010 06:00 AM
How dose Linux find the correct file? DavidFracs Linux - Kernel 1 08-31-2009 12:54 PM
Perl find file and then replace string in file moos3 Programming 5 07-29-2009 07:10 AM
GTK+ compiling - libtool cant find correct .la file Wafeliron Linux - General 2 07-20-2009 12:37 PM
How to set correct file/directory permissions pat.delaney Linux - Networking 5 12-02-2003 09:39 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration