C#将制定目录文件名转换成大写的方法
作者:work24 发布时间:2022-10-03 19:46:36
标签:C#,转换,大写
本文实例讲述了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#程序设计有所帮助。
0
投稿
猜你喜欢
- 工厂方法模式(Factory Method):定义一个用于创建对象的接口,让子类决定实例化哪一个类。工厂方法使一个类的实例化延迟到其子类。工
- 前言在一些日常业务中,会遇到一些琐碎文件需要统一打包到一个压缩包中上传,业务方在后台接收到压缩包后自行解压,然后解析相应文件。而且可能涉及安
- 最近做了一个MyEclipse项目,但是没开始多久就发现了这个问题:只要文件被修改过,不论多小的修改,保存的时候都会跳出一个框框,里面写着t
- 使用环境项目环境:Idea 2020.2.3、 Maven 3.6.3 、springboot 2.1.4本人在创建springboot项目
- 前言:本文源码基于spring-framework-5.3.10。mvc是spring源码中的一个子模块!一、RequestMappingH
- 简介switch的新特性可是源远流长,早在JDK 12就以预览功能被引入了,最终在JDK 14成为了正式版本的功能:JEP 361: Swi
- 从现在开始,大家可以跟随着我的脚步来自定义一个属于自己的Spring框架。但是,在学习自定义Spring框架之前,我们得先来回顾一下Spri
- 本文实例讲述了Java泛型的使用限制。分享给大家供大家参考,具体如下:一 什么情况下不能使用泛型1 不能使用泛型的形参创建对象。T o=ne
- 什么是反射?反射是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法和属性;这种动
- 前言我们经常会被问到这么一个问题:SpringBoot相对于spring有哪些优势呢?其中有一条答案就是SpringBoot自动注入。那么自
- 以下摘自胖哥分享的 2022开工福利教程。在学习Spring Security的时候你有没有下面这两个疑问:Spring Security的
- 现在Java的大部分项目都是基于Maven, 在Maven项目中使用Selenium2. 非常简单。 首先你需要配置好
- 谷歌官方推出了一种侧滑菜单的实现方式(抽屉效果),即 DrawerLayout,这个类是在Support Library里的,需要加上and
- <profiles> <profile> <
- 1.图的遍历从图中某一顶点出发访问图中其余顶点,且每个顶点仅被访问一次图的遍历有两种深度优先遍历DFS、广度优先遍历BFS2.深度优先遍历深
- 1、引言在SpringMVC的使用中,后端与前端的交互一般是使用Json格式进行数据传输,SpringMVC的@Response
- 最近在学习springboot,session这个点一直困扰了我好久,今天把这些天踩的坑分享出来吧,希望能帮助更多的人。一、pom.xml配
- 1、使用JPA 的@Enumerated 注解 ,可以直接将Enum映射到数据库中。但是value的值只有两种方式选择,一种是使用枚举的序号
- 基于Java语言实现Socket通信由于近日项目需求,需要在服务器中增加Socket通信的功能,接收硬件设备发送的心跳包和相关数据,因此又重
- SpringBoot 2.1.4 错误处理机制springboot的自动配置中帮我们配置了相关的错误处理组件,例如访问一个不存在的页面,就会