C#將制定目錄文件名轉(zhuǎn)換成大寫的方法
更新時間:2015年04月16日 11:20:45 作者:work24
這篇文章主要介紹了C#將制定目錄文件名轉(zhuǎn)換成大寫的方法,涉及C#操作文件及字符串的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實(shí)例講述了C#將制定目錄文件名轉(zhuǎn)換成大寫的方法。分享給大家供大家參考。具體如下:
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#程序設(shè)計有所幫助。
相關(guān)文章
C#實(shí)現(xiàn)JSON和對象之間互相轉(zhuǎn)換功能示例
這篇文章主要介紹了C#實(shí)現(xiàn)JSON和對象之間互相轉(zhuǎn)換功能,結(jié)合實(shí)例形式較為詳細(xì)的分析了C#實(shí)現(xiàn)對象與json之間相互轉(zhuǎn)換的操作技巧,需要的朋友可以參考下2017-09-09C#實(shí)現(xiàn)幾十萬級數(shù)據(jù)導(dǎo)出Excel及Excel各種操作實(shí)例
本篇文章主要介紹了C#實(shí)現(xiàn)幾十萬級數(shù)據(jù)導(dǎo)出Excel及Excel各種操作實(shí)例,這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。2017-02-02C#使用泛型方法設(shè)計實(shí)現(xiàn)單向鏈表詳解
這篇文章主要為大家詳細(xì)介紹了C#如何使用泛型方法設(shè)計實(shí)現(xiàn)一個單向鏈表,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-02-02c#求范圍內(nèi)素數(shù)的示例分享(c#求素數(shù))
問題是判斷101-200之間有多少個素數(shù),并輸出所有素數(shù)。下面是使用C#解決這個問題的方法 ,需要的朋友可以參考下2014-03-03C#單例模式Singleton的實(shí)現(xiàn)詳解
單例模式(Singleton?Pattern)是日常開發(fā)中最簡單的設(shè)計模式之一,它提供了一種創(chuàng)建對象的最佳方式,本文主要為大家介紹的是C#單例模式的實(shí)現(xiàn)方法,需要的可以參考一下2023-05-05