469,352 Members | 2,150 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

Copying files from a dir that were created in a specific date range

Hi,

How can i make a script to copy a bunch of files in a specific directory, that were created in a specifc date range?

Thanks in advance,
Teixeira
Aug 18 '07 #1
9 3246
numberwhun
3,503 Expert Mod 2GB
First, we are definitely going to want to see your code that you have been working with so we can help you troubleshoot it. (don't forget the code tags around it).

When copying a directory, I would start by reading File::Copy::Recursive. As for the date of the file, there are a number of ways to do this, but we will want to know more first. Like, are you doing the copy based on a specific (fixed) date range or a date range that is a specific age (ie: 5 days back after 30 days have passed).

Also, what operating system are you working on?

Regards,

Jeff
Aug 18 '07 #2
Hi,

i'm working in FreeBSD.
I wanna copy the files in a directory "/usr/local/var/qmailscan/quarantine" from 15 July 2007 until 18 July 2007.

I actually didn't start almost anyhing, i was just making some experiences with File::Copy() which was the function i googled and supposlly would help-me.

Thanks,
Teixeira
Aug 18 '07 #3
numberwhun
3,503 Expert Mod 2GB
Yes, File::Copy will help you with copying the files as you need, but since you are looking for specific dates, then I would suggest a Regular Expression to pull out the date field(s) and do a comparison to see if they match what you need.

Regards,

Jeff
Aug 18 '07 #4
Thanks,

i'll learn more about regular expressions because i'm not inside regular expressions world yet.
If you could make a simple code sample for me to have as a guideline i'd appreciate very much.

Thanks and have a nice weekend.
Teixeira
Aug 18 '07 #5
KevinADC
4,059 Expert 2GB
I would consider using the grep() function and stat() to first collect a list of the files you are interested in copying. Then use File::Copy to copy them.

Expand|Select|Wrap|Line Numbers
  1. use File::Copy;
  2. chdir('path/to/folder') or die "$!";
  3. opendir(DIR,'.') or die "$!";
  4. my @list = grep {here you calculate the date range using stat()}  readdir DIR;
  5. close DIR;
  6. foreach my $file (@list) {
  7.    copy file stuff here
  8. }   
You could do it in one loop through the directory but I find it's often beneificial to make a list/array first when doing work like this.

perldoc: stat function

You can look up the other functions on the same website I linked you to.
Aug 18 '07 #6
thanks for your answer.

i searched a little more and i found an easier way to list files, attendding to my reduced experience in perl.
Here's the script

Expand|Select|Wrap|Line Numbers
  1. chdir("/home/myname/Desktop"); #changes dir
  2. @files = <*>;   #get an array with all filenames
  3.  
  4. foreach $file (@files)
  5. {
  6.   $t=stat($file)[9];  #here i tried to use the function you suggested me to get the  datetime of the creation of file
  7.  
  8.   if($t=="2007-07-17")
  9.   {
  10.     print $t . "\n";  
  11.   }
  12. }
  13.  
  14.  
it raises an error compiling....
Maybe it can be a very simple thing to understand but not for me.
I've 3years experience working with vb and c# and get inside perl don't seem to be easy stuff by the way i dont have a nice IDE with intellisense to program perl i must use nano editor which difficult a little bit more ;)

Best regards,
Teixeira
Aug 21 '07 #7
numberwhun
3,503 Expert Mod 2GB
For an editor that has an intellisense type system, try Komodo from Active State. It is what I use to code and it has saved me a few times.

Regards,

Jeff
Aug 21 '07 #8
Thanks Jeff it looks nice, i'll give a try in de free demo

Best regards,
Teixeira
Aug 21 '07 #9
numberwhun
3,503 Expert Mod 2GB
Cool! Glad you like the looks of it. In my opinion, it is one of the better editors out there. Granted, its a bit pricey, but it handles multiple languages (perl, python, php, tcl/tk, html, etc). Its really quite versatile.

LOL! I sound like an add for the IDE. I should bill them for it.

Regards,

Jeff
Aug 21 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Robert Tarantino | last post: by
2 posts views Thread by David Mitchell | last post: by
10 posts views Thread by Martin Ho | last post: by
4 posts views Thread by Robert Iver | last post: by
3 posts views Thread by tkpmep | last post: by
5 posts views Thread by Albert-jan Roskam | last post: by
reply views Thread by Albert-jan Roskam | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.