#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2009
    Posts
    14
    Rep Power
    0

    move files with YYYYMMDD to the right folders


    hi,

    let's say I have a bunch of directories with this format (YYYYMMDD), now I have a bunch of files (with *YYYYMMDD*.*) but I need to move files to destination folder based on the DATE (YYYYMMDD) in the file names. what is best way to achieve this?

    Thanks
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,474
    Rep Power
    1875
    If there is only ever one sequence of 8 digits in each filename, then perhaps
    Code:
    perl -e 'foreach $i ( @ARGV ) { print "mv $i $1/$i\n" if ( $i =~ /.(\d{8})./ ); }' *
    The two dots on either end of the RE ensure you don't match just the directories themselves.

    If you're on a Unix/Linux host, then the rename function should suffice.
    Eg
    Code:
    perl -e 'foreach $i ( @ARGV ) { rename "$i","$1/$i" if ( $i =~ /.(\d{8})./ ); }' *
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2009
    Posts
    14
    Rep Power
    0
    Thanks. How about create folder YYYYMMDD if it doesn't exist?


    Originally Posted by salem
    If there is only ever one sequence of 8 digits in each filename, then perhaps
    Code:
    perl -e 'foreach $i ( @ARGV ) { print "mv $i $1/$i\n" if ( $i =~ /.(\d{8})./ ); }' *
    The two dots on either end of the RE ensure you don't match just the directories themselves.

    If you're on a Unix/Linux host, then the rename function should suffice.
    Eg
    Code:
    perl -e 'foreach $i ( @ARGV ) { rename "$i","$1/$i" if ( $i =~ /.(\d{8})./ ); }' *
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,474
    Rep Power
    1875
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo