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.05";
  16.  
  17.  
  18. 		[STAThreadAttribute]
  19. 		static int Main( string[] args )
  20. 		{
  21. 			#region Initialize variables
  22.  
  23. 			int rc = 0;
  24. 			string separartor = "\n";
  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 WriteError( );
  51. 						case "/C":
  52. 							if ( copyset )
  53. 							{
  54. 								return WriteError( "Duplicate command line switch /C" );
  55. 							}
  56. 							copy = true;
  57. 							copyset = true;
  58. 							break;
  59. 						case "/L":
  60. 							if ( logset )
  61. 							{
  62. 								return WriteError( "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 WriteError( );
  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", "\n" ).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 WriteError( 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}\n", progver );
  103. 				logtext += String.Format( "{0}\n", Environment.OSVersion.VersionString );
  104. 				logtext += String.Format( "Search started at {0}\n\n", logbegin.ToString( "yyyy-MM-dd, HH:mm:ss.fff" ) );
  105. 				logtext += String.Format( "COMSPEC=\"{0}, ProductVersion={1}\"\n\n", comspec, FileVersionInfo.GetVersionInfo( comspec ).ProductVersion );
  106.  
  107. 				StreamReader file = new StreamReader( comspec, Encoding.ASCII );
  108. 				string content = file.ReadToEnd( );
  109. 				file.Close( );
  110.  
  111. 				List<string> intcmds = new List<string>( );
  112. 				//string excludestr = "ABOVENORMAL,AFFINITY,APPICON,BAT,BELOWNORMAL,CMD,CMDCMDLINE,CMDEXTVERSION,COM,COMSPEC,COPYCMD,COPYRIGHT,CRLF,CSVFS,DEFINED,DIRCMD,DISABLEDELAYEDEXPANSION,DISABLEEXTENSIONS,DLL,DO,ENABLEDELAYEDEXPANSION,ENABLEEXTENSIONS,ELSE,EOF,EQU,ERRORLEVEL,EXE,EXIST,FAT,FH,GEQ,GTR,HIGH,HIGHESTNUMANODENUMBER,HH,IDI,IN,INFO,JS,KERNEL,LEQ,LIST,LOW,LSS,MAX,MIN,MM,MSC,MUI,NEQ,NODE,NORMAL,NOT,NTDLL,NTFS,NY,OFF,ON,PATHEXT,RANDOM,REALTIME,REFS,SCRIPT,SEPARATE,SHARED,UNCC,US,VBS,VERSION,VS,WAIT,WA,WC,WD,WINDOWS,WP,WS,XCOPY";
  113. 				// extended exclusion string for Windows 2000 and XP; the longer the exclusion string, the higher the risk that some new future internal command will be omited in the output
  114. 				string excludestr = "ABOVENORMAL,AFFINITY,ANSI,APPICON,ASCII,AZ,BAT,BELOWNORMAL,BOTH,CMD,CMDCMDLINE,CMDEXTVERSION,COM,COMSPEC,CONFIG,COPYCMD,COPYRIGHT,CRLF,CSVFS,CTRL,CURRENT,DEFINED,DIRCMD,DISABLEDELAYEDEXPANSION,DISABLEEXTENSIONS,DLL,DO,DOC,DOS,DWORD,ENABLEDELAYEDEXPANSION,ENABLEEXTENSIONS,ELSE,ENTER,EOF,EQU,ERROR,ERRORLEVEL,EXE,EXIST,EXISTS,EXPAND,FALSE,FAT,FH,GEQ,GTR,GUI,HIGH,HIGHESTNUMANODENUMBER,HH,HKEY,HSM,IDI,IDLE,IN,INFO,IS,JS,KERNEL,LEQ,LIST,LNK,LOCAL,LOW,LSS,MACHINE,MAX,MIN,MM,MSC,MUI,NEQ,NODE,NORMAL,NOT,NT,NTDLL,NTFS,NY,NYA,OFF,ON,OTHER,PATHEXT,PROCESSING,RANDOM,REALTIME,REFS,REG,REGEDT,SCRIPT,SEPARATE,SHARED,STACK,SYS,SZ,TEMP,TWO,UNC,UNCC,UNKNOWN,US,USER,VAR,VBS,VERSION,VS,WAIT,WA,WC,WD,WINDOWS,WKERNEL,WORD,WP,WS,WV,XCOPY,XP";
  115. 				string[] excludearr = excludestr.Split( ",".ToCharArray( ) );
  116. 				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>( );
  117.  
  118. 				string pattern = @"([A-Z]\0){2,}";
  119. 				Regex regex = new Regex( pattern );
  120. 				if ( regex.IsMatch( content ) )
  121. 				{
  122. 					logtext += "List of regex matches:";
  123. 					foreach ( Match match in regex.Matches( content ) )
  124. 					{
  125. 						string line = String.Empty;
  126. 						string intcmd = match.ToString( ).Replace( "\0", String.Empty );
  127. 						line += String.Format( "\n{0,-24}", intcmd );
  128. 						if ( exclude.Contains( intcmd ) )
  129. 						{
  130. 							line += String.Format( "\texcluded by exlusion list", intcmd );
  131. 						}
  132. 						else if ( intcmds.Contains( intcmd ) )
  133. 						{
  134. 							line += String.Format( "\tskipped duplicate", intcmd );
  135. 						}
  136. 						else
  137. 						{
  138. 							intcmds.Add( intcmd );
  139. 							line += String.Format( "\tadded to the list of internal commands", intcmd );
  140. 						}
  141. 						logtext += line.TrimEnd( );
  142. 					}
  143. 					logtext += String.Format( "\n\nResults so far:\n\n{0}\n\n", String.Join( "\n", intcmds.ToArray( ) ) );
  144. 					intcmds.Sort( );
  145. 					logtext += String.Format( "\nResults after sorting:\n\n{0}\n\n", String.Join( "\n", intcmds.ToArray( ) ) );
  146. 				}
  147.  
  148. 				// Return a default list if we could not find the internal commands in %COMSPEC%
  149. 				if ( intcmds.Count == 0 )
  150. 				{
  151. 					logtext += "\nUsing default, hard-coded list\n\n";
  152. 					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";
  153. 					string[] defintcmdsarr = defintcmdsstr.Split( ",".ToCharArray( ), StringSplitOptions.RemoveEmptyEntries );
  154. 					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>( );
  155. 					rc = 2;
  156. 				}
  157.  
  158. 				string result = String.Join( separartor, intcmds.ToArray( ) );
  159.  
  160. 				Console.Write( result );
  161.  
  162. 				if ( copy )
  163. 				{
  164. 					logtext += "Writing result to clipboard . . .\n\n";
  165. 					Clipboard.SetText( result );
  166. 					logtext += String.Format( "Text written to clipboard:\n\n{0}\n\n", Clipboard.GetText( ) );
  167. 				}
  168.  
  169. 				if ( logging )
  170. 				{
  171. 					logend = DateTime.Now;
  172. 					TimeSpan duration = logend - logbegin;
  173. 					logtext += String.Format( "Search ended at {0} ({1:N0} milliseconds)\n", logend.ToString( "yyyy-MM-dd, HH:mm:ss.fff" ), duration.TotalMilliseconds );
  174. 					StreamWriter logstream = new StreamWriter( logfile );
  175. 					logstream.Write( logtext );
  176. 					logstream.Close( );
  177. 				}
  178.  
  179. 				return rc;
  180. 			}
  181. 			catch ( Exception e )
  182. 			{
  183. 				return WriteError( e.Message );
  184. 			}
  185. 		}
  186.  
  187.  
  188. 		#region Error Handling
  189.  
  190. 		/*
  191. 		ListIntCmd.exe,  Version 1.05
  192. 		List all available internal commands
  193.  
  194. 		Usage:  LISTINTCMD  [ "separator" ]  [ /C ]  [ /L[:logfile] ]
  195.  
  196. 		Where:  separator     is the character or string used to separate the
  197. 		                      command names in the output (default: linefeed)
  198. 		        /C            Copies output to clipboard
  199. 		        /L[:logfile]  Logs the entire search process to logfile
  200. 		                      (default log file name and location:
  201. 		                      ListIntCmd.log in program's parent folder)
  202.  
  203. 		Notes:  Use doublequotes if separator contains spaces or "special" characters.
  204. 		        separator accepts \n for linefeeds, \t for tabs, \/ for slashes,
  205. 		        \\ for backslashes, and """" for doublequotes.
  206. 		        Return code 2 if commands could not be retrieved and default list
  207. 		        had to be returned instead, 1 on other errors, 0 if all is well.
  208.  
  209. 		Written by Rob van der Woude
  210. 		http://www.robvanderwoude.com
  211. 		*/
  212.  
  213. 		public static int WriteError( string errormsg = null )
  214. 		{
  215. 			if ( !String.IsNullOrEmpty( errormsg ) )
  216. 			{
  217. 				Console.Error.WriteLine( );
  218. 				Console.ForegroundColor = ConsoleColor.Red;
  219. 				Console.Error.Write( "ERROR: " );
  220. 				Console.ForegroundColor = ConsoleColor.White;
  221. 				Console.Error.WriteLine( errormsg );
  222. 				Console.ResetColor( );
  223. 			}
  224.  
  225. 			Console.Error.WriteLine( );
  226.  
  227. 			Console.Error.WriteLine( "ListIntCmd.exe,  Version {0}", progver );
  228.  
  229. 			Console.Error.WriteLine( "List all available internal commands" );
  230.  
  231. 			Console.Error.WriteLine( );
  232.  
  233. 			Console.Error.Write( "Usage:  " );
  234. 			Console.ForegroundColor = ConsoleColor.White;
  235. 			Console.Error.WriteLine( "LISTINTCMD  [ \"separator\" ]  [ /C ]  [ /L[:logfile] ]" );
  236. 			Console.ResetColor( );
  237.  
  238. 			Console.Error.WriteLine( );
  239.  
  240. 			Console.Error.Write( "Where:  " );
  241. 			Console.ForegroundColor = ConsoleColor.White;
  242. 			Console.Error.Write( "separator" );
  243. 			Console.ResetColor( );
  244. 			Console.Error.WriteLine( "     is the character or string used to separate the" );
  245.  
  246. 			Console.Error.WriteLine( "                      command names in the output (default: linefeed)" );
  247.  
  248. 			Console.ForegroundColor = ConsoleColor.White;
  249. 			Console.Error.Write( "        /C            C" );
  250. 			Console.ResetColor( );
  251. 			Console.Error.WriteLine( "opies output to clipboard" );
  252.  
  253. 			Console.ForegroundColor = ConsoleColor.White;
  254. 			Console.Error.Write( "        /L[:logfile]  L" );
  255. 			Console.ResetColor( );
  256. 			Console.Error.Write( "ogs the entire search process to " );
  257. 			Console.ForegroundColor = ConsoleColor.White;
  258. 			Console.Error.WriteLine( "logfile" );
  259. 			Console.ResetColor( );
  260.  
  261. 			Console.Error.WriteLine( "                      (default log file name and location:" );
  262.  
  263. 			Console.Error.WriteLine( "                      ListIntCmd.log in program's parent folder)" );
  264.  
  265. 			Console.Error.WriteLine( );
  266.  
  267. 			Console.Error.Write( "Notes:  Use doublequotes if " );
  268. 			Console.ForegroundColor = ConsoleColor.White;
  269. 			Console.Error.Write( "separator" );
  270. 			Console.ResetColor( );
  271. 			Console.Error.WriteLine( " contains spaces or \"special\" characters." );
  272.  
  273. 			Console.ForegroundColor = ConsoleColor.White;
  274. 			Console.Error.Write( "        separator" );
  275. 			Console.ResetColor( );
  276. 			Console.Error.Write( " accepts " );
  277. 			Console.ForegroundColor = ConsoleColor.White;
  278. 			Console.Error.Write( "\\n" );
  279. 			Console.ResetColor( );
  280. 			Console.Error.Write( " for linefeeds, " );
  281. 			Console.ForegroundColor = ConsoleColor.White;
  282. 			Console.Error.Write( "\\t" );
  283. 			Console.ResetColor( );
  284. 			Console.Error.Write( " for tabs, " );
  285. 			Console.ForegroundColor = ConsoleColor.White;
  286. 			Console.Error.Write( "\\/" );
  287. 			Console.ResetColor( );
  288. 			Console.Error.WriteLine( " for slashes," );
  289.  
  290. 			Console.ForegroundColor = ConsoleColor.White;
  291. 			Console.Error.Write( "        \\\\" );
  292. 			Console.ResetColor( );
  293. 			Console.Error.Write( " for backslashes, and " );
  294. 			Console.ForegroundColor = ConsoleColor.White;
  295. 			Console.Error.Write( "\"\"\"\"" );
  296. 			Console.ResetColor( );
  297. 			Console.Error.WriteLine( " for doublequotes." );
  298.  
  299. 			Console.Error.WriteLine( "        Return code 2 if commands could not be retrieved and default list" );
  300.  
  301. 			Console.Error.WriteLine( "        had to be returned instead, 1 on other errors, 0 if all is well." );
  302.  
  303. 			Console.Error.WriteLine( );
  304.  
  305. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  306.  
  307. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  308. 			return 1;
  309. 		}
  310.  
  311. 		#endregion Error Handling
  312. 	}
  313. }
  314.  

page last uploaded: 2017-04-06, 13:33