Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with file names 1

Status
Not open for further replies.

Porshe

Programmer
Jun 5, 2001
38
US
Hello...
I have a perl script that will generate results to another file. The time is generated and printed in the file. But I also want the time to be the name of the file. This is because I will have many files and need to know quickly which file I need by the time it was generated.

Thanks,
Porshe
 
Here's how you would create a file name 'fileHHMM'.

use POSIX;
$time = POSIX::strftime("%H%M",localtime());
$file = "file" . $time;
 
Well - I use this to get the local time:

my ($readtime) = (stat($t_results))[8]; #gets time
$read= localtime($readtime);

Then I want to get the local time as my filename:
###stores report data in HTML file
open OF, ">$read.html" or warn ("Can't Open $t_results: $!\n");

My output is this:
Tue Jun 26 15:29:59 2001.html

*but I don't get anything in the file-
HELP
 
I think this is a file name problem. In Windows, you can't have a colon in the file name. So you could replace the colon with a hyphen, by doing the following:

$read =~ s/:/-/g;

 
If you're doing this on a unix system the spaces in the data/time will give you problems too. Unix really doesn't handle spaces in filenames very well. I'd suggest you convert them to underscores like this:
Code:
$read =~ s/ /_/g;
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Yup, it was the spaces but now I get stuff in the file which is good except the time of my file is this:
Tue_Jun__5_10:52:13_2001.pl

my code looks like:
my ($readtime) = (stat($t_results))[8]; #gets time
$read= localtime($readtime);
$read =~ s/ /_/g;

###stores report data in HTML file
open OF, ">$read.pl" or warn ("Can't Open $t_results: $!\n");

I've tried several things but nothing works.
Thanks for helping!

 
If you want the time in the file to be normally formatted, assign $read to another variable before you edit the spaces, and write that variable to the file instead of $read. Is that what the problem was?
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Oh- the format isn't the problem, sorry. It's the date. It should have today's date and I can't figure out where it's getting that date from.
 
It looks like it's getting the date from the last access data/time for the file whose name is in $t_results (that's what the stat function is returning). If you want the CURRENT date and time, replace these two lines with the following one:
Code:
# replace these two lines
my ($readtime) = (stat($t_results))[8];           #gets time
$read= localtime($readtime);
# with this one
$read = localtime;
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Yes! I knew what the prob was, I just didn't know how to fix it!
Thanks tsdragon!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top