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]; // Check if a source file was specified if ( string.IsNullOrWhiteSpace( sourcefile ) ) { return WriteError( "Invalid source file specification" ); } // Check if the source file name is valid, and make sure to use its full path try { sourcefile = Path.GetFullPath( sourcefile ).Trim( '"' ); } catch ( ArgumentException ) { return WriteError( "No wildcards allowed in source file" ); } // Check if the source file exists if ( !File.Exists( sourcefile ) ) { return WriteError( "File not found: \"" + sourcefile + "\"" ); } string targetspec = args[1]; if ( string.IsNullOrWhiteSpace( targetspec ) ) { return WriteError( "Invalid target file specification" ); } // Check if the target directory exists 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 + "\"" ); } // Extract the FILE specification (removing the path) string targetfilespec = string.Empty; if ( targetspec.IndexOf( "\\" ) == -1 ) { targetfilespec = targetspec; } else { targetfilespec = targetspec.Substring( targetspec.LastIndexOf( "\\" ) + 1 ); } string[] targetfiles = Directory.EnumerateFiles( targetdir, targetfilespec ).ToArray<string>( ); 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 (single filespec, wildcards * and ? are allowed) /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 http://www.robvanderwoude.com */ 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 target\nfile(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.Error.WriteLine( " are the files whose timestamp are to be modified\n (single filespec, wildcards * and ? are allowed)" ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( " /D" ); Console.ResetColor( ); Console.Error.WriteLine( "ebug displays file name and timestamps before and\n 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 matching\n \"short\" (8.3) file names (for backwards compatibility with FAT16)." ); Console.Error.WriteLine( ); Console.Error.WriteLine( "Written by Rob van der Woude" ); Console.Error.WriteLine( "http://www.robvanderwoude.com" ); return 1; } } }