![]() |
Languages »
C# »
Date / Time
Beginner
License: The Code Project Open License (CPOL)
Append Date and Time to FileBy TimGradwellAppend date and time to a file for achival purposes |
C#, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
When I periodically backup my source files, I need a unique identifier so that I can keep all my backups organised. The attached program does precisely this by appending the date and time to the selected file. The date and time are written in the order of year, month, day, hours, minutes and seconds. This means that when sorting by name, the files are also sorted by their date/time.
The app takes the file passed as an argument to the program and runs the AppendDate() function on it.
static void AppendDate(String filePath)
{
FileInfo info = new FileInfo(filePath);
String folder = Path.GetDirectoryName(filePath);
String fileName = Path.GetFileNameWithoutExtension(filePath);
String extension = Path.GetExtension(filePath);
String newName = folder + "\\" + fileName + "_" + ISO_Date() + extension;
info.MoveTo(newName);
}
Append date grabs the date in an ISO-esque format from the ISO_Date function.
static String ISO_Date()
{
return DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss");
}
It's a console app, but that's not to say you can't run it through Windows Explorer. Simply copy the EXE into a memorable location (e.g. c:\program files\AppendDate), then "Open" the file you want to append the date to with the AppendDate.exe.
Typically, I will create a zip file using the "Send To" explorer context menu:
Then, append the date to the file using AppendDate.exe:
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 6 Oct 2008 Editor: Deeksha Shenoy |
Copyright 2008 by TimGradwell Everything else Copyright © CodeProject, 1999-2009 Web22 | Advertise on the Code Project |