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

I have a directory in which there are a number of files with the timestamp tagged along with the name. For example: AAAA*20111031*.log AAAA*sfdaf*20111031*.log etc I need to automate copying of these files to a different directory. Since the date changes daily I tried using a global variable $date = `date +%Y%m%d`. I am not able to use a statement like if ($file =~ AAAA\*\$date) {copy program} where $file globs through the directory Any suggestions ???

Replies are listed 'Best First'.
Re: Help in using backtick operator
by Perlbotics (Archbishop) on Oct 31, 2011 at 20:23 UTC

    Not sure if I understand your question correctly, but I would start with:

    • chomp $date; , or better:
    • build $date without shelling out (e.g. POSIX::strftime or localtime and sprintf)
    • using a correct regular expression, e.g. if ($file =~ m/AAAA\*.*?$date/) { ... }
      Update: Missed, that there is a literal * in the filename. \$ matches literal $, not $date. Non-greedy matching is probably not necessary here, but does no harm (in response to question below). It would be a good idea to also anchor the regexp.
    • (also add usestrict; and use warnings; if not already done so)
    HTH

      Yeah i did notice i made a mistake in the regex. But even after making the change it isn't working. if ($file =~ m/AAAA\*$date/) is not working. I haven't understood why you used ? in the regex too.
Re: Help in using backtick operator
by Marshall (Canon) on Oct 31, 2011 at 22:20 UTC
    I take it that 20111031 is the date of that log file. And it appears that the logging software starts a new log file each day? So, all you have to do is move the old log files to some archive directory if it is from yesterday. If that's not right, then please explain.

    This type of string "20111031" is great as long as it has leading zeroes for the months and years as that can be used in a simple string comparison against the current date string.

    So how to get the current date string?

    #!/usr/bin/perl -w use strict; my ($year,$month,$day) = (gmtime(time))[5,4,3]; $year +=1900; $month +=1; my $date = sprintf("%4d%02d%02d", $year,$month,$day); print "date = $date\n"; #date = 20111031
    Extract the date string from the filename using a regex, then use a simple string comparison and then move the file it if is less than today's date. Here's how to extract the date string..

    #!/usr/bin/perl -w use strict; while (<DATA>) { #one of many, many ways to get the 8 digit date my ($date) = /(\d{8}).*.log$/; print "$date\n"; } =prints 20111031 // Oct 31, 2011 20110101 // Jan 01, 2011 =cut __DATA__ AAAA*20111031*.log AAAA*sfdaf*20110101*.log
    I suppose from the formulation of the problem that you are thinking in terms of Windows wildcards and not UNIX regular expressions. Experiment with the above code and I think it will work for you. Report back if you have problems.
Re: Help in using backtick operator
by mwp (Hermit) on Oct 31, 2011 at 21:14 UTC

    Is there a literal asterisk in your filename, or do you want to expand the asterisks in your example? If the latter, I echo Perlbotics' advice to use localtime() and sprintf() to get the date, and suggest you do something like:

    foreach my $filename (<AAAA*$date*.log>) { ... }
    Check out the glob() builtin and the <> operator in perlop for more information.

    alakaboo