rename according to time

Discussion in 'C Programming' started by byte, Jan 28, 2008.

  1. byte

    byte Guest

    hi C programmers,
    im a c newbie.
    i would like to rename an existing file(xxx.7z) according to time()
    and
    localtime() and get the outcome like 0127172718.7z
    (month:day:hour:min:seconds with suffix name)
    i am not sure how to do this in c.
    please help.
    p.s:i hope run this code under xp
    byte, Jan 28, 2008
    #1
    1. Advertising

  2. byte wrote:
    > hi C programmers,
    > im a c newbie.
    > i would like to rename an existing file(xxx.7z) according to time()
    > and
    > localtime() and get the outcome like 0127172718.7z
    > (month:day:hour:min:seconds with suffix name)
    > i am not sure how to do this in c.
    > please help.
    > p.s:i hope run this code under xp


    Your operating system doesn't matter, it depends how you compile it.
    Using Visual Studio, using lcc-win, using mingw, ...

    Take a look at the rename() function included in <stdio.h>.

    - Jensen
    Jensen Somers, Jan 28, 2008
    #2
    1. Advertising

  3. byte

    Mark Bluemel Guest

    byte wrote:
    > hi C programmers,
    > im a c newbie.
    > i would like to rename an existing file(xxx.7z) according to time()
    > and
    > localtime() and get the outcome like 0127172718.7z
    > (month:day:hour:min:seconds with suffix name)
    > i am not sure how to do this in c.


    I'm inclined to believe you can't do this in standard C - you'll need to
    use operating-system specific functionality.

    > please help.
    > p.s:i hope run this code under xp


    I suggest you ask in a Windows programming group for the OS-specifics.

    You could also look at msdn -
    http://msdn2.microsoft.com/en-us/library/aa365239(VS.85).aspx looks
    relevant, but I'm not a Windows programmer so can't comment much.
    Mark Bluemel, Jan 28, 2008
    #3
  4. byte

    Army1987 Guest

    byte wrote:

    > hi C programmers,
    > im a c newbie.
    > i would like to rename an existing file(xxx.7z) according to time()
    > and
    > localtime() and get the outcome like 0127172718.7z
    > (month:day:hour:min:seconds with suffix name)
    > i am not sure how to do this in c.

    Look up strftime() and rename().

    --
    Army1987 (Replace "NOSPAM" with "email")
    Army1987, Jan 28, 2008
    #4
  5. Mark Bluemel said:

    > byte wrote:
    >> hi C programmers,
    >> im a c newbie.
    >> i would like to rename an existing file(xxx.7z) according to time()
    >> and
    >> localtime() and get the outcome like 0127172718.7z
    >> (month:day:hour:min:seconds with suffix name)
    >> i am not sure how to do this in c.

    >
    > I'm inclined to believe you can't do this in standard C - you'll need to
    > use operating-system specific functionality.


    What's wrong with strftime, Mark?

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Jan 28, 2008
    #5
  6. byte

    erfan Guest

    On 1ÔÂ28ÈÕ, ÏÂÎç8ʱ57·Ö, Jensen Somers <> wrote:
    > byte wrote:
    > > hi C programmers,
    > > im a c newbie.
    > > i would like to rename an existing file(xxx.7z) according to time()
    > > and
    > > localtime() and get the outcome like 0127172718.7z
    > > (month:day:hour:min:seconds with suffix name)
    > > i am not sure how to do this in c.
    > > please help.
    > > p.s:i hope run this code under xp

    >
    > Your operating system doesn't matter, it depends how you compile it.
    > Using Visual Studio, using lcc-win, using mingw, ...
    >
    > Take a look at the rename() function included in <stdio.h>.
    >
    > - Jensen

    #include<time.h>
    #include<stdio.h>
    #include<stdlib.h>
    srand();
    rename(*old name,rand(time(null)));
    erfan, Jan 28, 2008
    #6
  7. byte

    Mark Bluemel Guest

    Mark Bluemel wrote:
    > byte wrote:
    >> hi C programmers,
    >> im a c newbie.
    >> i would like to rename an existing file(xxx.7z) according to time()
    >> and
    >> localtime() and get the outcome like 0127172718.7z
    >> (month:day:hour:min:seconds with suffix name)
    >> i am not sure how to do this in c.

    >
    > I'm inclined to believe you can't do this in standard C - you'll need to
    > use operating-system specific functionality.


    Doh. I need to read the up-to-date standard more carefully. I'm still
    inclined to think K&R (classic).
    Mark Bluemel, Jan 28, 2008
    #7
  8. byte

    Mark Bluemel Guest

    Richard Heathfield wrote:
    > Mark Bluemel said:
    >
    >> byte wrote:
    >>> hi C programmers,
    >>> im a c newbie.
    >>> i would like to rename an existing file(xxx.7z) according to time()
    >>> and
    >>> localtime() and get the outcome like 0127172718.7z
    >>> (month:day:hour:min:seconds with suffix name)
    >>> i am not sure how to do this in c.

    >> I'm inclined to believe you can't do this in standard C - you'll need to
    >> use operating-system specific functionality.

    >
    > What's wrong with strftime, Mark?


    As my other followup indicates, I learnt C from the original K&R book...

    Add to that my inclination to work on Unix/Linux pretty much
    exclusively, and to use (consciously) a lot of Posix functionality, I
    have to confess to sometimes not realising how much is now part of the
    standard.

    Sorry!
    Mark Bluemel, Jan 28, 2008
    #8
  9. byte

    Guest

    On Jan 28, 3:30 pm, erfan <> wrote:
    > On 1ÔÂ28ÈÕ, ÏÂÎç8ʱ57·Ö, Jensen Somers <> wrote:
    >
    > > byte wrote:
    > > > hi C programmers,
    > > > im a c newbie.
    > > > i would like to rename an existing file(xxx.7z) according to time()
    > > > and
    > > > localtime() and get the outcome like 0127172718.7z
    > > > (month:day:hour:min:seconds with suffix name)
    > > > i am not sure how to do this in c.
    > > > please help.
    > > > p.s:i hope run this code under xp

    >
    > > Your operating system doesn't matter, it depends how you compile it.
    > > Using Visual Studio, using lcc-win, using mingw, ...

    >
    > > Take a look at the rename() function included in <stdio.h>.

    >
    > > - Jensen

    >
    > #include<time.h>
    > #include<stdio.h>
    > #include<stdlib.h>
    > srand();
    > rename(*old name,rand(time(null)));

    What is that code supposed to do?
    It's not a valid C snip.
    , Jan 28, 2008
    #9
  10. wrote:
    > On Jan 28, 3:30 pm, erfan <> wrote:
    >> On 1ÔÂ28ÈÕ, ÏÂÎç8ʱ57·Ö, Jensen Somers <> wrote:
    >>
    >>> byte wrote:
    >>>> hi C programmers,
    >>>> im a c newbie.
    >>>> i would like to rename an existing file(xxx.7z) according to time()
    >>>> and
    >>>> localtime() and get the outcome like 0127172718.7z
    >>>> (month:day:hour:min:seconds with suffix name)
    >>>> i am not sure how to do this in c.
    >>>> please help.
    >>>> p.s:i hope run this code under xp
    >>> Your operating system doesn't matter, it depends how you compile it.
    >>> Using Visual Studio, using lcc-win, using mingw, ...
    >>> Take a look at the rename() function included in <stdio.h>.
    >>> - Jensen

    >> #include<time.h>
    >> #include<stdio.h>
    >> #include<stdlib.h>
    >> srand();
    >> rename(*old name,rand(time(null)));

    > What is that code supposed to do?
    > It's not a valid C snip.


    Does it have to be?

    It's just a basic illustration of what should be done. The topic starter
    should be able to figure his steps out from this code.

    - Jensen
    Jensen Somers, Jan 28, 2008
    #10
  11. Jensen Somers wrote:
    > wrote:
    >> On Jan 28, 3:30 pm, erfan <> wrote:
    >>> On 1ÔÂ28ÈÕ, ÏÂÎç8ʱ57·Ö, Jensen Somers <>
    >>> wrote:
    >>>> byte wrote:
    >>>>> hi C programmers,
    >>>>> im a c newbie.
    >>>>> i would like to rename an existing file(xxx.7z) according to
    >>>>> time() and
    >>>>> localtime() and get the outcome like 0127172718.7z
    >>>>> (month:day:hour:min:seconds with suffix name)
    >>>>> i am not sure how to do this in c.
    >>>>> please help.
    >>>>> p.s:i hope run this code under xp
    >>>> Your operating system doesn't matter, it depends how you compile
    >>>> it. Using Visual Studio, using lcc-win, using mingw, ...
    >>>> Take a look at the rename() function included in <stdio.h>.
    >>>> - Jensen
    >>> #include<time.h>
    >>> #include<stdio.h>
    >>> #include<stdlib.h>
    >>> srand();
    >>> rename(*old name,rand(time(null)));

    >> What is that code supposed to do?
    >> It's not a valid C snip.

    >
    > Does it have to be?
    >
    > It's just a basic illustration of what should be done. The topic
    > starter should be able to figure his steps out from this code.

    Well, rand doesn't take an argument (srand does though) and returns an int,
    rename takes 2 char *. So your code isn't even good pseudo code.

    Furthermore a filename generated with rand doesn't have anything to do with
    the OP's request, who wanted filenames containg date and time, rather than a
    pseudo random number, so vippstar's "code" wasn't even close.

    Bye, Jojo
    Joachim Schmitz, Jan 28, 2008
    #11
  12. Joachim Schmitz wrote:
    > Jensen Somers wrote:
    >> wrote:
    >>> On Jan 28, 3:30 pm, erfan <> wrote:
    >>>> #include<time.h>
    >>>> #include<stdio.h>
    >>>> #include<stdlib.h>
    >>>> srand();
    >>>> rename(*old name,rand(time(null)));
    >>> What is that code supposed to do?
    >>> It's not a valid C snip.

    >> Does it have to be?
    >>
    >> It's just a basic illustration of what should be done. The topic
    >> starter should be able to figure his steps out from this code.

    > Well, rand doesn't take an argument (srand does though) and returns an int,
    > rename takes 2 char *. So your code isn't even good pseudo code.
    >
    > Furthermore a filename generated with rand doesn't have anything to do with
    > the OP's request, who wanted filenames containg date and time, rather than a
    > pseudo random number, so vippstar's "code" wasn't even close.


    It was erfan, not vippstar, who wrote it.
    Philip Potter, Jan 28, 2008
    #12
  13. Philip Potter wrote:
    > Joachim Schmitz wrote:
    >> Jensen Somers wrote:
    >>> wrote:
    >>>> On Jan 28, 3:30 pm, erfan <> wrote:
    >>>>> #include<time.h>
    >>>>> #include<stdio.h>
    >>>>> #include<stdlib.h>
    >>>>> srand();
    >>>>> rename(*old name,rand(time(null)));
    >>>> What is that code supposed to do?
    >>>> It's not a valid C snip.
    >>> Does it have to be?
    >>>
    >>> It's just a basic illustration of what should be done. The topic
    >>> starter should be able to figure his steps out from this code.

    >> Well, rand doesn't take an argument (srand does though) and returns
    >> an int, rename takes 2 char *. So your code isn't even good pseudo
    >> code. Furthermore a filename generated with rand doesn't have anything to
    >> do with the OP's request, who wanted filenames containg date and
    >> time, rather than a pseudo random number, so vippstar's "code"
    >> wasn't even close.

    >
    > It was erfan, not vippstar, who wrote it.

    Oops, sorry!
    Joachim Schmitz, Jan 28, 2008
    #13
  14. byte

    Bill Reid Guest

    Richard Heathfield <> wrote in message
    news:...
    > Mark Bluemel said:
    >
    > > byte wrote:
    > >> hi C programmers,
    > >> im a c newbie.
    > >> i would like to rename an existing file(xxx.7z) according to time()
    > >> and
    > >> localtime() and get the outcome like 0127172718.7z
    > >> (month:day:hour:min:seconds with suffix name)
    > >> i am not sure how to do this in c.

    > >
    > > I'm inclined to believe you can't do this in standard C - you'll need to
    > > use operating-system specific functionality.

    >
    > What's wrong with strftime, Mark?


    Nothing, except by the time you plow through all the billions of
    cryptic conversion specifiers to figure out how to format the strftime()
    string, you could have just used sprintf() to print your own string from
    the tm structure about seven times over...

    But in any event, as is now very clear, this is one of the rare
    things that can actually be accomplished in "standard" C...

    ---
    William Ernest Reid
    Bill Reid, Jan 28, 2008
    #14
  15. byte

    Geoff Guest

    On Mon, 28 Jan 2008 04:48:45 -0800 (PST), byte
    <> wrote:

    >hi C programmers,
    >im a c newbie.
    >i would like to rename an existing file(xxx.7z) according to time()
    >and
    >localtime() and get the outcome like 0127172718.7z
    >(month:day:hour:min:seconds with suffix name)
    >i am not sure how to do this in c.
    >please help.
    >p.s:i hope run this code under xp


    #include <time.h>
    #include <stdio.h>
    #include <stdlib.h>

    // stuff a string with today's date
    // adding hours, mins, secs left as exercise for student
    void make_time_string (void)
    {
    time_t long_time;
    struct tm *ltime;
    char string[20];

    time(&long_time);
    ltime = localtime(&long_time);

    sprintf(string, "%02i%02i%02i",
    ltime->tm_year + 1900,
    ltime->tm_mon + 1,
    ltime->tm_mday);
    }

    Compiles OK in VC++ 6.0, functionality not tested.
    Geoff, Jan 28, 2008
    #15
  16. byte

    Guest

    On Jan 28, 6:48 am, byte <> wrote:
    > hi C programmers,
    > im a c newbie.
    > i would like to rename an existing file(xxx.7z) according to time()
    > and
    > localtime() and get the outcome like 0127172718.7z
    > (month:day:hour:min:seconds with suffix name)
    > i am not sure how to do this in c.
    > please help.
    > p.s:i hope run this code under xp


    You mean something like this:

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    /
    **********************************************************************/
    /* Name:
    */
    /* mk_timestamped_filename() - Return a timestamped filename.
    */
    /*
    */
    /* Synopsis:
    */
    /* #include "dirtools.h"
    */
    /* int
    mk_timestamped_filename( */
    /* char *dest,
    */
    /* char *filename,
    */
    /* char *extension
    */
    /* );
    */
    /*
    */
    /* Description:
    */
    /* The mk_timestamped_filename() function uses the filename and
    */
    /* extension arguments to construct a filename with a date/time
    */
    /* stamp as a component of the filename. E.G.: If the filename
    */
    /* is "ABC" and the extension is "DEF" the resulting filename
    */
    /* would be "ABC.YYYYMMDDMMSS.DEF" where YYYY is the year, MM is
    */
    /* the month, DD is the day of the month, MM is the minute, and
    */
    /* SS is the second.
    */
    /*
    */
    /* Arguments:
    */
    /* char *dest - The area memory where the timestamped file
    */
    /* name will be constructed. Be sure to allow
    */
    /* enough space to build the full filename!
    */
    /* char *filename - The leading component of filename.
    */
    /* char *extension - An optional extension that will follow the
    */
    /* the timestamp. If NULL or an empty string
    */
    /* is used this component of the file name will
    */
    /* be omitted.
    */
    /*
    */
    /* Return Value:
    */
    /* The address of the dest argument.
    */
    /*
    */
    /* See Also:
    */
    /* uniqname().
    */
    /*
    */
    /* Example:
    */
    /* #include <stdio.h>
    */
    /* #include "dirtools.h"
    */
    /*
    */
    /* int
    */
    /* main( int argc, char **argv )
    */
    /*
    { */
    /* char dest[FILENAME_MAX+1];
    */
    /* char *extension = argc < 3 ? "" : argv[2];
    */
    /* char *filename = argc < 2 ? "fubar" : argv[1];
    */
    /*
    */
    /*
    puts( */
    /*
    mk_timestamped_filename( */
    /* dest,
    */
    /* filename,
    */
    /* extension
    */
    /* )
    */
    /* );
    */
    /* return 0;
    */
    /* }
    */
    /*
    */
    /
    **********************************************************************/

    char *
    mk_timestamped_filename( char *dest, char *filename, char *extension )
    {
    time_t tv;
    struct tm tm;
    char wrkbuf[FILENAME_MAX + 1] = "";

    /
    ******************************************************************/
    /* Build the time related component of the file.
    */
    /
    ******************************************************************/

    tv = time( NULL );
    tm = *localtime( &tv );
    strftime( wrkbuf, sizeof(wrkbuf), "%Y%m%d%H%M%S", &tm );

    /
    ******************************************************************/
    /* Use sprintf() to build the full filename with or without the
    */
    /* extension.
    */
    /
    ******************************************************************/

    sprintf(
    dest,
    extension == NULL || *extension == 0 ? "%s.%s" : "%s.%s.%s",
    filename,
    wrkbuf,
    extension
    );

    return dest;
    }
    , Jan 30, 2008
    #16
    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. Tan
    Replies:
    1
    Views:
    305
    Sean M
    Aug 2, 2005
  2. TheTravellingSalesman

    Loop according to time

    TheTravellingSalesman, Dec 3, 2007, in forum: Java
    Replies:
    2
    Views:
    315
    TheTravellingSalesman
    Dec 3, 2007
  3. TheTravellingSalesman

    Loop according to time

    TheTravellingSalesman, Dec 3, 2007, in forum: Java
    Replies:
    2
    Views:
    316
  4. J.H.Kim

    time adjustment code according to time zone

    J.H.Kim, Oct 24, 2011, in forum: C Programming
    Replies:
    3
    Views:
    456
    Joel C. Salomon
    Oct 24, 2011
  5. David Joseph Bonnici
    Replies:
    1
    Views:
    204
    Gunnar Hjalmarsson
    May 15, 2005
Loading...

Share This Page