SoFunction
Updated on 2025-03-08

C# How to convert directory file names to capitalization

This article illustrates how C# converts directory file names into capitalization. Share it for your reference. The details are as follows:

using System;
using ;
using ;
namespace RobvanderWoude
{
 class UpCase
 {
  static int Main( string[] args )
  {
   string dir = ;
   string filespec = ;
   char[] trailingbackslash = "\\".ToCharArray( 0, 1 );
   char[] locaseletters = "abcdefghijklmnopqrstuvwxyz".ToCharArray( 0, 26 );
   bool verbose = false;
   #region Command Line Parsing
   switch (  )
   {
    case 0:
     return WriteError(  );
    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(  );
   }
   if ( ( filespec ) || filespec == "/?" )
   {
    return WriteError(  );
   }
   if ( ( "/?".ToCharArray( 0, 2 ) ) != -1 )
   {
    return WriteError( "Invalid file specification: \"" + filespec + "\"" );
   }
   #endregion Command Line Parsing
   try
   {
    // Check if the directory exists
    try
    {
     dir = ( filespec );
     if ( ( dir ) )
     {
      dir = ( "." );
     }
     if ( !( dir ) )
     {
      return WriteError( "Directory not found: \"" + dir + "\"" );
     }
     dir = ( trailingbackslash ) + "\\";
    }
    catch ( ArgumentException )
    {
     return WriteError( "Parent directory not found" );
    }
    // Extract the FILE specification (removing the path)
    string filenames = ( ( "\\" ) + 1 );
    // Enumerate the files
    string[] files = ( dir, filenames ).ToArray<string>( );
    int count = 0;
    foreach ( string file in files )
    {
     if ( ( file ) )
     {
      string filename = ( file );
      if ( ( locaseletters ) > -1 )
      {
       count++;
       string newfilename = dir + ( );
       ( file, newfilename );
      }
     }
    }
    if ( verbose )
    {
     ( "{0} matching file{1} renamed", ( count == 0 ? "No" : ( ) ), ( count == 1 ?  : "s" ) );
    }
    return count;
   }
   catch ( Exception e )
   {
    return WriteError(  );
   }
  }
  public static int WriteError( Exception e )
  {
   return WriteError( e == null ? null :  );
  }
  public static int WriteError( string errorMessage )
  {
   /*
   , Version 1.02
   Rename specified files to all upper case
   Usage:   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, . /V instead of /Verbose.
   Written by Rob van der Woude
   */
   if ( !( errorMessage ) )
   {
    ( );
     = ;
    ( "ERROR: " );
     = ;
    ( errorMessage );
    ( );
   }
   ( );
   ( ", Version 1.02" );
   ( "Rename specified files to all upper case" );
   ( );
   ( "Usage:  " );
    = ;
   ( " filespec [ /Verbose ]" );
   ( );
   ( );
   ( "Where:  " );
    = ;
   ( "filespec" );
   ( );
   ( "  is (are) the file(s) to be renamed (wildcards allowed)" );
    = ;
   ( "     /V" );
   ( );
   ( "erbose  displays the number of files renamed" );
   ( );
   ( "Note:   Use doublequotes if filespec contains spaces." );
   ( "     Return code (\"ErrorLevel\") equals the number of renamed files." );
   ( "     Switch may be abbreviated, . " );
    = ;
   ( "/V" );
   ( );
   ( " instead of " );
    = ;
   ( "/V" );
   ( );
   ( "erbose." );
   ( );
   ( "Written by Rob van der Woude" );
   ( "" );
   return 0;
  }
 }
}

I hope this article will be helpful to everyone's C# programming.