Click here to Skip to main content
15,446,336 members
Articles / Programming Languages / C#
Posted 6 Oct 2008

Tagged as


28 bookmarked

Append Date and Time to File

Rate me:
Please Sign up or sign in to vote.
4.16/5 (15 votes)
6 Oct 2008CPOL1 min read
Append date and time to a file for achival purposes


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 Code

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;

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");

Using the Code

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:



  • 6th October 2008, 22:15 - Initial submission
  • 6th October 2008, 23:00 - Added code snippets to article
  • 7th October 2008, 09:30 - Fixed typo


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

GeneralGood code example. Pin
Donsw26-Jan-09 11:33
MemberDonsw26-Jan-09 11:33 
GeneralVery useful Compatibility with other Operations Systems Pin
heia15-Oct-08 20:04
Memberheia15-Oct-08 20:04 
GeneralVery Useful Application Pin
Jason Barry14-Oct-08 2:10
professionalJason Barry14-Oct-08 2:10 
This is a very simple, but useful application. It got my 5.
GeneralRe: Very Useful Application Pin
TimGradwell14-Oct-08 3:01
MemberTimGradwell14-Oct-08 3:01 
Generalthis check and protection was useful for my use case Pin
xoox7-Oct-08 2:41
Memberxoox7-Oct-08 2:41 
GeneralNice idea Pin
Dinesh Mani7-Oct-08 2:01
MemberDinesh Mani7-Oct-08 2:01 
GeneralRe: Nice idea Pin
TimGradwell8-Oct-08 2:21
MemberTimGradwell8-Oct-08 2:21 
GeneralUse HH to get 24-hour time Pin
PIEBALDconsult6-Oct-08 12:42
professionalPIEBALDconsult6-Oct-08 12:42 
GeneralRe: Use HH to get 24-hour time Pin
TimGradwell6-Oct-08 22:35
MemberTimGradwell6-Oct-08 22:35 
GeneralRe: Use HH to get 24-hour time Pin
edobrzel8-Oct-08 6:32
Memberedobrzel8-Oct-08 6:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.