Click here to Skip to main content
15,888,803 members
Articles / Programming Languages / C#
Article

WinTouch - Change access date, creation date and modified date for files and directories

Rate me:
Please Sign up or sign in to vote.
4.80/5 (10 votes)
11 Jul 2005 96.6K   1.4K   33   9
Touches a file with specified date/time so that its access, creation or modified date is changed.

Sample Image - WinTouch.gif

Introduction

This tool is similar to the touch utility in UNIX. I wrote this tool while I was experimenting with some time based files and I needed to change the creation date of files to a past or previous date.

Usage

.NET 1.1 is required for using this tool. The usage can be obtained by typing "wintouch /?" on the command line.

Examples

wintouch -p *.txt -s -t "5/5/2005 4:14:15AM" -a -c -m 
wintouch -a
wintouch -a -m -c -t "5/5/2003 2:2:2AM" -d subdir

Code Walkthrough

First, we walk through the different input parameters and set appropriate local variables.

C#
static void Main(string[] args)
{
  WinTouch winTouch = new WinTouch();
  for(int i=0; i<args.Length; i++)
  {
    string s = args[i];
    switch (s.ToLower())
    {
        case "-s":
            winTouch.recurseSubDirs = true;
            break;
        case "-a":
            winTouch.changeAccessDate = true;
    ...

The core of this small program is the method TouchFilesInDir which accepts as parameters the directory within which files have to be 'touched' and the file name pattern.

For each file in the directory that matches the pattern, we change the access time, modified time or creation time as requested.

C#
string[] files = Directory.GetFiles(dir, filePattern);
foreach(string oneFile in files)
{
    string path = oneFile;
    try
    {
        if (this.changeAccessDate)
        {
            File.SetLastAccessTime(path, this.touchTime);
        }
        if (this.changeModifiedDate)
        {
            File.SetLastWriteTime(path, this.touchTime);
        }
    if (this.changeCreatedDate)
    {
        File.SetCreationTime(path, this.touchTime);
    }
        ...

We then do the same for the list of directories also. If the user has chosen to recurse through sub directories, then we recursively call TouchFilesInDir.

C#
if (this.recurseSubDirs)
{
    string[] allDirs = Directory.GetDirectories(dir);
    foreach(string oneDir in allDirs)
    {
        this.TouchFilesInDir(oneDir, filePattern);
    }
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
France France
~/sathishvj

Comments and Discussions

 
GeneralMany Thanks Pin
JaECH3-Oct-07 6:21
JaECH3-Oct-07 6:21 
GeneralGimme some sunglasses Pin
dogby13-Aug-07 15:48
dogby13-Aug-07 15:48 
GeneralTime Difference Pin
Member 417285128-May-07 23:21
Member 417285128-May-07 23:21 
QuestionRead only files? Pin
DuneAgent31-Jul-06 22:33
DuneAgent31-Jul-06 22:33 
GeneralCreation Date Pin
Jamsey25-Jul-06 2:26
Jamsey25-Jul-06 2:26 
GeneralNice Pin
Michael P Butler11-Jul-05 8:46
Michael P Butler11-Jul-05 8:46 
GeneralI hate to be terribly negative BUT ..... Pin
fwsouthern11-Jul-05 8:01
fwsouthern11-Jul-05 8:01 
This has the potential for abuse BUT it is superficial. For example, use this against a MS Word document created in the past. As to "windows", it will have the new date BUT as to Word, the internal header will still show the original creation date. If all you want is to change the external file date, for example, to make the file dates of a release identical and representative of the release date, then this has function. BUT consider the potential for abuse.
GeneralRe: I hate to be terribly negative BUT ..... Pin
SathishVJ11-Jul-05 22:59
SathishVJ11-Jul-05 22:59 
GeneralRe: I hate to be terribly negative BUT ..... Pin
fwsouthern12-Jul-05 6:09
fwsouthern12-Jul-05 6:09 

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.