====== touch ======
A small C# utility to modify a file's timestamp. Requires a filename and timestamp in the form //YYYYMMDDHHMMSS//; e.g. to update the timestamp of ''myfile,txt'' to 13:30:29 on 10-Mar-2018, use:
touch myfile.txt 20180310133029
===== Implementation =====
Visual Studio solution available [[https://github.com/richardswinbank/touch|on GitHub]]. Really just this inside your main program class:
static void Main(string[] args)
{
try
{
if (args.Length != 2)
throw new TouchException("touch usage: touch ");
var filename = args[0];
if (!File.Exists(filename))
throw new TouchException("touch: file \"" + filename + "\" not found");
DateTime mt = ParseTimestamp(args[1]);
Console.WriteLine("Set last modified time of \"" + filename + "\" to " + mt.ToString("dd MMM yyyy HH:mm:ss") + "? (Y to apply)");
if (Console.ReadKey(true).Key == ConsoleKey.Y)
{
File.SetLastWriteTime(filename, mt);
Console.WriteLine("Timestamp updated");
return;
}
Console.WriteLine("No changes made");
}
catch (TouchException e)
{
Console.WriteLine(e.Message);
Environment.Exit(1);
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
Environment.Exit(2);
}
}
private static DateTime ParseTimestamp(string ts)
{
return new DateTime(
ParseInt(ts, 0, 4)
, ParseInt(ts, 4, 2)
, ParseInt(ts, 6, 2)
, ParseInt(ts, 8, 2)
, ParseInt(ts, 10, 2)
, ParseInt(ts, 12, 2)
);
}
private static int ParseInt(string ts, int off, int len)
{
try
{
return int.Parse(ts.Substring(off, len));
}
catch (FormatException)
{
InvalidTimestamp(ts);
}
catch (ArgumentOutOfRangeException)
{
InvalidTimestamp(ts);
}
return -1; // never reached
}
private static void InvalidTimestamp(string ts)
{
throw new TouchException("touch: invalid timestamp: " + ts);
}
private class TouchException : Exception
{
public TouchException(string message) : base(message)
{
}
}
{{tag> }}