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!

Check folder and files modified in last 5 days

Status
Not open for further replies.

sheed

Programmer
Jun 14, 2005
38
US
Hi,

Need some help in figuring out how to find out if the folder is modified within last 11 days and then going through the folder content and checking if any of the files are modified within last 5 days. Here is the code I am trying but some how it doesn't work:

Code:
#!/usr/bin/perl

# Check if the directory is modified within last 11 days
# Check if the files of the directory are modified in last 5 days

opendir(DIR,"C:\\test\\data");
my @dir=grep { !/^\.+$/ } readdir(DIR);

if (-M $dir < 11)
{
	print "Last Directory change:\t" . scalar localtime($dir) . "\n"; 
}

closedir(DIR);

foreach $file (@dir)
{
	if (-M $file < 5)
	{
		print "************************Last change:\t" . scalar localtime($file) . "\n"; 
	}
}

running the above script gives the following output and it doesn't seem to be right as the folder is modified in last 11 days and does contain some files modified within last 5 days.

Code:
Last Directory change:  Wed Dec 31 18:00:00 1969

Any help is really appreciated. Thanks
 
How about something like this?
Code:
my $dir = 'c:/test';
if (-M $dir >= 11) {
    print "Directory $dir/ last modified ", scalar localtime((stat $dir)[9]), "\n";
    chdir $dir;
    opendir DIR, '.';
    foreach (readdir DIR) {
        next if $_ eq "." || $_ eq "..";
        if (-M $_ >= 5) {
            print "File $dir/$_ last modified ", scalar localtime((stat)[9]), "\n";
        }
    }
    closedir DIR;
}
Oh, and you don't need to use the double-backslashes in windows, you can use forwardslashes and they'll work just fine (for example, 'C:/test/data'.)
 
Thanks it worked. I am also trying to move the files from one directory to another also I only want to move a file not a directory here is what I am doing:

Code:
#!/usr/bin/perl

my $dir = 'C:/test/data';
if (-M $dir >= 11) 
{
    print "Directory $dir/ last modified ", scalar localtime((stat $dir)[9]), "\n";
    chdir $dir;
    opendir DIR, '.';
    foreach (readdir DIR) 
	{
			next if $_ eq "." || $_ eq "..";
			if (-M $_ >= 5) 
			{
				print "File $dir/$_ last modified ", scalar localtime((stat)[9]), "\n";
				#check if a file then move if directory then skip
				if(-e "$_")
				{
					rename ("C:/test/data/$_", "C:/test/data/moved");
				}
            }
    }
    closedir DIR;
}

rename is not working as also -e "$_" to check if a file or directory. Any help is really appreciated. Thanks
 
I misread your post, you want to move the files if they have been modified five days ago or less, right? You'll want to use <= rather than >= - here's some code that might help with the file moving.
Code:
use File::Copy qw/move/;
my $dir = 'c:/test/data';
if (-M $dir <= 11) {
    print "Directory $dir/ last modified ", scalar localtime((stat $dir)[9]), "\n";
    chdir $dir;
    opendir DIR, '.';
    foreach (readdir DIR) {
        next if $_ eq "." || $_ eq "..";
        if (-M $_ <= 5) {
            if (-f $_) {
                if (move $_, "./moved/$_") {
                    print "$_ - successfully moved\n";
                } else {
                    warn "$_ - move failed.\n";
                }
            }
        }
    }
    closedir DIR;
}
The -f file test operator returns true if the file is a plain file. If you want to look up the rest of the file test operators, take a look at perldoc -f -X.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top