Click here to Skip to main content
6,629,377 members and growing! (22,036 online)
Email Password   helpLost your password?
Languages » C# » Date / Time     Beginner License: The Code Project Open License (CPOL)

Append Date and Time to File

By TimGradwell

Append date and time to a file for achival purposes
C#, Dev
Posted:6 Oct 2008
Views:9,026
Bookmarked:22 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
15 votes for this article.
Popularity: 4.58 Rating: 3.89 out of 5
1 vote, 6.7%
1
1 vote, 6.7%
2
3 votes, 20.0%
3
5 votes, 33.3%
4
5 votes, 33.3%
5
result.JPG

Introduction

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

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:

SendToCompressed.JPG

Then, append the date to the file using AppendDate.exe:

usage.JPG

History

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

License

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

About the Author

TimGradwell


Member

Location: United Kingdom United Kingdom

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 10 of 10 (Total in Forum: 10) (Refresh)FirstPrevNext
GeneralGood code example. PinmemberDonsw12:33 26 Jan '09  
GeneralVery useful Compatibility with other Operations Systems Pinmemberheia21:04 15 Oct '08  
GeneralVery Useful Application PinmemberJason Barry3:10 14 Oct '08  
GeneralRe: Very Useful Application PinmemberTimGradwell4:01 14 Oct '08  
Generalthis check and protection was useful for my use case Pinmemberxoox3:41 7 Oct '08  
GeneralNice idea PinmemberDinesh Mani3:01 7 Oct '08  
GeneralRe: Nice idea PinmemberTimGradwell3:21 8 Oct '08  
GeneralUse HH to get 24-hour time PinmemberPIEBALDconsult13:42 6 Oct '08  
GeneralRe: Use HH to get 24-hour time PinmemberTimGradwell23:35 6 Oct '08  
GeneralRe: Use HH to get 24-hour time Pinmemberedobrzel7:32 8 Oct '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin 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