Adding timestap to filename using perl

Hello,

I am trying to create a file in windows and i want the filename to have timestamp as well but something is wrong and i can not understand waht. The code that i use is the following

($cwkday,$cmonth,$cday,$ctime,$cyear) = split(/\s+/, localtime);
 $current_date = $cday."/".$cmonth."/".$cyear." ".$ctime;

 if($index==0)
   { 
      #Create the first line of the new file
      print("Creating the first line of the new file \n");
      push(@array,'Account_id');
      push(@array,'Subscriber_id');
      push(@array,'Package_id');
      push(@array,'effective_date');
      push(@array,'Expiration_date');
      # open (OUTPUT_FILE, '>>C:\perl\output\proration.$cyear$cmonth$cday.txt')||die "$!";
      open (OUTPUT_FILE, '>>C:\Users\christos.rovolis\Desktop\perl\output\proration_$current_date.txt')||die "$!";
      print OUTPUT_FILE (join ("|", @array), "\n");
      $index++;
   }

Using the above code i receive as filename the
proration_$current_date.txt

I tried to use "" instead '' but with no luck

Thank you for your help,

Your $current_date is in human readable format (6/Mar/2015 19:07:00). If you try to create a file using such a format, with the spaces in between the date, perl assumes in it's open routine that these are multiple arguments and it will fail. To rectify this, use the following format YYYYmmdd_HHmmss

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
# Build current_date in YYYYmmdd_HHmmss format
$current_date = $year+1900 . sprintf("%02d",$mon+1) . sprintf("%02d", $mday) . "_" . sprintf("%02d", $hour) . sprintf("%02d", $min) . sprintf("%02d", $sec);

print "$current_date\n";
$index=0;
 if($index==0)
   { 
      #Create the first line of the new file
      print("Creating the first line of the new file \n");
      push(@array,'Account_id');
      push(@array,'Subscriber_id');
      push(@array,'Package_id');
      push(@array,'effective_date');
      push(@array,'Expiration_date');
      # open (OUTPUT_FILE, '>>C:\perl\output\proration.$cyear$cmonth$cday.txt')||die "$!";
      open (OUTPUT_FILE, ">>proration_$current_date.txt")||die "$!";
      print OUTPUT_FILE (join ("|", @array), "\n");
      $index++;
   }
1 Like

Balajesury thank you for your help.

I used the code that you send me andthe problem is with the following line:
open (OUTPUT_FILE, ">>proration_$current_date.txt")||die "$!";

when i use double quotes i get the error 'Invalid argument'

If i use single quotes the date is not printed. Maybe is something related to windows?

Kind Regards

Before you use it in the open routine, could you try to print it. It'll give you an idea if the filename is being built properly or not. Say something like this:

$filename = "C:\whatever\$current_date.txt";
print "$filename\n";

Hello,

When i use:
my $filename="C:\Users\christos.rovolis\Desktop\perl\output\proration_$current_date.txt";
the print of the $filename gives the following output which is incorrect:
C:SERRISTOS.ROVOLISDESKTOPPERLOUTPUTPRORATION_9MAR201513:23:30.TXT
while when i use single quotes in print:
$filename='C:\Users\christos.rovolis\Desktop\perl\output\proration_$current_date.txt'

I get: C:\Users\christos.rovolis\Desktop\perl\output\proration_$current_date.txt

---------- Post updated at 06:55 AM ---------- Previous update was at 06:28 AM ----------

But i think that the problem is in the open function, even if i use the following code

my $number=123;
open (OUTPUT_FILE, ">>proration_$number.txt")||die "$!";

if i use double quotes i get an error and if i use single quotes i get as the name of the file:
proration_$number.txt

Sorry about that.. it's the classic problem with representing path (and a quite common rookie mistake :slight_smile: ). If you use one backslash in front of a character, it may be interpreted as an escape character. So you need to escape the escape character behaviour by introducing another backslash.. so, the right way to do it would be:

$filename = "C:\\Users\\christos.rovolis\\Desktop\\perl\\output\\proration_$current_date.txt";

And single quotes are used in perl if you want to mean the literal sense of a string. Something like WYSIWYG in a variable. So $current_date will be exactly represented as $current_date inside a single quote. To interpret variables inside a string, use double quotes.