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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • teixeira
    New Member
    • Dec 2006
    • 8

    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
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    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::Rec ursive. 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

    Comment

    • teixeira
      New Member
      • Dec 2006
      • 8

      #3
      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

      Comment

      • numberwhun
        Recognized Expert Moderator Specialist
        • May 2007
        • 3467

        #4
        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

        Comment

        • teixeira
          New Member
          • Dec 2006
          • 8

          #5
          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

          Comment

          • KevinADC
            Recognized Expert Specialist
            • Jan 2007
            • 4092

            #6
            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.

            Code:
            use File::Copy;
            chdir('path/to/folder') or die "$!";
            opendir(DIR,'.') or die "$!";
            my @list = grep {here you calculate the date range using stat()}  readdir DIR;
            close DIR;
            foreach my $file (@list) {
               copy file stuff here
            }
            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.

            Comment

            • teixeira
              New Member
              • Dec 2006
              • 8

              #7
              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

              Code:
              chdir("/home/myname/Desktop"); #changes dir
              @files = <*>;   #get an array with all filenames
              
              foreach $file (@files)
              {
                $t=stat($file)[9];  #here i tried to use the function you suggested me to get the  datetime of the creation of file
                
                if($t=="2007-07-17")
                {
                  print $t . "\n";  
                }
              }
              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

              Comment

              • numberwhun
                Recognized Expert Moderator Specialist
                • May 2007
                • 3467

                #8
                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

                Comment

                • teixeira
                  New Member
                  • Dec 2006
                  • 8

                  #9
                  Thanks Jeff it looks nice, i'll give a try in de free demo

                  Best regards,
                  Teixeira

                  Comment

                  • numberwhun
                    Recognized Expert Moderator Specialist
                    • May 2007
                    • 3467

                    #10
                    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

                    Comment

                    Working...