Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Search for a file created today and rename it by appending the current date and copy it to a different location

by perlgb (Initiate)
on Nov 22, 2012 at 15:04 UTC ( #1005144=perlquestion: print w/ replies, xml ) Need Help??
perlgb has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have a minimum knowledge in perl and trying to get my head around to do a script. I need to write a script that look for a file in a folder where modified/created date is today and and rename it by appending the current date at the end of the file name. Then copy it to a different location,

File Name format - "ABC DEF GHI 3648.html" - This file is generated everyday with a random number(3468).

Need to rename it to - "ABC_DEF_GHI_22112012.html" I have started with the below script,

#!/usr/bin/perl use strict; # Specify the folder for file search my $startFolder = "D:\\XXX\\XXX\\"; opendir (DIR, $startFolder) || die "Can't access $startFolder"; # to search for the document foreach my $file (readdir(DIR)) { #Modified today and specify the file pattern next unless (-M $file <= 1 && $file =~ m/\ABC DEF GHI/); } closedir(DIR);

The next step is to rename the file as specified above and copy it to a new location. Please let me know the best way to do it. Thanks.

Comment on Search for a file created today and rename it by appending the current date and copy it to a different location
Download Code
Re: Search for a file created today and rename it by appending the current date and copy it to a different location
by 2teez (Priest) on Nov 22, 2012 at 16:12 UTC

    "..The next step is to rename the file ..."
    then check rename function
    "..and copy it to a new location."
    you can use File::Copy

    You may also want to check File::Find, to easily transverse a directory tree.

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
      ...even easier to use: File::Find::Rule.

      «The Crux of the Biscuit is the Apostrophe»

      Thanks. Could you please tell me how can i use the File::Find function to search for a file modified or created today?

        File::Find traverses a directory tree and calls the wanted function for each directory entry found. Inside the wanted function, you can use lstat or stat or one of the -X functions to decide if you want to process that directory entry. Example:

        #!/usr/bin/perl use strict; use warnings; use File::Find; find( { wanted => sub { return unless -l $File::Find::name; print "Found a symlink: $File::Find::name\n"; } }, '.' );

        File::Find comes with a script named find2perl. You invoke it like find, but instead of traversing directories, it writes perl code for performing the directory traversal using File::Find to STDOUT. The generated code is not pretty, may have some unused parts, but it generally works:

        /tmp>find2perl . -type l #! /usr/bin/perl -w eval 'exec /usr/bin/perl -S $0 ${1+"$@"}' if 0; #$running_under_some_shell use strict; use File::Find (); # Set the variable $File::Find::dont_use_nlink if you're using AFS, # since AFS cheats. # for the convenience of &wanted calls, including -eval statements: use vars qw/*name *dir *prune/; *name = *File::Find::name; *dir = *File::Find::dir; *prune = *File::Find::prune; sub wanted; # Traverse desired filesystems File::Find::find({wanted => \&wanted}, '.'); exit; sub wanted { my ($dev,$ino,$mode,$nlink,$uid,$gid); (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) && -l _ && print("$name\n"); }

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: Search for a file created today and rename it by appending the current date and copy it to a different location
by peter (Sexton) on Nov 23, 2012 at 13:16 UTC

    You should see what the filename is. I think it's only the filename and not the absolute filename.

    The -M operator won't work with just the relative filename, because it probably doesn't exists in the current directory.

    Peter Stuifzand

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1005144]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (14)
As of 2014-10-23 12:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (125 votes), past polls