0

I am getting some error when i am trying to run the following code ,

My requirement is to find the file with date-format Ex : I have files in a directory with names

    01-02-2013.tar.gz
    02-02-2013.tar.gz

.....

so now i am trying to find a file 01-02-2013

here is the code snippet

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time-6*24*60*60);
$year += 1900;
$mon += 1;
$mday = '0'.$mday if ($mday<10);
$mon = '0'.$mon if ($mon<10);
my $date_folder =~ /"$mday-$mon-$year"/;
        if (-e $date_folder){
                 unlink $date_folder or die ("Cannot Delete $date_folder : $!");
                        }

here i am basically trying to find the file 6 days back(for example : 01-02-2013.tar.gz ) of current date , SomeWhere i feel like my $date_folder =~ /"$mday-$mon-$year"/; is going wrong ,

Any Suggestions ?

mviswa
  • 147
  • 1
  • 1
  • 10

2 Answers2

1

Just remove the quotation marks:

my $date_folder =~ /$mday-$mon-$year/;

they are just literal characters in a regex.

Oh, oh, oh, sorry, I overlooked that the code makes no sense at all!

So, some basics: if you have something like $date_folder =~ /$mday-$mon-$year/;, your regex is matched against the content of $date_folder. If this is not set you get your "Use of uninitialized value $date_folder in pattern match (m//)".
$date_folder =~ /$mday-$mon-$year/; returns true or false.

For more details see:

In your case you need to loop over your files in the folder and inside the loop you need to check the current filename with your regex and if it is matching then do whatever you want with the file.

For more details see:

Community
  • 1
  • 1
stema
  • 90,351
  • 20
  • 107
  • 135
  • tried your thing but getting the following error : **Use of uninitialized value $date_folder in pattern match (m//)** – mviswa Aug 13 '13 at 06:53
  • Where do you set the value of `$date_folder`? It should contain the name of the current file you want to check. – stema Aug 13 '13 at 06:56
  • **my $date_folder =~ /$mday-$mon-$year/;** this one set the **$date_folder** to the value of the matching regex right , and i installed the script in the same directory where the files are in , for sure case i have added a **chdir('_path_')** but this one did not help either. @stema – mviswa Aug 13 '13 at 07:01
  • so you mean to use _OPENDIR_ , _READDIR_ and loop through directory ? – mviswa Aug 13 '13 at 07:18
  • Yes, or how do you want to get your filenames? Put some more links in my answer. – stema Aug 13 '13 at 07:22
  • I thought this should work `my $date_folder = "$mday-$mon-$year"; opendir(DIR,"/home/DB_BKPS"); my @del_file = grep (/$date_folder/,readdir(DIR)); closedir(DIR); print "$del_file[0]\n"; foreach my $file (@del_file){ chomp($file); unlink if ($file = "$del_file[0]");` but is throwing the following error **Use of uninitialized value $_ in unlink at tmp_perl.pl** – mviswa Aug 13 '13 at 08:04
1

Finally this thing worked for me

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time-6*24*60*60);
$year += 1900;
$mon += 1;
$mday = '0'.$mday if ($mday<10);
$mon = '0'.$mon if ($mon<10);

opendir(DIR,"/home/DB_BKPS");
        my @files = grep (/$mday-$mon-$year/,readdir(DIR));
        print "@files\n";
                foreach my $file (@files){
                chomp($file);
                unlink ($files[0]);
                        }
closedir(DIR);

Thanks you everyone for your time

mviswa
  • 147
  • 1
  • 1
  • 10