Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for newtempfile.cs

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

  1. using System;
  2. using System.IO;
  3.  
  4. namespace RobvandeWoude
  5. {
  6. 	class NewTempFile
  7. 	{
  8. 		static int Main( string[] args )
  9. 		{
  10. 			if ( args.Length > 0 )
  11. 			{
  12. 				return WriteError( string.Empty );
  13. 			}
  14.  
  15. 			try
  16. 			{
  17. 				Console.WriteLine( Path.GetTempFileName( ) );
  18. 				return 0;
  19. 			}
  20. 			catch ( Exception e )
  21. 			{
  22. 				return WriteError( e.Message );
  23. 			}
  24. 		}
  25.  
  26. 		public static int WriteError( string errorMessage )
  27. 		{
  28. 			/*
  29. 				NewTempFile,  Version 1.00
  30. 				Create a zero-byte temporary file and display its name on screen
  31.  
  32. 				Usage:    NEWTEMPFILE
  33.  
  34. 				Example:  In a batch file, use the following code to "capture" the name:
  35.  
  36. 				          FOR /F "tokens=*" %%A IN ('NEWTEMPFILE.EXE') DO SET TempFile="%%~A"
  37.  
  38. 				Written by Rob van der Woude
  39. 				http://www.robvanderwoude.com
  40. 			 */
  41. 			if ( string.IsNullOrEmpty( errorMessage ) == false )
  42. 			{
  43. 				Console.Error.WriteLine( );
  44. 				Console.ForegroundColor = ConsoleColor.Red;
  45. 				Console.Error.Write( "ERROR: " );
  46. 				Console.ForegroundColor = ConsoleColor.White;
  47. 				Console.Error.WriteLine( errorMessage );
  48. 				Console.ResetColor( );
  49. 			}
  50.  
  51. 			Console.Error.WriteLine( );
  52. 			Console.Error.WriteLine( "NewTempFile,  Version 1.00" );
  53. 			Console.Error.WriteLine( "Create a zero-byte temporary file and display its name on screen" );
  54. 			Console.Error.WriteLine( );
  55. 			Console.Error.Write( "Usage:    " );
  56. 			Console.ForegroundColor = ConsoleColor.White;
  57. 			Console.Error.WriteLine( "NEWTEMPFILE" );
  58. 			Console.ResetColor( );
  59. 			Console.Error.WriteLine( );
  60. 			Console.Error.WriteLine( "Example:  In a batch file, use the following code to \"capture\" the name:" );
  61. 			Console.Error.WriteLine( );
  62. 			Console.ForegroundColor = ConsoleColor.White;
  63. 			Console.Error.WriteLine( "          FOR /F \"tokens=*\" %%A IN ('NEWTEMPFILE.EXE') DO SET TempFile=\"%%~A\"" );
  64. 			Console.ResetColor( );
  65. 			Console.Error.WriteLine( );
  66. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  67. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  68. 			return 1;
  69. 		}
  70. 	}
  71. }
  72.  

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