Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for count.cs

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5.  
  6.  
  7. namespace RobvanderWoude
  8. {
  9. 	internal class Count
  10. 	{
  11. 		static readonly string progver = "1.00";
  12.  
  13.  
  14. 		static int Main( string[] args )
  15. 		{
  16. 			bool quietmode = false;
  17. 			int count = 0;
  18. 			string filter = string.Empty;
  19. 			SearchOption searchoption = SearchOption.TopDirectoryOnly;
  20.  
  21.  
  22. 			if ( args.Contains( "/?" ) )
  23. 			{
  24. 				return ShowHelp( );
  25. 			}
  26.  
  27. 			foreach ( string arg in args )
  28. 			{
  29. 				if ( arg[0] == '/' )
  30. 				{
  31. 					if ( arg.ToUpper( ) == "/Q" )
  32. 					{
  33. 						if ( quietmode )
  34. 						{
  35. 							return ShowHelp( "Duplicate command line switch /Q" );
  36. 						}
  37. 						quietmode = true;
  38. 					}
  39. 					else if ( arg.ToUpper( ) == "/S" && !Console.IsInputRedirected )
  40. 					{
  41. 						if ( searchoption == SearchOption.AllDirectories )
  42. 						{
  43. 							return ShowHelp( "Duplicate command line switch /S" );
  44. 						}
  45. 						searchoption = SearchOption.AllDirectories;
  46. 					}
  47. 					else
  48. 					{
  49. 						return ShowHelp( "Invalid command line switch {0}", arg );
  50. 					}
  51. 				}
  52. 				else
  53. 				{
  54. 					if ( !string.IsNullOrWhiteSpace( filter ) )
  55. 					{
  56. 						if ( Console.IsInputRedirected )
  57. 						{
  58. 							return ShowHelp( "Duplicate filter specification" );
  59. 						}
  60. 						else
  61.                         {
  62. 							return ShowHelp( "Duplicate file specification" );
  63. 						}
  64. 					}
  65. 					filter = arg;
  66. 				}
  67. 			}
  68.  
  69.  
  70. 			if ( Console.IsInputRedirected )
  71. 			{
  72. 				while ( Console.In.Peek( ) != -1 )
  73. 				{
  74. 					string line = Console.ReadLine( );
  75. 					if ( line.Contains( filter ) )
  76. 					{
  77. 						count++;
  78. 					}
  79. 				}
  80. 			}
  81. 			else
  82. 			{
  83. 				if ( args.Length == 0 )
  84. 				{
  85. 					return ShowHelp( );
  86. 				}
  87.  
  88. 				try
  89. 				{
  90. 					string dir = Directory.GetParent( filter ).FullName;
  91. 					string files = Path.GetFileName( filter );
  92. 					if ( Directory.Exists( filter ) )
  93. 					{
  94. 						dir = Path.GetFullPath( filter );
  95. 						files = "*.*";
  96. 					}
  97. 					count = Directory.GetFiles( dir, files, searchoption ).Length;
  98. 				}
  99. 				catch( Exception e )
  100. 				{
  101. 					return ShowHelp( e.Message );
  102. 				}
  103. 			}
  104.  
  105. 			if ( !quietmode )
  106. 			{
  107. 				Console.WriteLine( count );
  108. 			}
  109.  
  110. 			return count;
  111. 		}
  112.  
  113.  
  114. 		public static int ShowHelp( params string[] errmsg )
  115. 		{
  116. 			#region Error Message
  117.  
  118. 			if ( errmsg.Length > 0 )
  119. 			{
  120. 				List<string> errargs = new List<string>( errmsg );
  121. 				errargs.RemoveAt( 0 );
  122. 				Console.Error.WriteLine( );
  123. 				Console.ForegroundColor = ConsoleColor.Red;
  124. 				Console.Error.Write( "ERROR:\t" );
  125. 				Console.ForegroundColor = ConsoleColor.White;
  126. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  127. 				Console.ResetColor( );
  128. 			}
  129.  
  130. 			#endregion Error Message
  131.  
  132.  
  133. 			#region Help Text
  134.  
  135. 			/*
  136. 			Count.exe,  Version 1.00
  137. 			Count specified objects and return result as errorlevel
  138.  
  139. 			Usage:    Count.exe  filespec  [ /S ]  [ /Q ]
  140.  
  141. 			   or:    some_prog  |  Count.exe  [ filter ]  [ /Q ]
  142.  
  143. 			Where:    filespec      specifies files to be counted (default: *.*)
  144. 			          some_prog     program whose output is to be counted (all lines)
  145. 			          filter        filter for some_prog (count lines containing filter)
  146. 			          /Q            Quiet mode: no screen output
  147. 			          /S            recursive (include Subdirectories)
  148.  
  149. 			Examples:
  150. 			=========
  151.  
  152. 			Count number of *.exe files in C:\Windows\System32 and subdirectories:
  153. 			Count.exe C:\Windows\System32\*.exe /S
  154.  
  155. 			Count number of subdirectories in C:\Windows\System32:
  156. 			DIR /AD /B /S C:\Windows\System32 | Count.exe
  157.  
  158. 			Count number of environment variables:
  159. 			SET | Count.exe "="
  160.  
  161. 			Notes:    Instead of Count.exe, one could use Windows' native FIND.EXE
  162. 			          with its /C switch in a FOR /F loop, but Count.exe may make
  163. 			          your batch code easier to read by using SET Count=%ErrorLevel%
  164. 			          Count.exe returns the number of (matching) lines, not the
  165. 			          number of occurrences of the specified filter string.
  166. 			          Count result is displayed on screen and returned as errorlevel;
  167. 			          both are limited to 32-bit integer values.
  168. 			          Return code (\"ErrorLevel\") equals count result, or -1 for
  169. 			          (command line) errors.
  170.  
  171. 			Written by Rob van der Woude
  172. 			https://www.robvanderwoude.com
  173. 			*/
  174.  
  175. 			#endregion Help Text
  176.  
  177.  
  178. 			#region Display Help Text
  179.  
  180. 			Console.Error.WriteLine( );
  181.  
  182. 			Console.Error.WriteLine( "Count.exe,  Version {0}", progver );
  183.  
  184. 			Console.Error.WriteLine( "Count specified objects and return result as errorlevel" );
  185.  
  186. 			Console.Error.WriteLine( );
  187.  
  188. 			Console.Error.Write( "Usage:    " );
  189. 			Console.ForegroundColor = ConsoleColor.White;
  190. 			Console.Error.WriteLine( "Count.exe  filespec  [ /S ]  [ /Q ]" );
  191. 			Console.ResetColor( );
  192.  
  193. 			Console.Error.WriteLine( );
  194.  
  195. 			Console.Error.Write( "   or:    " );
  196. 			Console.ForegroundColor = ConsoleColor.White;
  197. 			Console.Error.WriteLine( "some_prog  |  Count.exe  [ filter ]  [ /Q ]" );
  198. 			Console.ResetColor( );
  199.  
  200. 			Console.Error.WriteLine( );
  201.  
  202. 			Console.Error.Write( "Where:    " );
  203. 			Console.ForegroundColor = ConsoleColor.White;
  204. 			Console.Error.Write( "filespec" );
  205. 			Console.ResetColor( );
  206. 			Console.Error.WriteLine( "      specifies files to be counted (default: *.*)" );
  207.  
  208. 			Console.ForegroundColor = ConsoleColor.White;
  209. 			Console.Error.Write( "          some_prog" );
  210. 			Console.ResetColor( );
  211. 			Console.Error.WriteLine( "     program whose output is to be counted (all lines)" );
  212.  
  213. 			Console.ForegroundColor = ConsoleColor.White;
  214. 			Console.Error.Write( "          filter" );
  215. 			Console.ResetColor( );
  216. 			Console.Error.WriteLine( "        filter for some_prog (count lines containing filter)" );
  217.  
  218. 			Console.ForegroundColor = ConsoleColor.White;
  219. 			Console.Error.Write( "          /Q            Q" );
  220. 			Console.ResetColor( );
  221. 			Console.Error.WriteLine( "uiet mode: no screen output" );
  222.  
  223. 			Console.ForegroundColor = ConsoleColor.White;
  224. 			Console.Error.Write( "          /S" );
  225. 			Console.ResetColor( );
  226. 			Console.Error.Write( "            recursive (include " );
  227. 			Console.ForegroundColor = ConsoleColor.White;
  228. 			Console.Error.Write( "S" );
  229. 			Console.ResetColor( );
  230. 			Console.Error.WriteLine( "ubdirectories)" );
  231.  
  232. 			Console.Error.WriteLine( );
  233.  
  234. 			Console.Error.WriteLine( "Examples:" );
  235.  
  236. 			Console.Error.WriteLine( "=========" );
  237.  
  238. 			Console.Error.WriteLine( );
  239.  
  240. 			Console.Error.WriteLine( "Count number of *.exe files in C:\\Windows\\System32 and subdirectories:" );
  241.  
  242. 			Console.ForegroundColor = ConsoleColor.Green;
  243. 			Console.Error.WriteLine( "Count.exe C:\\Windows\\System32\\*.exe /S" );
  244. 			Console.ResetColor( );
  245.  
  246. 			Console.Error.WriteLine( );
  247.  
  248. 			Console.Error.WriteLine( "Count number of subdirectories in C:\\Windows\\System32:" );
  249.  
  250. 			Console.ForegroundColor = ConsoleColor.Green;
  251. 			Console.Error.WriteLine( "DIR /AD /B /S C:\\Windows\\System32 | Count.exe" );
  252. 			Console.ResetColor( );
  253.  
  254. 			Console.Error.WriteLine( );
  255.  
  256. 			Console.Error.WriteLine( "Count number of environment variables:" );
  257.  
  258. 			Console.ForegroundColor = ConsoleColor.Green;
  259. 			Console.Error.WriteLine( "SET | Count.exe \"=\"" );
  260. 			Console.ResetColor( );
  261.  
  262. 			Console.Error.WriteLine( );
  263.  
  264. 			Console.Error.WriteLine( "Notes:    Instead of Count.exe, one could use Windows' native FIND.EXE" );
  265.  
  266. 			Console.Error.WriteLine( "          with its /C switch in a FOR /F loop, but Count.exe may make" );
  267.  
  268. 			Console.Error.Write( "          your batch code easier to read, by using " );
  269. 			Console.ForegroundColor = ConsoleColor.Green;
  270. 			Console.Error.WriteLine( "SET Count=%ErrorLevel%" );
  271. 			Console.ResetColor( );
  272.  
  273. 			Console.Error.WriteLine( "          Count.exe returns the number of (matching) lines, not the" );
  274.  
  275. 			Console.Error.WriteLine( "          number of occurrences of the specified filter string." );
  276.  
  277. 			Console.Error.WriteLine( "          Count result is displayed on screen and returned as errorlevel;" );
  278.  
  279. 			Console.Error.WriteLine( "          both are limited to 32-bit integer values." );
  280.  
  281. 			Console.Error.WriteLine( "          Return code (\"ErrorLevel\") equals count result, or -1 for" );
  282.  
  283. 			Console.Error.WriteLine( "          (command line) errors." );
  284.  
  285. 			Console.Error.WriteLine( );
  286.  
  287. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  288.  
  289. 			Console.Error.WriteLine( "https://www.robvanderwoude.com" );
  290.  
  291. 			#endregion Display Help Text
  292.  
  293.  
  294. 			return -1;
  295. 		}
  296.  
  297. 	}
  298. }

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