C#将指定目录所有文件名转换成小写的方法
程序员文章站
2022-06-14 17:23:14
本文实例讲述了c#将指定目录所有文件名转换成小写的方法。分享给大家供大家参考。具体如下:
using system;
using system.io;
usi...
本文实例讲述了c#将指定目录所有文件名转换成小写的方法。分享给大家供大家参考。具体如下:
using system; using system.io; using system.linq; namespace robvanderwoude { class locase { static int main( string[] args ) { string dir = string.empty; string filespec = string.empty; char[] trailingbackslash = "\\".tochararray( 0, 1 ); char[] upcaseletters = "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( upcaseletters ) > -1 ) { count++; string newfilename = dir + filename.tolowerinvariant( ); 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 ) { 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( "locase.exe, version 1.02" ); console.error.writeline( "rename specified files to all lower case" ); console.error.writeline( ); console.error.write( "usage: " ); console.foregroundcolor = consolecolor.white; console.error.writeline( "locase.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" ); return 0; } } }
希望本文所述对大家的c#程序设计有所帮助。
上一篇: C#进度轴控件分享