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

i need to be able to create a file everyday that will have a naming convention so that it will be a timestamp.txt for example 25-may-2001 14:45:34.txt

i've used the following code:

$filename = $timestamp.".txt"; print $filename; sleep 5; open (TEMPFILE, ">$filename");

i used the print and sleep to ensure that $filename was indeed the correct name and it is. the only problem is that the file is not created. i am operating on win2000 and have adminstrator rights. HELP PLEASE

thanks again for your prompt responses!! perlmonks rule!!

Edited by footpad, 26 Jun 2001 - 09:19 am (PDT)

Replies are listed 'Best First'.
Re: still can't create a file using a variable
by lhoward (Vicar) on Jun 25, 2001 at 22:25 UTC
    Try adding some error checking to your code:
    $filename = $timestamp.".txt"; print $filename; sleep 5; open (TEMPFILE, ">$filename") or die "error, can't create $filename : +$!";
    This should tell you while the file create failed.
Re: still can't create a file using a variable
by bikeNomad (Priest) on Jun 25, 2001 at 22:29 UTC
    Are all the characters in your filename legal for your platform? On Windows, for instance, you can't use the ':' character in a filename except after a one-character volume name. Best to use $! and print out error messages, as others have suggested. You also may want to use a numeric timestamp instead.
      i created a datetime constant for use in a few of my scripts that run on Win32 && *NIX, for use in creating temporary directories and files. here's a snippet you may find helpful.
      use POSIX; use constant DATETIME => strftime("%Y-%m-%d_%H-%M-%S", localtime);
      this returns something like "2001-06-25_15-07-32"
      your mileage may vary

      ~Particle

Re: still can't create a file using a variable
by MadraghRua (Vicar) on Jun 26, 2001 at 03:25 UTC
    Try the localtime function which comes with Perl. Alternatively try page 82 of the Perl Cookbook or even chapter 3 of the same book.
    Concerning opening the file, is it being created where you think it should be? Your code example shows that it should be created in the same directory that you are running the script in - is this correct? You might want to add a variable describing the directory path you want to print to and use this to the $filename variable. So
    $path = "C:/path/to/directory" open(TEMPFILE, "$path/$filename") or die "Can't open file: $!\n"; ...whatever close TEMPFILE or die "Can't close file: $!\n";
    If you're still having problems, try out ActiveState's documentation on reading/writing to files and directories. It comes with the software.
    Good luck.

    MadraghRua
    yet another biologist hacking perl....