Click here to Skip to main content
Click here to Skip to main content

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

, 11 Jul 2005
Rate this:
Please Sign up or sign in to vote.
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.

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.

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.

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

About the Author

SathishVJ

France France
~/sathishvj

Comments and Discussions

 
GeneralMany Thanks PinmemberJaECH3-Oct-07 6:21 
GeneralGimme some sunglasses Pinmemberdogby13-Aug-07 15:48 
GeneralTime Difference PinmemberAbc Dimension28-May-07 23:21 
QuestionRead only files? PinmemberDuneAgent31-Jul-06 22:33 
GeneralCreation Date PinmemberJamsey1200125-Jul-06 2:26 
GeneralNice PinmemberMichael P Butler11-Jul-05 8:46 
GeneralI hate to be terribly negative BUT ..... Pinmemberfwsouthern11-Jul-05 8:01 
GeneralRe: I hate to be terribly negative BUT ..... PinmemberSathishVJ11-Jul-05 22:59 
GeneralRe: I hate to be terribly negative BUT ..... Pinmemberfwsouthern12-Jul-05 6:09 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 11 Jul 2005
Article Copyright 2005 by SathishVJ
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid