Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for trash.cs

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Microsoft.VisualBasic.FileIO;
  8.  
  9.  
  10. namespace RobvanderWoude
  11. {
  12. 	internal class Trash
  13. 	{
  14. 		static readonly string progver = "1.00";
  15.  
  16.  
  17. 		static int Main( string[] args )
  18. 		{
  19. 			UIOption dialogs = UIOption.OnlyErrorDialogs;
  20.  
  21. 			if ( args.Length == 0 || args.Contains( "/?" ) )
  22. 			{
  23. 				return ShowHelp( );
  24. 			}
  25.  
  26. 			foreach ( string arg in args )
  27. 			{
  28. 				if ( arg.ToUpper( ) == "/V" )
  29. 				{
  30. 					dialogs = UIOption.AllDialogs;
  31. 				}
  32. 				else if ( !File.Exists( arg ) )
  33. 				{
  34. 					return ShowHelp( "File \"{0}\" not found", arg );
  35. 				}
  36. 			}
  37.  
  38. 			foreach ( string arg in args )
  39. 			{
  40. 				if ( arg.ToUpper( ) != "/V" )
  41. 				{
  42. 					try
  43. 					{
  44. 						FileSystem.DeleteFile( arg, dialogs, RecycleOption.SendToRecycleBin );
  45. 					}
  46. 					catch ( Exception e )
  47. 					{
  48. 						return ShowHelp( e.Message );
  49. 					}
  50. 				}
  51. 			}
  52.  
  53. 			return 0;
  54. 		}
  55.  
  56.  
  57. 		public static int ShowHelp( params string[] errmsg )
  58. 		{
  59. 			#region Error Message
  60.  
  61. 			if ( errmsg.Length > 0 )
  62. 			{
  63. 				List<string> errargs = new List<string>( errmsg );
  64. 				errargs.RemoveAt( 0 );
  65. 				Console.Error.WriteLine( );
  66. 				Console.ForegroundColor = ConsoleColor.Red;
  67. 				Console.Error.Write( "ERROR:\t" );
  68. 				Console.ForegroundColor = ConsoleColor.White;
  69. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  70. 				Console.ResetColor( );
  71. 			}
  72.  
  73. 			#endregion Error Message
  74.  
  75.  
  76. 			#region Help Text
  77.  
  78. 			/*
  79. 			Trash.exe,  Version 1.00
  80. 			Send specified file(s) to the recycle bin
  81.  
  82. 			Usage:    TRASH.EXE  filespec  [ filespec  [ ... ] ] ]  [ /V ]
  83.  
  84. 			Where:    filespec   file to be sent to the recycle bin (no wildcards)
  85. 			          /V         Verbose output, show progress and error dialogs
  86. 			                     (default: error dialogs only)
  87.  
  88. 			Written by Rob van der Woude
  89. 			https://www.robvanderwoude.com
  90. 			*/
  91.  
  92. 			#endregion Help Text
  93.  
  94.  
  95. 			#region Display Help Text
  96.  
  97. 			Console.Error.WriteLine( );
  98.  
  99. 			Console.Error.WriteLine( "Trash.exe,  Version {0}", progver );
  100.  
  101. 			Console.Error.WriteLine( "Send specified file(s) to the recycle bin" );
  102.  
  103. 			Console.Error.WriteLine( );
  104.  
  105. 			Console.Error.Write( "Usage:    " );
  106. 			Console.ForegroundColor = ConsoleColor.White;
  107. 			Console.Error.WriteLine( "TRASH.EXE  filespec  [ filespec  [ ... ] ] ]  [ /V ]" );
  108. 			Console.ResetColor( );
  109.  
  110. 			Console.Error.WriteLine( );
  111.  
  112. 			Console.Error.Write( "Where:    " );
  113. 			Console.ForegroundColor = ConsoleColor.White;
  114. 			Console.Error.Write( "filespec" );
  115. 			Console.ResetColor( );
  116. 			Console.Error.WriteLine( "   file to be sent to the recycle bin (no wildcards)" );
  117.  
  118. 			Console.ForegroundColor = ConsoleColor.White;
  119. 			Console.Error.Write( "          /V         V" );
  120. 			Console.ResetColor( );
  121. 			Console.Error.WriteLine( "erbose output, show progress and error dialogs" );
  122.  
  123. 			Console.Error.WriteLine( "                     (default: error dialogs only)" );
  124.  
  125. 			Console.Error.WriteLine( );
  126.  
  127. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  128.  
  129. 			Console.Error.WriteLine( "https://www.robvanderwoude.com" );
  130.  
  131. 			#endregion Display Help Text
  132.  
  133. 			return -1;
  134. 		}
  135. 	}
  136. }

page last modified: 2022-10-20