C#将制定目录文件名转换成大写的方法
2015-04-16 11:20
1331 查看
本文实例讲述了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#程序设计有所帮助。
您可能感兴趣的文章:
相关文章推荐
- C#将指定目录所有文件名转换成小写的方法
- 使用C#实现阿拉伯数字到大写中文的转换-第二种方法
- 使用C#实现阿拉伯数字到大写中文的转换-第二种方法
- C#中把英文字母转换为大写或小写的方法
- C# 货币金额中文(英文)大写转换方法-工具类
- 目录所有子目录名及文件名大写转换成小写
- C#实现将商品金额小写转换成大写的方法
- sql server 关于表中只增标识问题 C# 实现自动化打开和关闭可执行文件(或 关闭停止与系统交互的可执行文件) ajaxfileupload插件上传图片功能,用MVC和aspx做后台各写了一个案例 将小写阿拉伯数字转换成大写的汉字, C# WinForm 中英文实现, 国际化实现的简单方法 ASP.NET Core 2 学习笔记(六)ASP.NET Core 2 学习笔记(三)
- python把指定目录下的递归所有目录和文件名转换成小写或大写
- C#实现小写金额转换大写的方法
- c# 字符串的首字母大写转换 方法
- C# 数字货币转换大写方法
- 从网址中获取域名,目录名,文件名的C#方法
- C#的path.GetFullPath 获取上级目录实现方法
- C#金额转换为汉字大写
- C#:小写金额转换为大写
- C#利用递归方法在树控件中例遍磁盘目录及文件
- [转]C#中得到程序当前工作目录和执行目录的一些方法
- Java 的JSON、XML转换方法——目录索引(转载)
- C#中字符串转换成枚举类型的方法