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;
}