Attributing an old time stamp for a new file.

Discussion in 'Ruby' started by Peter Bailey, Mar 6, 2007.

  1. Peter Bailey

    Peter Bailey Guest

    Hello,
    I need to read some data from some older files and put that info. into
    simple ASCII text files. But, I want those new text files to have the
    same time stamp as the original files that the data came from. Is there
    any way to do that?

    Thanks,
    Peter

    --
    Posted via http://www.ruby-forum.com/.
    Peter Bailey, Mar 6, 2007
    #1
    1. Advertising

  2. Peter Bailey

    Guest

    On Tue, 6 Mar 2007, Peter Bailey wrote:

    > Hello,
    > I need to read some data from some older files and put that info. into
    > simple ASCII text files. But, I want those new text files to have the
    > same time stamp as the original files that the data came from. Is there
    > any way to do that?
    >
    > Thanks,
    > Peter


    harp: ~> ri FileUtils.touch
    -------------------------------------------------------- FileUtils#touch
    touch(list, options = {})
    ------------------------------------------------------------------------
    Options: noop verbose

    Updates modification time (mtime) and access time (atime) of
    file(s) in +list+. Files are created if they don't exist.

    FileUtils.touch 'timestamp'
    FileUtils.touch Dir.glob('*.c'); system 'make'


    -a
    --
    be kind whenever possible... it is always possible.
    - the dalai lama
    , Mar 6, 2007
    #2
    1. Advertising

  3. On Mar 6, 2007, at 9:29 AM, wrote:
    > On Tue, 6 Mar 2007, Peter Bailey wrote:
    >> Hello,
    >> I need to read some data from some older files and put that info.
    >> into
    >> simple ASCII text files. But, I want those new text files to have the
    >> same time stamp as the original files that the data came from. Is
    >> there
    >> any way to do that?
    >>
    >> Thanks,
    >> Peter

    >
    > harp: ~> ri FileUtils.touch
    > --------------------------------------------------------
    > FileUtils#touch
    > touch(list, options = {})
    > ----------------------------------------------------------------------
    > --
    > Options: noop verbose
    >
    > Updates modification time (mtime) and access time (atime) of
    > file(s) in +list+. Files are created if they don't exist.
    >
    > FileUtils.touch 'timestamp'
    > FileUtils.touch Dir.glob('*.c'); system 'make'
    >
    >
    > -a
    > --
    > be kind whenever possible... it is always possible.
    > - the dalai lama


    Unless there's a way to specify to FileUtils.touch the value of the
    timestamp, you can use the touch command (if you're on a sufficiently
    unix-like platform):

    touch -r oldfile newfile

    ...which sets the times on 'newfile' to be the same as the -r
    (eference) file 'oldfile'

    -Rob

    Rob Biedenharn http://agileconsultingllc.com
    Rob Biedenharn, Mar 6, 2007
    #3
  4. Peter Bailey

    Peter Bailey Guest

    unknown wrote:
    > On Tue, 6 Mar 2007, Peter Bailey wrote:
    >
    >> Hello,
    >> I need to read some data from some older files and put that info. into
    >> simple ASCII text files. But, I want those new text files to have the
    >> same time stamp as the original files that the data came from. Is there
    >> any way to do that?
    >>
    >> Thanks,
    >> Peter

    >
    > harp: ~> ri FileUtils.touch
    > -------------------------------------------------------- FileUtils#touch
    > touch(list, options = {})
    > ------------------------------------------------------------------------
    > Options: noop verbose
    >
    > Updates modification time (mtime) and access time (atime) of
    > file(s) in +list+. Files are created if they don't exist.
    >
    > FileUtils.touch 'timestamp'
    > FileUtils.touch Dir.glob('*.c'); system 'make'
    >
    >
    > -a


    Thanks. ri verbage is rarely clear to me. These sample lines definitely
    seem to be referring to Unix stuff. "make" means nothing in Windows.
    where I am. Thanks, anyway.

    --
    Posted via http://www.ruby-forum.com/.
    Peter Bailey, Mar 6, 2007
    #4
  5. Peter Bailey

    Peter Bailey Guest

    > Unless there's a way to specify to FileUtils.touch the value of the
    > timestamp, you can use the touch command (if you're on a sufficiently
    > unix-like platform):
    >
    > touch -r oldfile newfile
    >
    > ...which sets the times on 'newfile' to be the same as the -r
    > (eference) file 'oldfile'
    >
    > -Rob
    >
    > Rob Biedenharn http://agileconsultingllc.com
    >


    Nope, I'm on Windows. Having a "touch" like that would certainly be
    nice, and simple. I'm going to keep playing with the FileUtils.touch to
    see what I can do. Thanks.

    --
    Posted via http://www.ruby-forum.com/.
    Peter Bailey, Mar 6, 2007
    #5
  6. On Wed, Mar 07, 2007 at 12:34:51AM +0900, Peter Bailey wrote:
    > unknown wrote:
    > > On Tue, 6 Mar 2007, Peter Bailey wrote:
    > >
    > >> Hello,
    > >> I need to read some data from some older files and put that info. into
    > >> simple ASCII text files. But, I want those new text files to have the
    > >> same time stamp as the original files that the data came from. Is there
    > >> any way to do that?
    > >>
    > >> Thanks,
    > >> Peter

    > >
    > > harp: ~> ri FileUtils.touch
    > > -------------------------------------------------------- FileUtils#touch
    > > touch(list, options = {})
    > > ------------------------------------------------------------------------
    > > Options: noop verbose
    > >
    > > Updates modification time (mtime) and access time (atime) of
    > > file(s) in +list+. Files are created if they don't exist.
    > >
    > > FileUtils.touch 'timestamp'
    > > FileUtils.touch Dir.glob('*.c'); system 'make'
    > >
    > >
    > > -a

    >
    > Thanks. ri verbage is rarely clear to me. These sample lines definitely
    > seem to be referring to Unix stuff.


    Not really. The first creates an empty file called 'timestamp'; the second
    updates the timestamps of all files *.c in the current directory, and then
    runs 'make' (which could be a DOS command)

    > "make" means nothing in Windows.
    > where I am. Thanks, anyway.


    Use the source :)

    Find fileutils.rb on your system (on mine it's
    /usr/lib/ruby/1.8/fileutils.rb but it'll be different under Windows)

    Scan down to "def touch". You'll see it's a simple function which just calls

    File.utime(t, t, path)

    which I think is what you're really looking for.

    ------------------------------------------------------------ File::utime
    File.utime(atime, mtime, file_name,...) => integer
    ------------------------------------------------------------------------
    Sets the access and modification times of each named file to the
    first two arguments. Returns the number of file names in the
    argument list.
    Brian Candler, Mar 6, 2007
    #6
  7. Peter Bailey

    Peter Bailey Guest

    Brian Candler wrote:
    > On Wed, Mar 07, 2007 at 12:34:51AM +0900, Peter Bailey wrote:
    >> >> Peter
    >> > FileUtils.touch 'timestamp'
    >> > FileUtils.touch Dir.glob('*.c'); system 'make'
    >> >
    >> >
    >> > -a

    >>
    >> Thanks. ri verbage is rarely clear to me. These sample lines definitely
    >> seem to be referring to Unix stuff.

    >
    > Not really. The first creates an empty file called 'timestamp'; the
    > second
    > updates the timestamps of all files *.c in the current directory, and
    > then
    > runs 'make' (which could be a DOS command)
    >
    >> "make" means nothing in Windows.
    >> where I am. Thanks, anyway.

    >
    > Use the source :)
    >
    > Find fileutils.rb on your system (on mine it's
    > /usr/lib/ruby/1.8/fileutils.rb but it'll be different under Windows)
    >
    > Scan down to "def touch". You'll see it's a simple function which just
    > calls
    >
    > File.utime(t, t, path)
    >
    > which I think is what you're really looking for.
    >
    > ------------------------------------------------------------ File::utime
    > File.utime(atime, mtime, file_name,...) => integer


    Thanks, Brian! Well, no, "make" is meaningless in DOS or Windows,
    believe me. But, I did look into the fileutils.rb file and I do indeed
    see what you point out. I'd never have thought to look into one of these
    files. Amazing to me. So, I've tried this and it still doesn't work:

    FileUtils.touch("file1", "file2")

    What does "t" stand for above?

    --
    Posted via http://www.ruby-forum.com/.
    Peter Bailey, Mar 6, 2007
    #7
  8. On Wed, Mar 07, 2007 at 01:43:52AM +0900, Peter Bailey wrote:
    > > ------------------------------------------------------------ File::utime
    > > File.utime(atime, mtime, file_name,...) => integer

    >
    > Thanks, Brian! Well, no, "make" is meaningless in DOS or Windows,
    > believe me. But, I did look into the fileutils.rb file and I do indeed
    > see what you point out. I'd never have thought to look into one of these
    > files. Amazing to me. So, I've tried this and it still doesn't work:
    >
    > FileUtils.touch("file1", "file2")
    >
    > What does "t" stand for above?


    Time. (atime and mtime are last accessed time and last modified time,
    respectively).
    Brian Candler, Mar 6, 2007
    #8
  9. On Mar 6, 2007, at 11:43 AM, Peter Bailey wrote:
    > Brian Candler wrote:
    >> On Wed, Mar 07, 2007 at 12:34:51AM +0900, Peter Bailey wrote:
    >>>>> Peter
    >>>> FileUtils.touch 'timestamp'
    >>>> FileUtils.touch Dir.glob('*.c'); system 'make'
    >>>> -a
    >>>
    >>> Thanks. ri verbage is rarely clear to me. These sample lines
    >>> definitely
    >>> seem to be referring to Unix stuff.

    >>
    >> Not really. The first creates an empty file called 'timestamp'; the
    >> second
    >> updates the timestamps of all files *.c in the current directory, and
    >> then
    >> runs 'make' (which could be a DOS command)
    >>
    >>> "make" means nothing in Windows.
    >>> where I am. Thanks, anyway.

    >>
    >> Use the source :)
    >>
    >> Find fileutils.rb on your system (on mine it's
    >> /usr/lib/ruby/1.8/fileutils.rb but it'll be different under Windows)
    >>
    >> Scan down to "def touch". You'll see it's a simple function which
    >> just
    >> calls
    >>
    >> File.utime(t, t, path)
    >>
    >> which I think is what you're really looking for.
    >>
    >> ------------------------------------------------------------
    >> File::utime
    >> File.utime(atime, mtime, file_name,...) => integer

    >
    > Thanks, Brian! Well, no, "make" is meaningless in DOS or Windows,
    > believe me. But, I did look into the fileutils.rb file and I do indeed
    > see what you point out. I'd never have thought to look into one of
    > these
    > files. Amazing to me. So, I've tried this and it still doesn't work:
    >
    > FileUtils.touch("file1", "file2")
    >
    > What does "t" stand for above?


    it is set to Time.now, but you want to do something like:

    newfile = "#{oldfile}.txt"
    File.open(oldfile) do |o|
    File.open(newfile, 'w') do |n|
    # while line = o.gets
    # n << fixup(line)
    # end
    end
    end
    t = File.mtime(oldfile)
    File.utime(t,t, newfile)

    Of course the inner loop is whatever you need it to be. The key here
    is to just do the part of FileUtils.touch that you need which is the
    call to File.utime on the new file. Since you just had to create it
    (and fill it with something), you don't have to create it (the rescue
    clause in touch).

    (I hadn't run across the File.utime before, but I should've expected
    that from Perl's influence.)

    -Rob

    Rob Biedenharn http://agileconsultingllc.com
    Rob Biedenharn, Mar 6, 2007
    #9

  10. > -----Original Message-----
    > Brian Candler wrote:
    > > On Wed, Mar 07, 2007 at 12:34:51AM +0900, Peter Bailey wrote:
    > >> >> Peter
    > >> > FileUtils.touch 'timestamp'
    > >> > FileUtils.touch Dir.glob('*.c'); system 'make'
    > >> >=20
    > >> >=20
    > >> > -a
    > >>=20
    > >> Thanks. ri verbage is rarely clear to me. These sample lines=20
    > >> definitely seem to be referring to Unix stuff.

    > >=20
    > > Not really. The first creates an empty file called 'timestamp'; the=20
    > > second updates the timestamps of all files *.c in the current=20
    > > directory, and then runs 'make' (which could be a DOS command)
    > >=20
    > >> "make" means nothing in Windows.=20
    > >> where I am. Thanks, anyway.

    > >=20
    > > Use the source :)
    > >=20
    > > Find fileutils.rb on your system (on mine it's=20
    > > /usr/lib/ruby/1.8/fileutils.rb but it'll be different under Windows)
    > >=20
    > > Scan down to "def touch". You'll see it's a simple function=20

    > which just=20
    > > calls
    > >=20
    > > File.utime(t, t, path)
    > >=20
    > > which I think is what you're really looking for.
    > >=20
    > >=20

    > ------------------------------------------------------------=20
    > File::utime
    > > File.utime(atime, mtime, file_name,...) =3D> integer

    >=20
    > Thanks, Brian! Well, no, "make" is meaningless in DOS or=20
    > Windows, believe me. But, I did look into the fileutils.rb=20
    > file and I do indeed see what you point out. I'd never have=20
    > thought to look into one of these files. Amazing to me. So,=20
    > I've tried this and it still doesn't work:
    >=20
    > FileUtils.touch("file1", "file2")
    >=20
    > What does "t" stand for above?


    The "t" is the accesstime and modified time you want to set the file to.
    The FileUtils.touch sets the time on the specified file(s) to the
    current time (and creates the file if it doesn't exist). This works for
    me as expected on Windows.

    File.utime will set the time to any artibary time value. Assuming
    testfile.txt already exists the following works for me on Windows.

    a =3D Time.now - (24*60*60*3) # To get the time three days ago
    File.utime(a,a,"testfile.txt") # To set the accessed and modified time
    for the file to three days ago

    Hope this helps

    --Bill
    William (Bill) Froelich, Mar 6, 2007
    #10
  11. Peter Bailey

    Peter Bailey Guest

    Rob Biedenharn wrote:
    > On Mar 6, 2007, at 11:43 AM, Peter Bailey wrote:
    >>>
    >>>
    >>>

    >> FileUtils.touch("file1", "file2")
    >>
    >> What does "t" stand for above?

    >
    > it is set to Time.now, but you want to do something like:
    >
    > newfile = "#{oldfile}.txt"
    > File.open(oldfile) do |o|
    > File.open(newfile, 'w') do |n|
    > # while line = o.gets
    > # n << fixup(line)
    > # end
    > end
    > end
    > t = File.mtime(oldfile)
    > File.utime(t,t, newfile)
    >
    > Of course the inner loop is whatever you need it to be. The key here
    > is to just do the part of FileUtils.touch that you need which is the
    > call to File.utime on the new file. Since you just had to create it
    > (and fill it with something), you don't have to create it (the rescue
    > clause in touch).
    >
    > (I hadn't run across the File.utime before, but I should've expected
    > that from Perl's influence.)
    >
    > -Rob
    >
    > Rob Biedenharn http://agileconsultingllc.com
    >


    Thank you all, gentlemen.
    These lines are the ones that made it shine for me, Rob:

    t = File.mtime(oldfile)
    File,utime(t,t,newfile)

    because that's exactly what I need to do. I need to grab the time stamp
    for my postscript files and then assign those times to my simple text
    files derived from those .ps files.

    Thanks again!

    -Peter

    --
    Posted via http://www.ruby-forum.com/.
    Peter Bailey, Mar 6, 2007
    #11
  12. On 06.03.2007 17:43, Peter Bailey wrote:
    > Thanks, Brian! Well, no, "make" is meaningless in DOS or Windows,
    > believe me.


    This general statement is wrong. *You* might not have make on your
    Windows box - I and a lot others do. :)

    Kind regards

    robert
    Robert Klemme, Mar 6, 2007
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ladygrinningsoul

    Modifying a file's time stamp

    ladygrinningsoul, Dec 5, 2004, in forum: Perl
    Replies:
    1
    Views:
    542
    ladygrinningsoul
    Dec 6, 2004
  2. =?Utf-8?B?QmVydA==?=

    Date/Time stamp in SQL

    =?Utf-8?B?QmVydA==?=, Jan 30, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    21,412
    Wardeaux
    Jan 30, 2004
  3. Zean Smith

    Time stamp problem

    Zean Smith, Feb 7, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    335
    Zean Smith
    Feb 7, 2006
  4. srini
    Replies:
    9
    Views:
    11,503
    Beauregard T. Shagnasty
    Apr 14, 2004
  5. Picker Leon

    Add File Names with Time Stamp

    Picker Leon, Dec 3, 2003, in forum: Perl Misc
    Replies:
    12
    Views:
    277
    Ben Morrow
    Dec 4, 2003
Loading...

Share This Page