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