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.
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
Code:
#!/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.
Last edited by Corona688; 04-25-2011 at 06:06 PM..
Reason: simplify out sprintf
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.
Code:
#!/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?
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
Code:
#!/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.
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.".
Hello Folks,
I have an requirement, where i need to get total count of the file based on creation date with there filename selected pattern.
Filename: MobileProtocol.20171228T154200.157115.udr
I want to get the count of files created on each day based on a pattern find.
find . -type... (7 Replies)
Hello ,
I am looking for a script to print file name and its last updated time.
FILE CREATION-TIME FILE-NAME
24/10/2017 12:34 TDR-IU-8-2017.10.24.07:40:00-2017.10.24.07:45:00
when we run l command it print the directory and the files with details like permission,... (1 Reply)
Hi guys,
I had a scenario...
1. I had to get the previous days date in yyyymmdd format
2. i had to create a file with Date inthe format yyyymmdd.txt format
both are different
thanks guys in advance.. (4 Replies)
The scope of the shell/perl script is to read the input text file. Validate the expiry date of each certificate and send the mail to the user. The user takes action to add the new certificate to the storage file and user owns the responsibility to update the input text file with the new certificate... (5 Replies)
Hi,
Can anyone tell me a process to find the creation date of a file in a directory.
If suppose I have a file in a directory created in 2009
-rw-r--r-- 1 xyz guest 2480 Jul 16 2009 sample.txt
The command should return the the file creation date as 16/07/2009
thanks, (2 Replies)
Hi,
I just need to know way of getting date of file when it was created.
eg i have a file abc created on 23 aug. Now i need to know date of file i.e. 23 aug. How can i get that data.
Thanks
Sarbjit (7 Replies)
I want to gzip a file and append the creation date to the end of the file. How can I accomplish this task. Basically they are log files which need a creation date stamp appended to make sure they do not overwrite other log files.
-jack (3 Replies)
Hi All,
I need to get file creation date.
I have to access one file who's name is like...
abc.log092308
and the date changes as per current date. And i am accessing this file next day. meance in above case i will access above file on 09-24-2008
Also one problem is that this file... (2 Replies)
Dear Expert,
Is there a command to do that in Unix?
In such a way that we don't need to actually "write" or
modified the content.
-- monkfan (4 Replies)