Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for upcase.cs

(view source code of upcase.cs as plain text)

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4.  
  5.  
  6. namespace RobvanderWoude
  7. {
  8. 	class UpCase
  9. 	{
  10. 		public static string progver = "2.02";
  11.  
  12.  
  13. 		static int Main( string[] args )
  14. 		{
  15. 			bool verbose = false;
  16. 			char[] locaseletters = "abcdefghijklmnopqrstuvwxyz".ToCharArray( );
  17. 			List<string> files = new List<string>( );
  18.  
  19. 			#region Command Line Parsing
  20.  
  21. 			if ( Console.IsInputRedirected )
  22. 			{
  23. 				// No command line arguments in case of redirected input
  24. 				if ( args.Length > 0 )
  25. 				{
  26. 					return ShowHelp( );
  27. 				}
  28. 			}
  29. 			else
  30. 			{
  31. 				if ( args.Length == 0 || ( args.Length == 1 && args[0].ToUpper( ).Substring( 0, 2 ) == "/V" ) )
  32. 				{
  33. 					args = new string[] { "*.*" };
  34. 				}
  35. 				foreach ( string arg in args )
  36. 				{
  37. 					if ( arg[0] == '/' )
  38. 					{
  39. 						if ( arg == "/?" )
  40. 						{
  41. 							return ShowHelp( );
  42. 						}
  43. 						if ( arg.Length > 1 && arg.Substring( 0, 2 ).ToUpper( ) == "/V" )
  44. 						{
  45. 							if ( verbose )
  46. 							{
  47. 								return ShowHelp( "Duplicate command line switch /V" );
  48. 							}
  49. 							verbose = true;
  50. 						}
  51. 						else
  52. 						{
  53. 							return ShowHelp( "Invalid command line switch \"{0}\"", arg );
  54. 						}
  55. 					}
  56. 					else
  57. 					{
  58. 						string parentfolder = Directory.GetParent( arg ).FullName;
  59. 						if ( !Directory.Exists( parentfolder ) )
  60. 						{
  61. 							return ShowHelp( "Invalid parent folder \"{0}\"", parentfolder );
  62. 						}
  63. 						string filespec = Path.GetFileName( arg );
  64. 						foreach ( string file in Directory.GetFiles( parentfolder, filespec ) )
  65. 						{
  66. 							if ( Path.GetFileName( file ).IndexOfAny( locaseletters ) > -1 )
  67. 							{
  68. 								if ( !files.Contains( file ) )
  69. 								{
  70. 									files.Add( file );
  71. 								}
  72. 							}
  73. 						}
  74. 					}
  75. 				}
  76. 			}
  77.  
  78. 			#endregion Command Line Parsing
  79.  
  80.  
  81. 			if ( Console.IsInputRedirected )
  82. 			{
  83. 				#region Convert redirected input to upper case
  84.  
  85. 				Console.OpenStandardInput( );
  86. 				string input = Console.In.ReadToEnd( );
  87. 				Console.OpenStandardOutput( );
  88. 				string output = input.ToUpper( );
  89. 				Console.Out.Write( output );
  90. 				return 0;
  91.  
  92. 				#endregion Convert redirected input to upper case
  93. 			}
  94. 			else
  95. 			{
  96. 				#region Rename files to upper case
  97.  
  98. 				foreach ( string file in files )
  99. 				{
  100. 					if ( File.Exists( file ) )
  101. 					{
  102. 						string parentfolder = Directory.GetParent( file ).FullName;
  103. 						string filename = Path.GetFileName( file );
  104. 						if ( filename.IndexOfAny( locaseletters ) > -1 )
  105. 						{
  106. 							string newfilename = Path.Combine( parentfolder, filename.ToUpperInvariant( ) );
  107. 							try
  108. 							{
  109. 								if ( verbose )
  110. 								{
  111. 									Console.WriteLine( "\"{0}\"  =>  \"{1}\"", file, newfilename );
  112. 								}
  113. 								File.Move( file, newfilename );
  114. 							}
  115. 							catch ( Exception e )
  116. 							{
  117. 								Console.Error.WriteLine( "Error renaming \"{0}\": {1}", filename, e.Message );
  118. 							}
  119. 						}
  120. 					}
  121. 				}
  122. 				if ( verbose )
  123. 				{
  124. 					Console.WriteLine( "{0} matching file{1} renamed", ( files.Count == 0 ? "No" : files.Count.ToString( ) ), ( files.Count == 1 ? String.Empty : "s" ) );
  125. 				}
  126. 				return files.Count;
  127.  
  128. 				#endregion Rename files to upper case
  129. 			}
  130. 		}
  131.  
  132.  
  133. 		#region Error handling
  134.  
  135. 		public static int ShowHelp( params string[] errmsg )
  136. 		{
  137. 			#region Help text
  138.  
  139. 			/*
  140. 			UpCase.exe,  Version 2.02
  141. 			Either rename specified files or render redirected input to all upper case
  142.  
  143. 			Usage:    UpCase.exe  [ filespec  [ filespec  [ ... ] ] ]  [ /V ]
  144.  
  145. 			or:       somecommand  |  UpCase.exe
  146.  
  147. 			Where:    filespec     file(s) to be renamed (wildcards allowed, default: *.*)
  148. 					  somecommand  command whose output will be rendered to upper case
  149. 					  /V           Verbose mode: displays file renaming and files count
  150.  
  151. 			Notes:    Use doublequotes if filespec contains non-alphnumeric characters.
  152. 			          If no folder is specified in filespec, current directory is assumed.
  153. 					  Return code (\"ErrorLevel\") equals the number of renamed files,
  154. 					  or -1 in case of errors.
  155. 			          This program requires .NET Framework 4.5; use an older version of
  156. 			          of this program if you don't have .NET Framework 4.5 installed.
  157.  
  158. 			Written by Rob van der Woude
  159. 			http://www.robvanderwoude.com
  160. 			*/
  161.  
  162. 			if ( errmsg.Length > 0 )
  163. 			{
  164. 				List<string> errargs = new List<string>( errmsg );
  165. 				errargs.RemoveAt( 0 );
  166. 				Console.Error.WriteLine( );
  167. 				Console.ForegroundColor = ConsoleColor.Red;
  168. 				Console.Error.Write( "ERROR:\t" );
  169. 				Console.ForegroundColor = ConsoleColor.White;
  170. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  171. 				Console.ResetColor( );
  172. 			}
  173.  
  174. 			Console.Error.WriteLine( );
  175.  
  176. 			Console.Error.WriteLine( "IpCase.exe,  Version {0}", progver );
  177.  
  178. 			Console.Error.WriteLine( "Either rename specified files or render redirected input to all upper case" );
  179.  
  180. 			Console.Error.WriteLine( );
  181.  
  182. 			Console.Error.Write( "Usage:    " );
  183. 			Console.ForegroundColor = ConsoleColor.White;
  184. 			Console.Error.WriteLine( "UpCase.exe  [ filespec  [ filespec  [ ... ] ] ]  [ /V ]" );
  185. 			Console.ResetColor( );
  186.  
  187. 			Console.Error.WriteLine( );
  188.  
  189. 			Console.Error.Write( "or:       " );
  190. 			Console.ForegroundColor = ConsoleColor.White;
  191. 			Console.Error.WriteLine( "somecommand  |  UpCase.exe" );
  192. 			Console.ResetColor( );
  193.  
  194. 			Console.Error.WriteLine( );
  195.  
  196. 			Console.Error.Write( "Where:    " );
  197. 			Console.ForegroundColor = ConsoleColor.White;
  198. 			Console.Error.Write( "filespec" );
  199. 			Console.ResetColor( );
  200. 			Console.Error.WriteLine( "     file(s) to be renamed (wildcards allowed, default: *.*)" );
  201.  
  202. 			Console.ForegroundColor = ConsoleColor.White;
  203. 			Console.Error.Write( "          somecommand" );
  204. 			Console.ResetColor( );
  205. 			Console.Error.WriteLine( "  command whose output will be rendered to upper case" );
  206.  
  207. 			Console.ForegroundColor = ConsoleColor.White;
  208. 			Console.Error.Write( "          /V           V" );
  209. 			Console.ResetColor( );
  210. 			Console.Error.WriteLine( "erbose mode: displays file renaming and files count" );
  211.  
  212. 			Console.Error.WriteLine( );
  213.  
  214. 			Console.Error.WriteLine( "Notes:    Use doublequotes if filespec contains non-alphnumeric characters." );
  215.  
  216. 			Console.Error.WriteLine( "          If no folder is specified in filespec, current directory is assumed." );
  217.  
  218. 			Console.Error.WriteLine( "          Return code (\"ErrorLevel\") equals the number of renamed files," );
  219.  
  220. 			Console.Error.WriteLine( "          or -1 in case of errors." );
  221.  
  222. 			Console.Error.WriteLine( "          This program requires .NET Framework 4.5; use an older version of" );
  223.  
  224. 			Console.Error.WriteLine( "          of this program if you don't have .NET Framework 4.5 installed." );
  225.  
  226. 			Console.Error.WriteLine( );
  227.  
  228. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  229.  
  230. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  231.  
  232. 			#endregion Help text
  233.  
  234. 			return -1;
  235. 		}
  236.  
  237. 		#endregion Error handling
  238. 	}
  239. }
  240.  

page last modified: 2024-04-16; loaded in 0.0280 seconds