欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#将制定目录文件名转换成大写的方法

程序员文章站 2023-11-06 21:05:34
本文实例讲述了c#将制定目录文件名转换成大写的方法。分享给大家供大家参考。具体如下: using system; using system.io; using...

本文实例讲述了c#将制定目录文件名转换成大写的方法。分享给大家供大家参考。具体如下:

using system;
using system.io;
using system.linq;
namespace robvanderwoude
{
 class upcase
 {
  static int main( string[] args )
  {
   string dir = string.empty;
   string filespec = string.empty;
   char[] trailingbackslash = "\\".tochararray( 0, 1 );
   char[] locaseletters = "abcdefghijklmnopqrstuvwxyz".tochararray( 0, 26 );
   bool verbose = false;
   #region command line parsing
   switch ( args.length )
   {
    case 0:
     return writeerror( string.empty );
    case 1:
     filespec = args[0].trim( '"' );
     break;
    case 2:
     filespec = args[0].trim( '"' );
     if ( args[1].substring( 0, 2 ).toupper( ) == "/v" )
     {
      verbose = true;
     }
     else
     {
      return writeerror( "invalid command line switch: " + args[1] );
     }
     break;
    default:
     return writeerror( string.empty );
   }
   if ( string.isnullorwhitespace( filespec ) || filespec == "/?" )
   {
    return writeerror( string.empty );
   }
   if ( filespec.indexofany( "/?".tochararray( 0, 2 ) ) != -1 )
   {
    return writeerror( "invalid file specification: \"" + filespec + "\"" );
   }
   #endregion command line parsing
   try
   {
    // check if the directory exists
    try
    {
     dir = path.getdirectoryname( filespec );
     if ( string.isnullorwhitespace( dir ) )
     {
      dir = path.getfullpath( "." );
     }
     if ( !directory.exists( dir ) )
     {
      return writeerror( "directory not found: \"" + dir + "\"" );
     }
     dir = dir.trimend( trailingbackslash ) + "\\";
    }
    catch ( argumentexception )
    {
     return writeerror( "parent directory not found" );
    }
    // extract the file specification (removing the path)
    string filenames = filespec.substring( filespec.lastindexof( "\\" ) + 1 );
    // enumerate the files
    string[] files = directory.enumeratefiles( dir, filenames ).toarray<string>( );
    int count = 0;
    foreach ( string file in files )
    {
     if ( file.exists( file ) )
     {
      string filename = path.getfilename( file );
      if ( filename.indexofany( locaseletters ) > -1 )
      {
       count++;
       string newfilename = dir + filename.toupperinvariant( );
       file.move( file, newfilename );
      }
     }
    }
    if ( verbose )
    {
     console.writeline( "{0} matching file{1} renamed", ( count == 0 ? "no" : count.tostring( ) ), ( count == 1 ? string.empty : "s" ) );
    }
    return count;
   }
   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 )
  {
   /*
   upcase.exe, version 1.02
   rename specified files to all upper case
   usage:  upcase.exe filespec [ /verbose ]
   where:  filespec  is (are) the file(s) to be renamed (wildcards allowed)
        /verbose  displays the number of files renamed
   notes:  use doublequotes if filespec contains spaces.
        return code (\"errorlevel\") equals the number of renamed files.
        switch may be abbreviated, e.g. /v instead of /verbose.
   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( "upcase.exe, version 1.02" );
   console.error.writeline( "rename specified files to all upper case" );
   console.error.writeline( );
   console.error.write( "usage:  " );
   console.foregroundcolor = consolecolor.white;
   console.error.writeline( "upcase.exe filespec [ /verbose ]" );
   console.resetcolor( );
   console.error.writeline( );
   console.error.write( "where:  " );
   console.foregroundcolor = consolecolor.white;
   console.error.write( "filespec" );
   console.resetcolor( );
   console.error.writeline( "  is (are) the file(s) to be renamed (wildcards allowed)" );
   console.foregroundcolor = consolecolor.white;
   console.error.write( "     /v" );
   console.resetcolor( );
   console.error.writeline( "erbose  displays the number of files renamed" );
   console.error.writeline( );
   console.error.writeline( "note:   use doublequotes if filespec contains spaces." );
   console.error.writeline( "     return code (\"errorlevel\") equals the number of renamed files." );
   console.error.write( "     switch may be abbreviated, e.g. " );
   console.foregroundcolor = consolecolor.white;
   console.error.write( "/v" );
   console.resetcolor( );
   console.error.write( " instead of " );
   console.foregroundcolor = consolecolor.white;
   console.error.write( "/v" );
   console.resetcolor( );
   console.error.writeline( "erbose." );
   console.error.writeline( );
   console.error.writeline( "written by rob van der woude" );
   console.error.writeline( "http://www.baidu.com" );
   return 0;
  }
 }
}

希望本文所述对大家的c#程序设计有所帮助。