[C#] C#将制定目录的文件名都转换成大写 →→→→→进入此内容的聊天室

来自 , 2019-10-10, 写在 C#, 查看 129 次.
URL http://www.code666.cn/view/bc5fcb00
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4.  
  5.  
  6. namespace RobvanderWoude
  7. {
  8.         class UpCase
  9.         {
  10.                 static int Main( string[] args )
  11.                 {
  12.                         string dir = string.Empty;
  13.                         string filespec = string.Empty;
  14.                         char[] trailingbackslash = "\\".ToCharArray( 0, 1 );
  15.                         char[] locaseletters = "abcdefghijklmnopqrstuvwxyz".ToCharArray( 0, 26 );
  16.                         bool verbose = false;
  17.  
  18.  
  19.                         #region Command Line Parsing
  20.  
  21.                         switch ( args.Length )
  22.                         {
  23.                                 case 0:
  24.                                         return WriteError( string.Empty );
  25.                                 case 1:
  26.                                         filespec = args[0].Trim( '"' );
  27.                                         break;
  28.                                 case 2:
  29.                                         filespec = args[0].Trim( '"' );
  30.                                         if ( args[1].Substring( 0, 2 ).ToUpper( ) == "/V" )
  31.                                         {
  32.                                                 verbose = true;
  33.                                         }
  34.                                         else
  35.                                         {
  36.                                                 return WriteError( "Invalid command line switch: " + args[1] );
  37.                                         }
  38.                                         break;
  39.                                 default:
  40.                                         return WriteError( string.Empty );
  41.                         }
  42.  
  43.                         if ( string.IsNullOrWhiteSpace( filespec ) || filespec == "/?" )
  44.                         {
  45.                                 return WriteError( string.Empty );
  46.                         }
  47.                         if ( filespec.IndexOfAny( "/?".ToCharArray( 0, 2 ) ) != -1 )
  48.                         {
  49.                                 return WriteError( "Invalid file specification: \"" + filespec + "\"" );
  50.                         }
  51.  
  52.                         #endregion Command Line Parsing
  53.  
  54.  
  55.                         try
  56.                         {
  57.                                 // Check if the directory exists
  58.                                 try
  59.                                 {
  60.                                         dir = Path.GetDirectoryName( filespec );
  61.                                         if ( string.IsNullOrWhiteSpace( dir ) )
  62.                                         {
  63.                                                 dir = Path.GetFullPath( "." );
  64.                                         }
  65.                                         if ( !Directory.Exists( dir ) )
  66.                                         {
  67.                                                 return WriteError( "Directory not found: \"" + dir + "\"" );
  68.                                         }
  69.                                         dir = dir.TrimEnd( trailingbackslash ) + "\\";
  70.                                 }
  71.                                 catch ( ArgumentException )
  72.                                 {
  73.                                         return WriteError( "Parent directory not found" );
  74.                                 }
  75.  
  76.                                 // Extract the FILE specification (removing the path)
  77.                                 string filenames = filespec.Substring( filespec.LastIndexOf( "\\" ) + 1 );
  78.                                 // Enumerate the files
  79.                                 string[] files = Directory.EnumerateFiles( dir, filenames ).ToArray<string>( );
  80.                                 int count = 0;
  81.                                 foreach ( string file in files )
  82.                                 {
  83.                                         if ( File.Exists( file ) )
  84.                                         {
  85.                                                 string filename = Path.GetFileName( file );
  86.                                                 if ( filename.IndexOfAny( locaseletters ) > -1 )
  87.                                                 {
  88.                                                         count++;
  89.                                                         string newfilename = dir + filename.ToUpperInvariant( );
  90.                                                         File.Move( file, newfilename );
  91.                                                 }
  92.                                         }
  93.                                 }
  94.                                 if ( verbose )
  95.                                 {
  96.                                         Console.WriteLine( "{0} matching file{1} renamed", ( count == 0 ? "No" : count.ToString( ) ), ( count == 1 ? string.Empty : "s" ) );
  97.                                 }
  98.                                 return count;
  99.                         }
  100.                         catch ( Exception e )
  101.                         {
  102.                                 return WriteError( e.Message );
  103.                         }
  104.                 }
  105.  
  106.                 public static int WriteError( Exception e )
  107.                 {
  108.                         return WriteError( e == null ? null : e.Message );
  109.                 }
  110.  
  111.                 public static int WriteError( string errorMessage )
  112.                 {
  113.                         /*
  114.                         UpCase.exe,  Version 1.02
  115.                         Rename specified files to all upper case
  116.  
  117.                         Usage:    UpCase.exe  filespec  [ /Verbose ]
  118.  
  119.                         Where:    filespec    is (are) the file(s) to be renamed (wildcards allowed)
  120.                                   /Verbose    displays the number of files renamed
  121.  
  122.                         Notes:    Use doublequotes if filespec contains spaces.
  123.                                   Return code (\"ErrorLevel\") equals the number of renamed files.
  124.                                   Switch may be abbreviated, e.g. /V instead of /Verbose.
  125.  
  126.                         Written by Rob van der Woude
  127.                         http://www.robvanderwoude.com
  128.                         */
  129.  
  130.                         if ( !string.IsNullOrWhiteSpace( errorMessage ) )
  131.                         {
  132.                                 Console.Error.WriteLine( );
  133.                                 Console.ForegroundColor = ConsoleColor.Red;
  134.                                 Console.Error.Write( "ERROR: " );
  135.                                 Console.ForegroundColor = ConsoleColor.White;
  136.                                 Console.Error.WriteLine( errorMessage );
  137.                                 Console.ResetColor( );
  138.                         }
  139.                         Console.Error.WriteLine( );
  140.                         Console.Error.WriteLine( "UpCase.exe,  Version 1.02" );
  141.                         Console.Error.WriteLine( "Rename specified files to all upper case" );
  142.                         Console.Error.WriteLine( );
  143.                         Console.Error.Write( "Usage:    " );
  144.                         Console.ForegroundColor = ConsoleColor.White;
  145.                         Console.Error.WriteLine( "UpCase.exe  filespec  [ /Verbose ]" );
  146.                         Console.ResetColor( );
  147.                         Console.Error.WriteLine( );
  148.                         Console.Error.Write( "Where:    " );
  149.                         Console.ForegroundColor = ConsoleColor.White;
  150.                         Console.Error.Write( "filespec" );
  151.                         Console.ResetColor( );
  152.                         Console.Error.WriteLine( "    is (are) the file(s) to be renamed (wildcards allowed)" );
  153.                         Console.ForegroundColor = ConsoleColor.White;
  154.                         Console.Error.Write( "          /V" );
  155.                         Console.ResetColor( );
  156.                         Console.Error.WriteLine( "erbose    displays the number of files renamed" );
  157.                         Console.Error.WriteLine( );
  158.                         Console.Error.WriteLine( "Note:     Use doublequotes if filespec contains spaces." );
  159.                         Console.Error.WriteLine( "          Return code (\"ErrorLevel\") equals the number of renamed files." );
  160.                         Console.Error.Write( "          Switch may be abbreviated, e.g. " );
  161.                         Console.ForegroundColor = ConsoleColor.White;
  162.                         Console.Error.Write( "/V" );
  163.                         Console.ResetColor( );
  164.                         Console.Error.Write( " instead of " );
  165.                         Console.ForegroundColor = ConsoleColor.White;
  166.                         Console.Error.Write( "/V" );
  167.                         Console.ResetColor( );
  168.                         Console.Error.WriteLine( "erbose." );
  169.                         Console.Error.WriteLine( );
  170.                         Console.Error.WriteLine( "Written by Rob van der Woude" );
  171.                         Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  172.                         return 0;
  173.                 }
  174.  
  175.         }
  176. }
  177. //csharp/7354

回复 "C#将制定目录的文件名都转换成大写"

这儿你可以回复上面这条便签

captcha