darrengan has asked for the wisdom of the Perl Monks concerning the following question:

Hi friends,
i have a folder with the following files:

file1_name_051111.txt
file2_name_051113.txt
file3_name_051113.txt
file4_name_051110.txt

with yymmdd numbering format as the file name

i need to scan this folder and get the yymmdd of the file and create a seperate folder with the yymmdd. Then move the revelent file with the same yymmdd into the folder.
how do i do it with perl and unix?

Cheers,
Darren

Replies are listed 'Best First'.
Re: get file name to create folder
by tirwhan (Abbot) on Nov 08, 2005 at 10:22 UTC
    This will work on the current folder:
    #!/usr/bin/perl use warnings; use strict; use File::Copy; while (my $filename=<*.txt>) { next if !($filename =~ m/_(\d{6})\.txt\z/); my $date=$1; if (!-e $date) { mkdir $date; } else { if (!-d $date) { die "$date already exists and is not a folder"; } elsif (!-x $date) { die "Wrong permissions on preexisting folder $date"; } } move($filename,"$date/") or die "Couldn't move $filename to $date: $!"; }
    If you want to remove the _yymmdd from the file name while moving, replace the last line with
    my $newname = $filename; $newname =~ s/_\d{6}//; move($filename,"$date/$newname") or die "Couldn't move $filename to $date/$newname: $!";

    Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan
Re: get file name to create folder
by b10m (Vicar) on Nov 08, 2005 at 10:51 UTC

    ... and UNIX (BASH in this case)

    #!/bin/bash for FILE in *.txt do DIR=${FILE##*_} DIR=${DIR/.txt/} [ ! -d $DIR ] && mkdir $DIR mv $FILE $DIR done
    --
    b10m

    All code is usually tested, but rarely trusted.
Re: get file name to create folder
by Rajeshk (Scribe) on Nov 08, 2005 at 10:25 UTC
    Hi,

    Try this one.

    use strict; use warnings; use File::Copy; my (@files, $file, $folder_name); opendir(DIR, ".") or die "Can't Open Current Directory: $!"; @files = grep /\d{6}\.txt$/, readdir(DIR); close DIR; foreach $file(@files){ if($file =~ /(\d{6})\.txt$/){ $folder_name = $1; mkdir $folder_name; move $file, "\./$folder_name/"; } }

    Thanks,

    Rajesh.K

    Edit: g0n - replaced pre tags with code tags

Re: get file name to create folder
by l.frankline (Hermit) on Nov 08, 2005 at 10:13 UTC
    Hi,

    Try this....

    use File::Copy; @files = qw(file1_name_051111.txt file2_name_051113.txt file3_name_051 +113.txt file4_name_051110.txt); foreach $fi (@files) { ($fol) = $fi =~m#(\d{6})\.txt#; mkdir $fol; copy("$fi", "$fol\\$fi"); unlink $fi; }
    Regards

    Franklin

    Edit: g0n - corrected code tags

Re: get file name to create folder
by darrengan (Sexton) on Nov 09, 2005 at 03:00 UTC
    Hi monks...
    Thanks for all the feedbacks... really kick me going...
    i manage to get what i want from the coding provided by you guys but i still need another small help.

    Understand that grep /\d{6}\.txt$/ will get 051109 from file1_name_051109.txt
    but how do i get the same result "051109" from file1_name_051109123024.txt
    This is more like a grep problem

    Thanks!