Solved

Finding modified date of the file and moving file to folder in perl

Posted on 2010-11-11
13
525 Views
Last Modified: 2012-06-22
Hi,

In perl how do I take the file modified date and create folder with name

archiveold/yyyy/MM/dd

suppose file modified date is 25-Oct-2010

If not exists then folders created should be

archiveold/2010/Oct/25

then I wanted to move the file to its relevant date folder

while (*.xml) {
  chomp;
  $file=$_ ;

  # suppose file modified date is 25-Oct-2010
  # create folders at path archiveold/2010/Oct/25 if dos not exists
  # move file in folder 'archiveold/2010/Oct/25'

}

Please guide
0
Comment
Question by:tia_kamakshi
  • 8
  • 5
13 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 34118203
use POSIX;
use File::Path qw(make_path);
while( <*.xml> ){
   my $path=strftime"archiveold/%Y/%m/%d",localtime((stat)[9]);
   make_path $path;
   rename $_,"$path/$_" or warn "$path/$_ $!";
}
0
 

Author Comment

by:tia_kamakshi
ID: 34119233
Hi,

Thanks for your code

When I am running my code below, then I am getting the error saying

"make_path" is not exported by the File::Path module
Can't continue after import errors at ./archiveFiles.pl line 4
BEGIN failed--compilation aborted at ./archiveFiles.pl line 4.


Please guide

Many Thanks
#!/usr/bin/perl

use POSIX;
use File::Path qw(make_path);


my $srcdir = "/home/dbali/config/";

opendir(DIR, $srcdir) or die "Can't open $srcdir: $!";
my @files = grep { /\.*$/i } readdir(DIR);

foreach my $file (@files) {
   my $path=strftime"archiveold/%Y/%m/%d",localtime((stat)[9]);
   make_path $path;
   rename $_,"$path/$_" or warn "$path/$_ $!";
}

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 34119336
What version of File::Path do you have?
does
use File::Path;
mkpath $path;
work?

Also, if you are using $file as your loop variable, then
localtime((stat)[9])
should be
localtime((stat "$srcdir/$file")[9]);
and
rename $_,"$path/$_"
should be
rename "$srcdir/$file","$path/$file";

also, /\.*$/i always matches, since all strings have zero or more "." before the end
so the grep is a noop

0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:tia_kamakshi
ID: 34120243
Many Thanks for your reply

I need all types of files

what should I write instead of /\.*$/i  in my below code to list all files regardless of any extension.

But list should not folders and files in subdirectories

my @files = grep { /\.*$/i } readdir(DIR);


Many Thanks for your help
0
 

Author Comment

by:tia_kamakshi
ID: 34121610
Hi,

I have modified my code to below. It is giving me error saying

Use of uninitialized value in -d at /usr/share/perl/5.8/File/Path.pm line 143.
fileparse(): need a valid pathname at /usr/share/perl/5.8/File/Path.pm line 144


Also, please help me in fixing

I need all types of files

what should I write instead of /\.*$/i  in my below code to list all files regardless of any extension.

But list should not folders and files in subdirectories

my @files = grep { /\.*$/i } readdir(DIR);


Please guide

Thanks,

#!/usr/bin/perl

use POSIX;
use File::Path;
mkpath $path;

my $srcdir = "/home/dbali/config/";

opendir(DIR, $srcdir) or die "Can't open $srcdir: $!";
my @files = grep { /\.*$/i } readdir(DIR);

foreach my $file (@files) {
   my $path=strftime"archiveold/%Y/%m/%d",localtime((stat "$srcdir/$file")[9]);
   make_path $path;
   rename "$srcdir/$file","$path/$file";
}

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 34122157
to get all files, just use
my @files =  readdir(DIR);
to omit directories, use
grep{ !-d "$srcdir/$_"}  readdir(DIR);
to list only plain files, use
grep{ -f "$srcdir/$_"}  readdir(DIR);

mkpath $path; should be inside the loop after $path is defined, where you now have make_path $path;
since you told me that your version of File::Path does not export that name
0
 

Author Comment

by:tia_kamakshi
ID: 34193682
Many Thanks for your replies.

I am sorry for late response. I was out of station last week.

I will get back to you today on this. Once again appologise for late response
0
 

Author Comment

by:tia_kamakshi
ID: 34194157
Hi,

I have modified the code and it is giving me below error saying

dbali@pbms-tstva2:~$ ./archiveFiles.pl
Can't locate object method "make_path" via package "archiveold/2010/02/04" (perhaps you forgot to load "archiveold/2010/02/04"?) at ./archiveFiles.pl line 14.
dbali@pbms-tstva2:~$


Please find my updated code

Please guide

Many Thanks

#!/usr/bin/perl

use POSIX;
use File::Path;

my $srcdir = "/home/dbali/config/";

opendir(DIR, $srcdir) or die "Can't open $srcdir: $!";
my @files = grep{ !-d "$srcdir/$_"}  readdir(DIR);

foreach my $file (@files) {
   my $path=strftime"archiveold/%Y/%m/%d",localtime((stat "$srcdir/$file")[9]);
   mkpath $path;
   make_path $path;
   rename "$srcdir/$file","$path/$file";
}

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 34194199
Apparently, your version of File::Path has only mkpath not make_path.
So use mkpath not make_path
0
 

Author Comment

by:tia_kamakshi
ID: 34196702
Many Thanks.

Now my script is working good.

But my files are not moving to destination directory

Please guide
#!/usr/bin/perl

use POSIX;
use File::Path;

my $srcdir = "/home/dbali/config/";

opendir(DIR, $srcdir) or die "Can't open $srcdir: $!";
my @files = grep{ !-d "$srcdir/$_"}  readdir(DIR);

foreach my $file (@files) {
   my $path=strftime"archiveold/%Y/%b/%d",localtime((stat "$srcdir/$file")[9]);
   mkpath $srcdir.$path;
   rename "$srcdir/$file","$srcdir.$path/$file";
}

Open in new window

0
 

Author Comment

by:tia_kamakshi
ID: 34212692
I wanted to give a 100% points to ozo solution. By mistake request is gone for closure. Please allocate all 500 points to ozo.

Many Thanks
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 34213949
 mkpath $srcdir.$path;
rename "$srcdir/$file",$srcdir.$path."/$file";
or
rename "$srcdir/$file","$srcdir$path/$file";




0
 

Author Comment

by:tia_kamakshi
ID: 34217490
Please allocate all 500 points to Ozo or please let me know how do I do it????
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
remove duplicates from the csv file 13 108
crawling gofundme 4 126
Perl string filter 5 78
Get a Perl script to return content from a module 7 79
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now