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)

相关推荐

  • C#获取图片文件扩展名的方法

    下面我给各位朋友整理了一篇C# 获取图片文件扩展名的例子,这里方法都非常的简单,我们只用到了image.RawFormat.Guid就实现了,具体看代码 例子 复制代码 代码如下: /// <summary> /// 根据图像获取图像的扩展名 /// </summary> /// <param name="image"></param> /// <returns></returns> public static S

  • C#文件后缀名的详细介绍

    .sln:解决方案文件,为解决方案资源管理器提供显示管理文件的图形接口所需的信息. .csproj:项目文件,创建应用程序所需的引用.数据连接.文件夹和文件的信息. .aspx:Web 窗体页由两部分组成:视觉元素(HTML.服务器控件和静态文本)和该页的编程逻辑.Visual Studio 将这两个组成部分分别存储在一个单独的文件中.视觉元素在.aspx 文件中创建. .ascx:ASP.NET的用户控件(也叫做"pagelets"),是作为一种封装了特定功能和行为(这两者要被用在W

  • C#实现的文件批量重命名功能示例

    本文实例讲述了C#实现的文件批量重命名功能.分享给大家供大家参考,具体如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; //C#批量重命名文件代码的实现 //添加文件操作空间引用 using System.IO;

  • 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[] trailingback

  • C#中文件名或文件路径非法字符判断方法

    文件路径或者保存模板出现非法字符判断 1)不为空判断 string strTemplateName = txtTemplateName.Text; if (string.IsNullOrWhiteSpace(strTemplateName)) { Show("请输入模板名称!", "提示", .Information, OK); txtTemplateName.Focus(); return; } 2)然后对strTemplateName 进行非法字符判断 if (

  • C#编程实现获取文件夹中所有文件的文件名

    本文实例讲述了C#编程实现获取文件夹中所有文件的文件名.分享给大家供大家参考,具体如下: 想实现这样一个功能:批量修改一个目录所有jpg文件的文件名,在其原始基础上加上特定字符串 我先把一个目录里所有的格式为jpg的文件名存储到数组里,然后统一修改数组值来批量修改jpg文件的文件名 代码如下: using System; using System.IO; namespace ConsoleApplication7 { /// <summary> /// Class1 的摘要说明. /// &l

  • C#将时间转成文件名使用方法

    返回值类型:System.Int64表示为 Windows 文件时间的当前 DateTime 对象的值. 例如: 复制代码 代码如下: DateTime.Now.ToFileTime().ToString() 会得到一个字符串,类似:130308237880937500 的结果 Windows 文件时间是一个 64 位值,它表示自协调世界时 (UTC) 公元 (C.E.) 1601 年 1 月 1 日午夜 12:00 以来已经过的 间隔数(以 100 纳秒为一个间隔). Windows 使用文件

  • C#实现的文件操作封装类完整实例【删除,移动,复制,重命名】

    本文实例讲述了C#实现的文件操作封装类.分享给大家供大家参考,具体如下: 最近发现群共享里面有个C# 文件操作封装类,其方法是调用Windows API 来操作的文件的删除.移动.复制.重命名操作.下载下来一试,发现果然不错,特在此记录,以防丢失! 文件操作类代码如下: using System; using System.Runtime.InteropServices; using System.IO; namespace LxFile { /// <summary> /// 文件操作代理,

  • C#实现保存文件时重名自动生成新文件的方法

    本文实例讲述了C#实现保存文件时重名自动生成新文件的方法.分享给大家供大家参考.具体如下: 将一个文档保存为 a.txt 时,发现此文件已经存在,则自动保存为 a(1).txt /// <summary> /// Generates a new path for duplicate filenames. /// </summary> /// <param name="path">The path.</param> /// <retu

  • C#获取并修改文件扩展名的方法

    本文实例讲述了C#获取并修改文件扩展名的方法.分享给大家供大家参考.具体分析如下: 这里使用C#编程的方法改变文件扩展名的文件,必须使用Path类. Path类用来解析文件系统路径的各个部分.静态方法Path.ChangeExtension方法可以用来改变文件扩展名.可用Path.GetExtension方法可用来取得的文件扩展名. 复制代码 代码如下: string filePath = @"c:\file.txt"; Console.WriteLine(filePath); Con

  • c#文件名/路径处理方法示例

    复制代码 代码如下: string filePath = @"E:\Randy0528\中文目录\JustTest.rar"; Response.Write("文件路径:"+filePath); Response.Write("<br/>更改路径字符串的扩展名.<br/>"); Response.Write(System.IO.Path.ChangeExtension(filePath, "txt"))

随机推荐