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#程序设计有所帮助。


猜你喜欢
- 1. xml文件中加入自定义 搜索view<com.etoury.etoury.ui.view.IconCenterEditText
- 一、前言最近做的项目由于引入第三方库导致在运行mvn clean package 打jar时,编译出来的 Jar 包很大(服务器多达500M
- spring boot配置hikari连接池属性事件起因与一个简单应用经常发生Young GC,甚至在没有请求量的情况下也经常发生GC (A
- 问题现象今天在做一个需求:将存入数据库中的数据读到后解析成list遍历分析数据格式:"[1677660600000, 167766
- 一、申请你的AppIDhttp://open.weixin.qq.com/ 友情提示:推荐使用eclipse打包软件最后一步的M
- 目录IO简介1.流Stream 2.IO流的继承结构3 File文件类3.1概述3.2创建对象3.3常用方法 3.4 练
- 前言:this 和 super 都是 Java 中常见的关键字,虽然二者在很多情况下都可以被省略,但它们在 Java 中所起的作用是不可磨灭
- IDEA 初使用昨天,我在某位大神的推荐下,下载了idea编辑器,同时被其强大的功能所震撼。此篇文章去帮助新手小白,来安装并,解决idea安
- 简要说明本文采用ImageSwitcher实现左右滑动切换图片。首先调用setFactory方法,设置视图工厂;然后设置手指触碰监听,判断左
- 示例1项目结构代码controller中 UserController.javapackage com.example.demo1110.c
- 题目要求阅读理解读完题的我be like:去看了遍英文版就懂了,题目中的种类【type】不是种类数…&hell
- 本文介绍了Spring Boot 部署jar和war两种方式的区别,分享给大家,具体如下:1、 packaging的方式不同,一种设置成ja
- List集合概述List集合是一个元素有序(每个元素都有对应的顺序索引,第一个元素索引为0)、且可重复的集合。List集合常用方 * ist是
- 我的安卓开发经历始于一个原生安卓项目开发。后来由于公司有个项目与几家医疗设备公司合作,需要我写安卓端的桥接代码给 react native
- 二叉树(binary tree)是一颗树,其中每个节点都不能有多于两个的儿子。1.二叉树节点作为图的特殊形式,二叉树的基本组成单元是节点与边
- Mybatis多层嵌套查询三张表:user article blog表的存储sql文件/*Navicat MySQL Data Transf
- 首先介绍一下Java解释器的概念,Java解释器:解释器是Java虚拟机非常重要的一部分,它的工作就是把字节码转化为机器码并在特定的平台进行
- 前言:由于最近有解析协议的一些业务场景,需要用到一些字节操作工具,这里封装了一些比较常用的转换方法,测试后基本没有问题,可能一些比较偏门的数
- 在这篇文章中,我们将介绍如下内容:==运算符与基元类型==运算符与引用类型==运算符与String类型==运算符与值类型==运算符与泛型==
- 在早期开发的时候,我们完成的都是静态页面也就是html页面,随着时间轴的发展,慢慢的引入了jsp页面,当在后端服务查询到数据之后可以转发到j