Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for tee.cs

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4.  
  5.  
  6. namespace RobvanderWoude
  7. {
  8. 	class Tee
  9. 	{
  10. 		static string progver = "1.03";
  11.  
  12.  
  13. 		static int Main( string[] args )
  14. 		{
  15. 			#region Command Line Parsing
  16.  
  17. 			string filename = String.Empty;
  18.  
  19. 			if ( args.Length == 1 || Console.IsInputRedirected )
  20. 			{
  21. 				if ( args[0].IndexOf( "?" ) > -1 )
  22. 				{
  23. 					return ShowHelp( );
  24. 				}
  25. 				filename = Path.GetFullPath( args[0] );
  26. 				if ( !Directory.Exists( Path.GetDirectoryName( filename ) ) )
  27. 				{
  28. 					return ShowHelp( "Invalid parent folder for output file" );
  29. 				}
  30. 				if ( Directory.Exists( filename ) )
  31. 				{
  32. 					return ShowHelp( "Please specify an output file name" );
  33. 				}
  34. 			}
  35. 			else
  36. 			{
  37. 				return ShowHelp( );
  38. 			}
  39.  
  40. 			#endregion
  41.  
  42. 			try
  43. 			{
  44. 				int inputn;
  45. 				string inputc;
  46.  
  47. 				StreamWriter file = new StreamWriter( filename, true );
  48.  
  49. 				do
  50. 				{
  51. 					inputn = Console.In.Read( );
  52. 					if ( inputn != -1 )
  53. 					{
  54. 						inputc = Convert.ToChar( Convert.ToByte( inputn ) ).ToString( );
  55. 						Console.Write( inputc );
  56. 						file.Write( inputc );
  57. 					}
  58. 				} while ( inputn != -1 );
  59.  
  60. 				file.Close( );
  61.  
  62. 				return 0;
  63. 			}
  64. 			catch ( Exception e )
  65. 			{
  66. 				return ShowHelp( e.Message );
  67. 			}
  68. 		}
  69.  
  70.  
  71. 		#region Error Handling
  72.  
  73. 		public static int ShowHelp( params string[] errmsg )
  74. 		{
  75. 			#region Error Message
  76.  
  77. 			if ( errmsg.Length > 0 )
  78. 			{
  79. 				List<string> errargs = new List<string>( errmsg );
  80. 				errargs.RemoveAt( 0 );
  81. 				Console.Error.WriteLine( );
  82. 				Console.ForegroundColor = ConsoleColor.Red;
  83. 				Console.Error.Write( "ERROR:\t" );
  84. 				Console.ForegroundColor = ConsoleColor.White;
  85. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  86. 				Console.ResetColor( );
  87. 			}
  88.  
  89. 			#endregion Error Message
  90.  
  91. 			#region Help Text
  92.  
  93. 			/*
  94. 			Tee,  Version 1.03
  95. 			Tee port for Windows: redirect Standard Input to Standard Output AND to a file
  96.  
  97. 			Usage:  somecommand  |  TEE.EXE  filename
  98.  
  99. 			Where:  somecommand     command whose output is piped into TEE's Standard Input
  100. 			        filename        the file that TEE's Standard Input will be appended to
  101.  
  102. 			Note:   This program requires .NET Framework 4.5; use an older version
  103. 			        of this program if you don't have .NET Framework 4.5 installed.
  104.  
  105. 			Written by Rob van der Woude
  106. 			http://www.robvanderwoude.com
  107. 			*/
  108.  
  109. 			Console.Error.WriteLine( );
  110.  
  111. 			Console.Error.WriteLine( "Tee,  Version {0}", progver );
  112.  
  113. 			Console.Error.WriteLine( "Tee port for Windows: redirect Standard Input to Standard Output AND to a file" );
  114.  
  115. 			Console.Error.WriteLine( );
  116.  
  117. 			Console.Error.Write( "Usage:  " );
  118. 			Console.ForegroundColor = ConsoleColor.White;
  119. 			Console.Error.WriteLine( "somecommand  |  TEE.EXE  filename" );
  120. 			Console.ResetColor( );
  121.  
  122. 			Console.Error.WriteLine( );
  123.  
  124. 			Console.Error.Write( "Where:  " );
  125. 			Console.ForegroundColor = ConsoleColor.White;
  126. 			Console.Error.Write( "somecommand" );
  127. 			Console.ResetColor( );
  128. 			Console.Error.WriteLine( "     command whose output is piped into TEE's Standard Input" );
  129.  
  130. 			Console.ForegroundColor = ConsoleColor.White;
  131. 			Console.Error.Write( "        filename" );
  132. 			Console.ResetColor( );
  133. 			Console.Error.WriteLine( "        the file that TEE's Standard Input will be appended to" );
  134.  
  135. 			Console.Error.WriteLine( );
  136.  
  137. 			Console.Error.WriteLine( "Note:   This program requires .NET Framework 4.5; use an older version" );
  138.  
  139. 			Console.Error.WriteLine( "        of this program if you don't have .NET Framework 4.5 installed." );
  140.  
  141. 			Console.Error.WriteLine( );
  142.  
  143. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  144.  
  145. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  146.  
  147. 			#endregion Help Text
  148.  
  149. 			return 1;
  150. 		}
  151.  
  152. 		#endregion
  153. 	}
  154. }
  155.  

page last modified: 2024-02-26; loaded in 0.0181 seconds