Re: How can I cause the datetime to be the name of the output file.....
- From: Michael Mair <Michael.Mair@xxxxxxxxxxxxxxx>
- Date: Sun, 05 Feb 2006 10:02:03 +0100
Keith Thompson wrote:
"Merlin" <mrb7494@xxxxxxxxx> writes:
I realize that I'm new to the group, but I'm hoping that someone might
be able to help me out. What I'm doing is using the GNU 7zip command
line utility to make a backup on my desktop. Essentially, I'm running
an executable that will create a folder, and drop a zipped up copy of
my data to that folder. There doesn't seem to be a way to get 7zip to
automatically create a file where the filename.zip would be a date/time
stamp (something like 020206143260.zip.
What I'd like to be able to do is allow 7.zip to create the file with
some arbitrary name, and then use the rename() function and the time.h
library to somehow grab the current date/time from the computer and
rename the file with the filename being the date/time stamp.
Would time.h be the right library to use? If so, does anyone have any
suggestions as to how a solution might be implemented? Any thoughts
would be greatly appreciated.
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
main()
{
chdir("C:\\Documents and Settings\\All Users\\Desktop\\");
mkdir("Backup");
chdir("C:\\Program Files\\Backup Utility\\");
system("7za.exe a -tzip \"C:\\Documents and Settings\\All
Users\\Desktop\\Backup\\Backup.zip\" \"C:\\Program
Files\\Data\\*.*\"");
chdir("C:\\");
}
A lot of this (<unistd.h>, 7zip, anything having to do with
directories) isn't covered by standard C, and so is off-topic, but the
core of your question is topical.
Use "int main(void)" rather than "main()".
Check for, and handle, errors on your function calls.
Add a "return 0;" at the end of the program. You might also do a
"return EXIT_FAILURE;" or "exit(EXIT_FAILURE);" if you detect an error
(this requires <stdlib.h>).
Yes, <time.h> is the header (not library) that you want to use to deal
with timestamps. Call time() to get a raw timestamp, type time_t,
then use either gmtime() or localtime() to convert the time_t to a "
struct tm", then use strftime() to generate a string in whatever
format you like.
If you can specify the file name when you invoke the command itself,
you can build a command string using sprintf(); making sure the string
into which you write the formatted is long enough is left as an
exercise.
If your standard library supports it, I suggest the use of snprintf()
instead of sprintf() to take care of unforeseen situations.
<OT>
If you care about being able to understand the file names after you've
generated them, you might use a format other than 020206143260.zip,
perhaps something like 2006-02-02-143260.zip; using MMMM-DD-YY also
ITYM: YYYY-MM-DD Sort of a rotational error...
has the advantage that it sorts nicely in a directory listing.
</OT>
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
.
- Follow-Ups:
- Re: How can I cause the datetime to be the name of the output file.....
- From: Keith Thompson
- Re: How can I cause the datetime to be the name of the output file.....
- References:
- How can I cause the datetime to be the name of the output file.....
- From: Merlin
- Re: How can I cause the datetime to be the name of the output file.....
- From: Keith Thompson
- How can I cause the datetime to be the name of the output file.....
- Prev by Date: Re: How can I cause the datetime to be the name of the output file.....
- Next by Date: Re: how to encrypt a C data and write a bin file and read a bin at run time and decrypt C data
- Previous by thread: Re: How can I cause the datetime to be the name of the output file.....
- Next by thread: Re: How can I cause the datetime to be the name of the output file.....
- Index(es):
Relevant Pages
|