C#将指定目录所有文件名转换成小写的方法
作者:work24 发布时间:2023-01-25 23:21:03
标签:C#,目录,文件名,小写
本文实例讲述了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#程序设计有所帮助。
0
投稿
猜你喜欢
- 背景众所周知,所有被打开的系统资源,比如流、文件或者Socket连接等,都需要被开发者手动关闭,否则随着程序的不断运行,资源泄露将会累积成重
- @ModelAttribute在父类、子类的执行顺序被 @ModelAttribute 注解的方法会在Controller每个方法执行之前都
- 前言最近工作上遇到很多批量插入的场景,但是百度很难得到我想要的结果,而且查出来的效果不是很好~所以就自己来写一份给大家参考,希望对大家有用M
- 一.求两直线交点class Point { double x; do
- 前言序列化想必大家都很熟悉了,对象在进行网络传输过程中,需要序列化之后才能传输到客户端,或者客户端的数据序列化之后送达到服务端序列化的标准解
- 本文研究的主要是ConcurrentMap.putIfAbsent(key,value)用法的相关内容,具体如下。业务上经常会遇到有这种场景
- Android 显示GIF图片实例详解gif图动画在Android中还是比较常用的,比如像新浪微博中,有很多gif图片,而且展示非常好,所以
- 我们知道在编程时许多操作(如更新UI)需要在主线程中完成,而且,耗时操作(如网络连接)需要放在子线程中,否则会引起ANR。所以我们常使用Ha
- 在Java中可以使用HttpServer类来实现Http服务器,该类位于com.sun.net包下(rt.jar)。实现代码如下:主程序类p
- 1 概念方面List是接口,ArrayList是List接口的一个实现类2 初始化方面2.1 List2.1.1 错误写 * ist list
- Java 分割字符串java.lang.String 的 split() 方法, JDK 1.4 or laterpublic
- 一、参数校验springboot 使用校验框架validation校验方法的入参SpringBoot的Web组件内部集成了hibernate
- 目录什么是Spring的占位符?Spring什么时候去解析并占位符什么是Spring的占位符?在以前的Spring Xml配置中我们可能会有
- 改进思考正常实现流程应该为继承ClassLoader虚拟类,并重写其loadClass方法和findClass方法,并在loadClass方
- mybatis in查询条件过长的解决方法1:分次查询,将参数且分割成多个短的查询后合并代码: in
- 该篇文章内容较多,包括有rabbitMq相关的一些简单理论介绍,provider消息推送实例,consumer消息消费实例,Direct、T
- MyBatis Generator简介MyBatis Generator(MBG)是MyBatis MyBatis 和iBATIS的代码生成
- 消息过滤RocketMQ分布式消息队列的消息过滤方式有别于其它MQ中间件,是在Consumer端订阅消息时再做消息过滤的。RocketMQ这
- 我们平时在日常项目中经常会遇到图片的上传和访问的情景,平时我们可能习惯于把图片传到resource或者项项目中的某个位置,这样会有一个缺点,
- 这篇文章主要介绍了spring boot如何指定启动端口,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的