sud
asked on
PERL - Find newest folder
Dear Experts,
I am trying to copy files from A:\temp\ to c:\xyz\temp
I need to do the following.
1. Look for latest folder like "20160630_Weekly_Data"
2. Compare text files in the source folder with the files in destination folder for timestamp, if new then do next step else not.
I am doing the file
opendir(DIR,"$srcfil") or die "Cannot open $srcfil\n";
my @files = readdir(DIR);
closedir(DIR);
foreach my $file (@files) {
#next if ($file !~ /\.txt$/i);
copy ("$srcfil/$file", $destfil) or warn "could not copy $file: $!\n";
}
Please help
I am trying to copy files from A:\temp\ to c:\xyz\temp
I need to do the following.
1. Look for latest folder like "20160630_Weekly_Data"
2. Compare text files in the source folder with the files in destination folder for timestamp, if new then do next step else not.
I am doing the file
opendir(DIR,"$srcfil") or die "Cannot open $srcfil\n";
my @files = readdir(DIR);
closedir(DIR);
foreach my $file (@files) {
#next if ($file !~ /\.txt$/i);
copy ("$srcfil/$file", $destfil) or warn "could not copy $file: $!\n";
}
Please help
ASKER
Thank you wilcoxon.
In the main directory there are several sub directories. The directory are created weekly and I want to find the newest directory like "20160630_Weekly_Data". How do I do that ?
After that I have I copy the files from the newest sub directory for which your code will be helpful.
In the main directory there are several sub directories. The directory are created weekly and I want to find the newest directory like "20160630_Weekly_Data". How do I do that ?
After that I have I copy the files from the newest sub directory for which your code will be helpful.
Is the "newest" directory guaranteed to have the latest modification date (eg most recent file written)? If so, this should work:
opendir DIR, $srcfil or die "Cannot open $srcfil: $!\n";
my $ndir = (sort { -M "$srcfil/$a" <=> -M "$srcfil/$b" } grep { -d $_ } readdir DIR)[0];
closedir DIR;
opendir DIR, "$srcfil/$ndir" or die "Cannot open $srcfil/$ndir: $!\n";
# remove the grep if you want
my @files = grep { -f "$srcfil/$ndir/$_" and /\.txt$/i } readdir(DIR);
closedir DIR;
foreach my $file (@files) {
next unless (-M "$srcfil/$ndir/$file" < -M "$destfil/$file");
copy "$srcfil/$ndir/$file", $destfil or warn "could not copy $file: $!\n"
}
ASKER
Thank you,
I am getting error message for the following line. It says use of uninitialized values in numeric lt < < >
next unless (-M "$srcfil/$ndir/$file" < -M "$destfil/$file");
I am getting error message for the following line. It says use of uninitialized values in numeric lt < < >
next unless (-M "$srcfil/$ndir/$file" < -M "$destfil/$file");
Ah. The destination file probably doesn't exist (so the second -M comes up undef). Change that line to:
next unless (!-f "$destfil/$fil" or -M "$srcfil/$ndir/$file" < -M "$destfil/$file");
ASKER
Great Thank you this helps.
Thanks,
Thanks,
ASKER
Another quick thing is I am interested only in subdirectory like "20160630_Weekly_Data". There are other subdirectory "20160630_weekly_meeting" and others So every week we get files in new subdirectory with the name like "2016xxxx_weekly_Data" subdirectory and using perl I want to find the latest ones.
How do I modify your following code ? Any thoughts ?
opendir DIR, $srcfil or die "Cannot open $srcfil: $!\n";
my $ndir = (sort { -M "$srcfil/$a" <=> -M "$srcfil/$b" } grep { -d $_ } readdir DIR)[0];
closedir DIR;
How do I modify your following code ? Any thoughts ?
opendir DIR, $srcfil or die "Cannot open $srcfil: $!\n";
my $ndir = (sort { -M "$srcfil/$a" <=> -M "$srcfil/$b" } grep { -d $_ } readdir DIR)[0];
closedir DIR;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If you need to traverse a directory tree, then instead of reinventing the wheel like arnold suggests, IMO it would be preferable to use the File::Find module to handle the recursion.
IMO, it would also be preferable to use a lexical var for the directory handle instead of the bareword.
IMO, it would also be preferable to use a lexical var for the directory handle instead of the bareword.
ASKER
Thank you expert for your help.
ASKER
Thank you Wilcoxon and arnold.
Open in new window