-
C#教程之C#教程之C#将制定目录文件名转换成大写的方法
本站最新发布 C#从入门到精通
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
本文实例讲述了C#将制定目录文件名转换成大写的方法。分享给大家供大家参考。具体如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
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" ); return 0; } } } |
希望本文所述对大家的C#程序设计有所帮助。
栏目列表
最新更新
如何使用OS模块中的stat方法
Python os 模块
seek() 方法
python打开文件实例1
Python写入文件
什么是流?
文件操作如何进制逐行读取
Python相对路径
with创建临时运行环境
Python文件操作
.Net Standard(.Net Core)实现获取配置信息
Linux PXE + Kickstart 自动装机
Shell 编程 基础
Shell 编程 条件语句
CentOS8-网卡配置及详解
Linux中LVM逻辑卷管理
1.数码相框-相框框架分析(1)
Ubuntu armhf 版本国内源
Linux中raid磁盘阵列
搭建简易网站
access教程之Access简介
mysql 安装了最新版本8.x版本后的报错:
Mysql空间数据&空间索引(spatial)
如何远程连接SQL Server数据库的图文教程
复制SqlServer数据库的方法
搜索sql语句
sql中返回参数的值
sql中生成查询的模糊匹配字符串
数据定义功能
数据操作功能