注册 登录
  • 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

C#代码将指定目录的所有文件名转换成小写

OC/C/C++ 水墨上仙 1784次浏览 已收录 手机上查看

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 )
		{
			/*
			LoCase.exe,  Version 1.02
			Rename specified files to all lower case
			Usage:    LoCase.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
			http://www.robvanderwoude.com
			*/
			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" );
			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
			return 0;
		}
	}
}


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明C#代码将指定目录的所有文件名转换成小写
喜欢 (0)
[开心洋葱]
分享 (0)
关于作者:
水墨上仙
……
加载中……