Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Parsing File Timestamp?

by Limo (Scribe)
on Aug 26, 2000 at 08:53 UTC ( #29785=perlquestion: print w/replies, xml ) Need Help??

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

My first program. It basically reads data and creates a file based upon it. This program runs daily. My problem is in naming thiese files, I need to append a "date identifier" ie "myfile.date" Here's the trick. The program may be run on week/month old data, at times. So, I thought of two ways to solve this:

1. parse "timestamp" for what I need. CON: don't know if it will work or how. What if file is "touched"?

2. I could grab it from tha path ie /export/home/mars-stats/device/config/082500/blah/blah/blah

CON: what if path changes?

Unfortunately, there's nothing contained in the data file name or the data itself that is relevant. Any suggestions on how to approach this?

Replies are listed 'Best First'.
RE: Parsing File Timestamp? (hopefully helpful threads)
by ybiC (Prior) on Aug 26, 2000 at 17:17 UTC
RE: Parsing File Timestamp?
by tenatious (Beadle) on Aug 26, 2000 at 22:30 UTC
    If I understand you correctly, you want to place a timestamp at the end of the filename. I'd advise against this because it prevents some machines from associating the file to an extension. Instead build the filename and place the timestamp in a location where you can easily get at it. e.g.
    # read info # and then we are ready to build our filename # unless you are writing binary data, the following should # do the trick. $time = time; $filename="myfile_".$time.".txt"; # (and then just print using $filename)

    time will give a machine-readable time in seconds from the epoch. It should be a valid way of doing things until around 2030. Hopefully, by then we won't have computers :)

    If the time needs to be readable by a human, you can do something like:

    $time = scalar localtime; $time =~ tr/ /_/; #get rid of the spaces; $filename = "myfile-".$time.".txt";
Re: Parsing File Timestamp?
by Limo (Scribe) on Aug 26, 2000 at 23:21 UTC
    This won't work, because it returns the current date. First, there's a typo in my post. The example should read:
    
    
    
    /export/home/mars-stats/device/config/2000/0825/blah/blah/blah.txt
    
    
    Your code would return:
    
    "blah-Sun_Aug_27_03:18:29_2000.txt"
    
    
    Which is a problem, if I want to run the program on a month old file, and create a file with THAT date on it. So, my new brilliant (?) idea is the following: Since each day, a new directory is created which contains the files I need to process, read the path to the file that I want to process:
    
    #get current working directory; let's say it returns:
    
    $path = "/export/home/mars-stats/device/config/2000/0825/blah/blah/blah.txt"
    
    
    
    Then create the rule:
    
    
    if while reading $path, you  encounter:
    
    begins with "/", followed by 1 or more "/", then one or more word characters, then a "1" or 
    a "2" followed by more than one digit, followed by one or more word characters
    until the end of the line, grab the string between the first word 
    character preceding a digit and the next word character,and replace the "/" with "."
    
    this should return "2000.0825" which I could use to append to my new file. This rule, if I can figure out the syntax, would cover any input file created in between the years 1900 - 2999. Not bad mileage! Am I missing something here? Be gentle; this is the first program I've ever written in ANY language!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://29785]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2022-12-07 18:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?