C#指定したファイルの最後の変更時間のタイムスタンプのコードを変更する

6705 ワード

工程過程の比較的に良いいくつかの内容の断片をバックアップして、以下の資料はC#が指定したファイルの最後の修正時間のタイムスタンプの内容を修正することについてです.
using System;
using System.IO;
using System.Linq;

namespace RobvanderWoude
{
	class CloneDate
	{
		static int Main( string[] args )
		{
			bool debug = false;
			switch ( args.Length )
			{
				case 0:
					return WriteError( string.Empty );
				case 2:
					break;
				case 3:
					if ( args[2].Substring( 0, 2 ).ToUpper( ) == "/D" )
					{
						debug = true;
					}
					else
					{
						return WriteError( "Invalid command line argument(s)" );
					}
					break;
				default:
					return WriteError( "Invalid number of command line arguments" );
			}

			try
			{
				string sourcefile = args[0];
				if ( string.IsNullOrWhiteSpace( sourcefile ) )
				{
					return WriteError( "Invalid source file specification" );
				}
				try
				{
					sourcefile = Path.GetFullPath( sourcefile ).Trim( '"' );
				}
				catch ( ArgumentException )
				{
					return WriteError( "No wildcards allowed in source file" );
				}
				if ( !File.Exists( sourcefile ) )
				{
					return WriteError( "File not found: "" + sourcefile + """ );
				}

				string targetspec = args[1];
				if ( string.IsNullOrWhiteSpace( targetspec ) )
				{
					return WriteError( "Invalid target file specification" );
				}
				string targetdir = string.Empty;
				try
				{
					targetdir = Path.GetDirectoryName( targetspec );
					if ( string.IsNullOrWhiteSpace( targetdir ) )
					{
						targetdir = Path.GetFullPath( "." );
					}
				}
				catch ( ArgumentException )
				{
					return WriteError( "Target folder not found: "" + targetspec + """ );
				}
				string targetfilespec = string.Empty;
				if ( targetspec.IndexOf( "\" ) == -1 )
				{
					targetfilespec = targetspec;
				}
				else
				{
					targetfilespec = targetspec.Substring( targetspec.LastIndexOf( "\" ) + 1 );
				}
				string[] targetfiles = Directory.EnumerateFiles( targetdir, targetfilespec ).ToArray( );
				DateTime timestamp = File.GetLastWriteTime( sourcefile );
				int count = 0;
				foreach ( string targetfile in targetfiles )
				{
					if ( targetfile.ToUpper( ) != sourcefile.ToUpper( ) )
					{
						count++;
						if ( debug )
						{
							Console.WriteLine( "File   : {0}", targetfile );
							Console.WriteLine( "Before : {0}", File.GetLastWriteTime( targetfile ) );
						}
						File.SetLastWriteTime( targetfile, timestamp );
						if ( debug )
						{
							Console.WriteLine( "After  : {0}", File.GetLastWriteTime( targetfile ) );
							Console.WriteLine( );
						}
					}
				}
				
				if ( debug )
				{
					Console.WriteLine( "{0} matching file{1}", count, ( count == 1 ? "" : "s" ) );
				}

				if ( count == 0 )
				{
					return WriteError( "No matching target files: "" + targetspec + """ );
				}

				return 0;
			}
			catch ( Exception e )
			{
				return WriteError( e.Message );
			}
		}

		public static int WriteError( Exception e )
		{
			return WriteError( e == null ? null : e.Message );
		}

		public static int WriteError( string errorMessage )
		{
			CloneDate.exe,  Version 0.50 BETA
			Modify the LastModified date (timestamp) of the target file(s) to
			match the specified source file's timestamp

			Usage:    CloneDate.exe  sourcefile  targetfiles  [ /Debug ]

			Where:    sourcefile     is the file whose timestamp is to be cloned
			          targetfiles    are the files whose timestamp are to be modified
			          /Debug         displays file name and timestamps before and
			                         after modification for each matching file

			Example:  CloneDate.exe C:boot.ini C:test.log
			          will change C:test.log's timestamp to match C:boot.ini's

			Notes:    Target filespec may include sourcefile (sourcefile will be skipped).
			          Always be careful when using wildcards; they may also return matching
			          "short" (8.3) file names (for backwards compatibility with FAT16).

			Written by Rob van der Woude

			if ( !string.IsNullOrWhiteSpace( errorMessage ) )
			{
				Console.Error.WriteLine( );
				Console.ForegroundColor = ConsoleColor.Red;
				Console.Error.Write( "ERROR: " );
				Console.ForegroundColor = ConsoleColor.White;
				Console.Error.WriteLine( errorMessage );
				Console.ResetColor( );
			}
			Console.Error.WriteLine( );
			Console.Error.WriteLine( "CloneDate.exe,  Version 0.50 BETA" );
			Console.Error.WriteLine( "Modify the LastModified date (timestamp) of the targetnfile(s) to match the specified source file's timestamp" );
			Console.Error.WriteLine( );
			Console.Error.Write( "Usage:    " );
			Console.ForegroundColor = ConsoleColor.White;
			Console.Error.WriteLine( "CloneDate.exe  sourcefile  targetfiles" );
			Console.ResetColor( );
			Console.Error.WriteLine( );
			Console.Error.Write( "Where:    " );
			Console.ForegroundColor = ConsoleColor.White;
			Console.Error.Write( "sourcefile" );
			Console.ResetColor( );
			Console.Error.WriteLine( "     is the file whose timestamp is to be cloned" );
			Console.ForegroundColor = ConsoleColor.White;
			Console.Error.Write( "          targetfiles" );
			Console.ResetColor( );
			Console.ForegroundColor = ConsoleColor.White;
			Console.Error.Write( "          /D" );
			Console.ResetColor( );
			Console.Error.WriteLine( "ebug         displays file name and timestamps before andn                         after modification for each matching file" );
			Console.Error.WriteLine( );
			Console.Error.Write( "Example:  " );
			Console.ForegroundColor = ConsoleColor.White;
			Console.Error.WriteLine( "CloneDate.exe C:\boot.ini C:\test.log" );
			Console.ResetColor( );
			Console.Error.WriteLine( "          will change C:\test.log's timestamp to match C:\boot.ini's" );
			Console.Error.WriteLine( );
			Console.Error.WriteLine( "Notes:    Target filespec may include sourcefile (sourcefile will be skipped)." );
			Console.Error.WriteLine( "          Always be careful when using wildcards; they may also return matchingn          "short" (8.3) file names (for backwards compatibility with FAT16)." );
			Console.Error.WriteLine( );
			Console.Error.WriteLine( "Written by Rob van der Woude" );
			return 1;
		}

	}
}