Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for listintcmd.cs

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Windows.Forms;
  8.  
  9.  
  10.  
  11. namespace RobvanderWoude
  12. {
  13. 	class ListIntCmd
  14. 	{
  15. 		public static string progver = "1.07";
  16.  
  17.  
  18. 		[STAThreadAttribute]
  19. 		static int Main( string[] args )
  20. 		{
  21. 			#region Initialize variables
  22.  
  23. 			int rc = 0;
  24. 			string separartor = Environment.NewLine;
  25. 			bool separatorset = false;
  26. 			bool copy = false;
  27. 			bool copyset = false;
  28. 			bool logging = false;
  29. 			bool logset = false;
  30. 			string logdir = Directory.GetParent( System.Reflection.Assembly.GetEntryAssembly( ).Location ).ToString( ); // This program's parent folder
  31. 			string logfile = Path.Combine( logdir, "ListIntCmd.log" );
  32. 			bool logfileset = false;
  33. 			string logtext = String.Empty;
  34. 			DateTime logbegin;
  35. 			DateTime logend;
  36.  
  37. 			#endregion Initialize variables
  38.  
  39.  
  40. 			#region Command Line Parsing
  41.  
  42. 			if ( args.Length > 0 )
  43. 			{
  44. 				foreach ( string arg in args )
  45. 				{
  46. 					switch ( arg.Substring( 0, Math.Min( 2, arg.Length ) ).ToUpper( ) )
  47. 					{
  48.  
  49. 						case "/?":
  50. 							return ShowHelp( );
  51. 						case "/C":
  52. 							if ( copyset )
  53. 							{
  54. 								return ShowHelp( "Duplicate command line switch /C" );
  55. 							}
  56. 							copy = true;
  57. 							copyset = true;
  58. 							break;
  59. 						case "/L":
  60. 							if ( logset )
  61. 							{
  62. 								return ShowHelp( "Duplicate command line switch /L" );
  63. 							}
  64. 							logging = true;
  65. 							logset = true;
  66. 							if ( arg.Length > 4 && arg[2] == ':' )
  67. 							{
  68. 								logfile = Environment.ExpandEnvironmentVariables( arg.Substring( 3 ).Trim( "\" ".ToCharArray( ) ) );
  69. 								logdir = Directory.GetParent( logfile ).ToString( );
  70. 								logfileset = true;
  71. 							}
  72. 							break;
  73. 						default:
  74. 							if ( separatorset )
  75. 							{
  76. 								return ShowHelp( );
  77. 							}
  78. 							// Translate:               \n to linefeed,                       \t to tab,             \/ to slash,          \\ to backslash, escaped \n to literal \n, escaped \t to literal \t
  79. 							separartor = arg.Replace( "\\n", Environment.NewLine ).Replace( "\\t", "\t" ).Replace( "\\/", "/" ).Replace( "\\\\", "\\" ).Replace( "\\\n", "\\n" ).Replace( "\\\t", "\\t" );
  80. 							separatorset = true;
  81. 							break;
  82. 					}
  83. 				}
  84. 			}
  85.  
  86. 			if ( logfileset && !String.IsNullOrEmpty( logfile ) )
  87. 			{
  88. 				if ( !Directory.Exists( logdir ) )
  89. 				{
  90. 					return ShowHelp( string.Format( "Invalid log path \"{0}\"", logdir ) );
  91. 				}
  92. 			}
  93.  
  94. 			#endregion Command Line Parsing
  95.  
  96.  
  97. 			try
  98. 			{
  99. 				string comspec = Environment.GetEnvironmentVariable( "COMSPEC" );
  100.  
  101. 				logbegin = DateTime.Now;
  102. 				logtext += string.Format( "ListIntCmd.exe,  Version {0}{1}", progver, Environment.NewLine );
  103. 				logtext += string.Format( "{0}{1}", Environment.OSVersion.VersionString, Environment.NewLine );
  104. 				logtext += string.Format( "Search started at {0}{1}{1}", logbegin.ToString( "yyyy-MM-dd, HH:mm:ss.fff" ), Environment.NewLine );
  105. 				logtext += string.Format( "COMSPEC=\"{0}, ProductVersion={1}\"{2}{2}", comspec, FileVersionInfo.GetVersionInfo( comspec ).ProductVersion, Environment.NewLine );
  106.  
  107. 				StreamReader file = new StreamReader( comspec, Encoding.ASCII );
  108. 				string content = file.ReadToEnd( );
  109. 				file.Close( );
  110.  
  111. 				string pattern = @"c\0m\0d\0\.\0e\0x\0e\0[\w\W]*?\(c\)";
  112. 				if ( Regex.IsMatch( content, pattern ) )
  113. 				{
  114. 					content = Regex.Match( content, pattern ).ToString( );
  115. 				}
  116.  
  117. 				List<string> intcmds = new List<string>( ) { "CD", "DATE", "GOTO", "KEYS", "REM", "TIME" }; // preload internal commands that are skipped by our previous -- too tight -- RegEx pattern
  118. 				logtext += string.Format( "{0}Preloaded internal commands:{0}{0}{1}{0}{0}", Environment.NewLine, string.Join( Environment.NewLine, intcmds.ToArray( ) ) );
  119. 				// extended exclusion string for Windows 2000..10; the longer the exclusion string, the higher the risk that some new future internal command will be omited in the output
  120. 				string excludestr = "BAT,CMD,COM,DISABLEDELAYEDEXPANSION,DLL,EQU,EXE,GEQ,GTR,HH,JS,LEQ,LSS,MM,MSC,NEQ,NTDLL,SM,VBS,VERSION,VS,WS";
  121. 				string[] excludearr = excludestr.Split( ",".ToCharArray( ) );
  122. 				List<string> exclude = new List<string>( excludearr ); // Optimized for .NET Framework 2.0; in .NET Framework 3.5+ we might have used List<string> exclude = excludestr.Split( ",".ToCharArray( ) ).ToList<string>( );
  123.  
  124. 				pattern = @"([A-Z]\0){2,}";
  125. 				Regex regex = new Regex( pattern );
  126. 				if ( regex.IsMatch( content ) )
  127. 				{
  128. 					logtext += string.Format( "{0}List of regex matches:{0}", Environment.NewLine );
  129. 					foreach ( Match match in regex.Matches( content ) )
  130. 					{
  131. 						string line = String.Empty;
  132. 						string intcmd = match.ToString( ).Replace( "\0", string.Empty );
  133. 						line += string.Format( "{0}{1,-24}", Environment.NewLine, intcmd );
  134. 						if ( exclude.Contains( intcmd ) )
  135. 						{
  136. 							line += string.Format( "\texcluded by exlusion list", intcmd );
  137. 						}
  138. 						else if ( intcmds.Contains( intcmd ) )
  139. 						{
  140. 							line += string.Format( "\tskipped duplicate", intcmd );
  141. 						}
  142. 						else
  143. 						{
  144. 							intcmds.Add( intcmd );
  145. 							line += string.Format( "\tadded to the list of internal commands", intcmd );
  146. 						}
  147. 						logtext += line.TrimEnd( );
  148. 					}
  149. 					logtext += string.Format( "{0}{0}{0}Results so far:{0}{0}{1}{0}{0}", Environment.NewLine, String.Join( Environment.NewLine, intcmds.ToArray( ) ) );
  150. 					intcmds.Sort( );
  151. 					logtext += string.Format( "{0}Results after sorting:{0}{0}{1}{0}{0}", Environment.NewLine, String.Join( Environment.NewLine, intcmds.ToArray( ) ) );
  152. 				}
  153.  
  154. 				// Return a default list if we could not find the internal commands in %COMSPEC%
  155. 				if ( intcmds.Count == 0 )
  156. 				{
  157. 					logtext += string.Format( "{0}Using default, hard-coded list{0}{0}", Environment.NewLine );
  158. 					string defintcmdsstr = "ASSOC,BREAK,CALL,CD,CHDIR,CLS,COLOR,COPY,DATE,DEL,DIR,DPATH,ECHO,ENDLOCAL,ERASE,EXIT,FOR,FTYPE,GOTO,IF,KEYS,MD,MKDIR,MKLINK,MOVE,PATH,PAUSE,POPD,PROMPT,PUSHD,RD,REM,REN,RENAME,RMDIR,SET,SETLOCAL,SHIFT,START,TIME,TITLE,TYPE,VER,VERIFY,VOL";
  159. 					string[] defintcmdsarr = defintcmdsstr.Split( ",".ToCharArray( ), StringSplitOptions.RemoveEmptyEntries );
  160. 					intcmds = new List<string>( defintcmdsarr ); // Optimized for .NET Framework 2.0; in .NET Framework 3.5+ we might have used List<string> intcmds = defintcmdsstr.Split( ",".ToCharArray( ) ).ToList<string>( );
  161. 					rc = 2;
  162. 				}
  163.  
  164. 				string result = String.Join( separartor, intcmds.ToArray( ) );
  165.  
  166. 				Console.Write( result );
  167.  
  168. 				if ( copy )
  169. 				{
  170. 					logtext += string.Format( "Writing result to clipboard . . .{0}{0}", Environment.NewLine );
  171. 					Clipboard.SetText( result );
  172. 					logtext += string.Format( "Text written to clipboard:{1}{1}{0}{1}{1}", Clipboard.GetText( ), Environment.NewLine );
  173. 				}
  174.  
  175. 				if ( logging )
  176. 				{
  177. 					logend = DateTime.Now;
  178. 					TimeSpan duration = logend - logbegin;
  179. 					logtext += string.Format( "Search ended at {0} ({1:N0} milliseconds){2}", logend.ToString( "yyyy-MM-dd, HH:mm:ss.fff" ), duration.TotalMilliseconds, Environment.NewLine );
  180. 					StreamWriter logstream = new StreamWriter( logfile );
  181. 					logstream.Write( logtext );
  182. 					logstream.Close( );
  183. 				}
  184.  
  185. 				return rc;
  186. 			}
  187. 			catch ( Exception e )
  188. 			{
  189. 				return ShowHelp( e.Message );
  190. 			}
  191. 		}
  192.  
  193.  
  194. 		#region Error Handling
  195.  
  196. 		public static int ShowHelp( params string[] errmsg )
  197. 		{
  198. 			#region Error Message
  199.  
  200. 			if ( errmsg.Length > 0 )
  201. 			{
  202. 				List<string> errargs = new List<string>( errmsg );
  203. 				errargs.RemoveAt( 0 );
  204. 				Console.Error.WriteLine( );
  205. 				Console.ForegroundColor = ConsoleColor.Red;
  206. 				Console.Error.Write( "ERROR:\t" );
  207. 				Console.ForegroundColor = ConsoleColor.White;
  208. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  209. 				Console.ResetColor( );
  210. 			}
  211.  
  212. 			#endregion Error Message
  213.  
  214.  
  215. 			#region Help Text
  216.  
  217. 			/*
  218. 			ListIntCmd.exe,  Version 1.07
  219. 			List all available internal commands
  220.  
  221. 			Usage:  LISTINTCMD  [ "separator" ]  [ /C ]  [ /L[:logfile] ]
  222.  
  223. 			Where:  separator     is the character or string used to separate the
  224. 			                      command names in the output (default: linefeed)
  225. 			        /C            Copies output to clipboard
  226. 			        /L[:logfile]  Logs the entire search process to logfile
  227. 			                      (default log file name and location:
  228. 			                      ListIntCmd.log in program's parent folder)
  229.  
  230. 			Notes:  Use doublequotes if separator contains spaces or "special" characters.
  231. 			        separator accepts \n for linefeeds, \t for tabs, \/ for slashes,
  232. 			        \\ for backslashes, and """" for doublequotes.
  233. 			        Return code 2 if commands could not be retrieved and default list
  234. 			        had to be returned instead, 1 on other errors, 0 if all is well.
  235.  
  236. 			Written by Rob van der Woude
  237. 			https://www.robvanderwoude.com
  238. 			*/
  239.  
  240. 			#endregion Help Text
  241.  
  242.  
  243. 			#region Display Help Text
  244.  
  245. 			Console.Error.WriteLine( );
  246.  
  247. 			Console.Error.WriteLine( "ListIntCmd.exe,  Version {0}", progver );
  248.  
  249. 			Console.Error.WriteLine( "List all available internal commands" );
  250.  
  251. 			Console.Error.WriteLine( );
  252.  
  253. 			Console.Error.Write( "Usage:  " );
  254. 			Console.ForegroundColor = ConsoleColor.White;
  255. 			Console.Error.WriteLine( "LISTINTCMD  [ \"separator\" ]  [ /C ]  [ /L[:logfile] ]" );
  256. 			Console.ResetColor( );
  257.  
  258. 			Console.Error.WriteLine( );
  259.  
  260. 			Console.Error.Write( "Where:  " );
  261. 			Console.ForegroundColor = ConsoleColor.White;
  262. 			Console.Error.Write( "separator" );
  263. 			Console.ResetColor( );
  264. 			Console.Error.WriteLine( "     is the character or string used to separate the" );
  265.  
  266. 			Console.Error.WriteLine( "                      command names in the output (default: linefeed)" );
  267.  
  268. 			Console.ForegroundColor = ConsoleColor.White;
  269. 			Console.Error.Write( "        /C            C" );
  270. 			Console.ResetColor( );
  271. 			Console.Error.WriteLine( "opies output to clipboard" );
  272.  
  273. 			Console.ForegroundColor = ConsoleColor.White;
  274. 			Console.Error.Write( "        /L[:logfile]  L" );
  275. 			Console.ResetColor( );
  276. 			Console.Error.Write( "ogs the entire search process to " );
  277. 			Console.ForegroundColor = ConsoleColor.White;
  278. 			Console.Error.WriteLine( "logfile" );
  279. 			Console.ResetColor( );
  280.  
  281. 			Console.Error.WriteLine( "                      (default log file name and location:" );
  282.  
  283. 			Console.Error.WriteLine( "                      ListIntCmd.log in program's parent folder)" );
  284.  
  285. 			Console.Error.WriteLine( );
  286.  
  287. 			Console.Error.Write( "Notes:  Use doublequotes if " );
  288. 			Console.ForegroundColor = ConsoleColor.White;
  289. 			Console.Error.Write( "separator" );
  290. 			Console.ResetColor( );
  291. 			Console.Error.WriteLine( " contains spaces or \"special\" characters." );
  292.  
  293. 			Console.ForegroundColor = ConsoleColor.White;
  294. 			Console.Error.Write( "        separator" );
  295. 			Console.ResetColor( );
  296. 			Console.Error.Write( " accepts " );
  297. 			Console.ForegroundColor = ConsoleColor.White;
  298. 			Console.Error.Write( "\\n" );
  299. 			Console.ResetColor( );
  300. 			Console.Error.Write( " for linefeeds, " );
  301. 			Console.ForegroundColor = ConsoleColor.White;
  302. 			Console.Error.Write( "\\t" );
  303. 			Console.ResetColor( );
  304. 			Console.Error.Write( " for tabs, " );
  305. 			Console.ForegroundColor = ConsoleColor.White;
  306. 			Console.Error.Write( "\\/" );
  307. 			Console.ResetColor( );
  308. 			Console.Error.WriteLine( " for slashes," );
  309.  
  310. 			Console.ForegroundColor = ConsoleColor.White;
  311. 			Console.Error.Write( "        \\\\" );
  312. 			Console.ResetColor( );
  313. 			Console.Error.Write( " for backslashes, and " );
  314. 			Console.ForegroundColor = ConsoleColor.White;
  315. 			Console.Error.Write( "\"\"\"\"" );
  316. 			Console.ResetColor( );
  317. 			Console.Error.WriteLine( " for doublequotes." );
  318.  
  319. 			Console.Error.WriteLine( "        Return code 2 if commands could not be retrieved and default list" );
  320.  
  321. 			Console.Error.WriteLine( "        had to be returned instead, 1 on other errors, 0 if all is well." );
  322.  
  323. 			Console.Error.WriteLine( );
  324.  
  325. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  326.  
  327. 			Console.Error.WriteLine( "https://www.robvanderwoude.com" );
  328.  
  329. 			#endregion Display Help Text
  330.  
  331.  
  332. 			return 1;
  333. 		}
  334.  
  335. 		#endregion Error Handling
  336. 	}
  337. }

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