Help with creating a text file in perl with file creation date.

Hi,

I am quite new to Perl scripting and i need to create a .TXT file using perl, with fields (A,B,C,D,E), and this text file should be named with current file creation date "XYZ_CCYYMMDD.TXT" (i.e.XYZ_2011042514:33 PM).

Can anyone who has done this, please share their expertise on this one.

Thanks.

Try:

#!/usr/bin/perl
chomp($d=`date +%Y%m%d%H:%M_%p`);
open F,">XYZ_$d.txt";
print F "A,B,C,D,E\n";
1 Like

It seems a common misconception that there's a Specific Way��� to do things in a computer language and you can find all the answers by googling "how do I do X in perl". It really doesn't work that way. You have to break it down into logical steps.

1) Logical step 1. How do I get the time in perl? I guess wildly and try perldoc -f time That returns it in seconds. A start.

2) Logical step 2. How can I convert that into something I can use? Well, the 'time' function documentation suggests the localtime function and perldoc -f localtime suggests the strftime function and the POSIX module and even gives examples. time isn't needed apparently since localtime calls it implicitly unless you give it otherwise. man strftime also tells you what strings strftime needs.

3) Logical step 3. How can I open a string as a filename? The same way you open any other filename. perldoc -f open

So you get

#!/usr/bin/perl

use POSIX;

my $filename=strftime("filename_%y%M%d.TXT", localtime);
print "filename is $filename\n";
open(FILE, ">$filename") || die("Couldn't open file");

print FILE "hello world\n";

close(FILE);
exit(0);

I knew none of this by heart, I just followed the trail of instructions.

1 Like

We have a function in Perl called "Open" which creates a file.

I am trying to create the .TXT file in the specified directory.
I tried concatinating the Directory with the string.

#!/usr/bin/perl

use POSIX;

#Directory where the text file will be created.
$directory =  '/space/data';
if (!(-d "$directory")) { #check if directory exists -  returns true if doesn't exist.
        printf "No directory\n", $? >> 8;
        exit (1); #exit because directory doesn't exist.
}


my $filename=strftime("filename_%y%M%d.TXT", localtime);
print "filename is $filename\n";
open($directory.">$filename") || die("Couldn't open file");
#open(FILE, ">$filename") || die("Couldn't open file");

print FILE "hello world\n";

close(FILE);
exit(0);

I am able to execute this script, but i am not getting the .TXT file been created in the folder '/space/data'.

Where am i going wrong? How can i create this .TXT file in the specified folder/directory?

Appreciate any help..

Answer so detailed like this is really great.
There's a Chinese proverb that reads: ", ", which means "If you give him a fish, he'll only have a single meal, if you teach him to fish, he will feed himself for his whole life.".

Thank you~

You did not concatenate anything. Even if you did, you'd end up with a '<' instead of a slash between dir and filename.

open() takes a filehandle as a first argument; you are missing that. So the call you need is as follows:

open(FILE, ">$directory/$filename") || die("Couldn't open file");

The formatting of date you probably want is "%y%m%d", not %M, which would print minute, not month.

Use

#!/usr/bin/perl -w

; the -w switch turns warnings on; very helpful for debugging.