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 Microsoft.VisualBasic.FileIO;
  6.  
  7.  
  8. namespace RobvanderWoude
  9. {
  10. 	internal class Trash
  11. 	{
  12. 		static readonly string progver = "1.01";
  13.  
  14.  
  15. 		static int Main( string[] args )
  16. 		{
  17. 			bool continueonerrors = false;
  18. 			bool debugmode = false;
  19. 			bool verbose = false;
  20. 			UIOption dialogs = UIOption.OnlyErrorDialogs;
  21. 			int rc = 0;
  22. 			int deleted = 0;
  23. 			int failed = 0;
  24.  
  25. 			if ( args.Length == 0 || args.Contains( "/?" ) )
  26. 			{
  27. 				return ShowHelp( );
  28. 			}
  29.  
  30. 			foreach ( string arg in args )
  31. 			{
  32. 				if ( arg.ToUpper( ) == "/C" )
  33. 				{
  34. 					if ( continueonerrors )
  35. 					{
  36. 						return ShowHelp( "Duplicate command line switch /C" );
  37. 					}
  38. 					continueonerrors = true;
  39. 				}
  40. 				else if ( arg.ToUpper( ) == "/D" )
  41. 				{
  42. 					if ( debugmode )
  43. 					{
  44. 						return ShowHelp( "Duplicate command line switch /D" );
  45. 					}
  46. 					debugmode = true;
  47. 				}
  48. 				else if ( arg.ToUpper( ) == "/V" )
  49. 				{
  50. 					if ( verbose )
  51. 					{
  52. 						return ShowHelp( "Duplicate command line switch /V" );
  53. 					}
  54. 					verbose = true;
  55. 					dialogs = UIOption.AllDialogs;
  56. 				}
  57. 				else if ( arg[0] == '/' )
  58. 				{
  59. 					return ShowHelp( "Invalid command line switch {0}", arg );
  60. 				}
  61. 				else if ( !Directory.Exists( Directory.GetParent( arg ).FullName ) )
  62. 				{
  63. 					if ( continueonerrors )
  64. 					{
  65. 						rc = 1;
  66. 					}
  67. 					else
  68. 					{
  69. 						return ShowHelp( "File \"{0}\" not found", arg );
  70. 					}
  71. 				}
  72. 			}
  73.  
  74. 			verbose = verbose || debugmode;
  75. 			List<string> filestodelete = new List<string>( );
  76.  
  77. 			foreach ( string arg in args )
  78. 			{
  79. 				if ( arg[0] != '/' )
  80. 				{
  81. 					DirectoryInfo directory = Directory.GetParent( arg );
  82. 					FileInfo[] files = directory.GetFiles( Path.GetFileName( arg ) );
  83. 					foreach ( FileInfo file in files )
  84. 					{
  85. 						filestodelete.Add( file.FullName );
  86. 					}
  87. 				}
  88. 			}
  89.  
  90. 			if ( filestodelete.Count == 0 )
  91. 			{
  92. 				if ( continueonerrors )
  93. 				{
  94. 					rc = 1;
  95. 					Console.WriteLine( "No files to delete" );
  96. 				}
  97. 				else
  98. 				{
  99. 					return ShowHelp( "No files to delete" );
  100. 				}
  101. 			}
  102.  
  103. 			foreach ( string file in filestodelete )
  104. 			{
  105. 				try
  106. 				{
  107. 					if ( verbose )
  108. 					{
  109. 						Console.Write( "Deleting \"{0}\" . . . ", file );
  110. 					}
  111. 					if ( !debugmode )
  112. 					{
  113. 						FileSystem.DeleteFile( file, dialogs, RecycleOption.SendToRecycleBin );
  114. 					}
  115. 					if ( verbose )
  116. 					{
  117. 						Console.ForegroundColor = ConsoleColor.Green;
  118. 						Console.WriteLine( "success" );
  119. 						Console.ResetColor( );
  120. 					}
  121. 					deleted++;
  122. 				}
  123. 				catch ( Exception e )
  124. 				{
  125. 					if ( continueonerrors )
  126. 					{
  127. 						rc = 1;
  128. 						if ( verbose )
  129. 						{
  130. 							Console.ForegroundColor = ConsoleColor.Red;
  131. 							Console.WriteLine( "failed" );
  132. 							Console.ResetColor( );
  133. 						}
  134. 						failed++;
  135. 					}
  136. 					else
  137. 					{
  138. 						return ShowHelp( e.Message );
  139. 					}
  140. 				}
  141. 			}
  142.  
  143. 			if ( verbose )
  144. 			{
  145. 				if ( deleted == 1 )
  146. 				{
  147. 					Console.Write( "\nSuccessfully deleted 1 file, " );
  148. 				}
  149. 				else
  150. 				{
  151. 					Console.Write( "\nSuccessfully deleted {0} files, ", deleted );
  152. 				}
  153. 				if ( failed == 1 )
  154. 				{
  155. 					Console.WriteLine( "1 file failed" );
  156. 				}
  157. 				else
  158. 				{
  159. 					Console.WriteLine( "{0} files failed", failed );
  160. 				}
  161. 			}
  162.  
  163. 			return rc;
  164. 		}
  165.  
  166.  
  167. 		public static int ShowHelp( params string[] errmsg )
  168. 		{
  169. 			#region Error Message
  170.  
  171. 			if ( errmsg.Length > 0 )
  172. 			{
  173. 				List<string> errargs = new List<string>( errmsg );
  174. 				errargs.RemoveAt( 0 );
  175. 				Console.Error.WriteLine( );
  176. 				Console.ForegroundColor = ConsoleColor.Red;
  177. 				Console.Error.Write( "ERROR:\t" );
  178. 				Console.ForegroundColor = ConsoleColor.White;
  179. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  180. 				Console.ResetColor( );
  181. 			}
  182.  
  183. 			#endregion Error Message
  184.  
  185.  
  186. 			#region Help Text
  187.  
  188. 			/*
  189. 			Trash.exe,  Version 1.01
  190. 			Send specified file(s) to the recycle bin
  191.  
  192. 			Usage:    TRASH.EXE  filespec  [ filespec  [ ... ] ] ]  [ options ]
  193.  
  194. 			Where:    filespec   file(s) to be sent to the recycle bin (wildcards allowed)
  195.  
  196. 			Options:  /C         Continue on errors (default: abort on first error)
  197. 			          /D         Debug mode: files are listed only, not deleted
  198. 			          /V         Verbose output: show file name(s), progress and error
  199. 			                     dialogs, and summary (default: error dialogs only)
  200.  
  201. 			Note:     Return code ("ErrorLevel"):
  202. 			          * successful deletion of all files specified:                             0
  203. 			          * invalid directory, failure to delete or no matching file (with    /C):  1
  204. 			          * invalid directory, failure to delete or no matching file (without /C): -1
  205. 			          * invalid command line argument:                                         -1
  206.  
  207. 			Written by Rob van der Woude
  208. 			https://www.robvanderwoude.com
  209. 			*/
  210.  
  211. 			#endregion Help Text
  212.  
  213.  
  214. 			#region Display Help Text
  215.  
  216. 			Console.Error.WriteLine( );
  217.  
  218. 			Console.Error.WriteLine( "Trash.exe,  Version {0}", progver );
  219.  
  220. 			Console.Error.WriteLine( "Send specified file(s) to the recycle bin" );
  221.  
  222. 			Console.Error.WriteLine( );
  223.  
  224. 			Console.Error.Write( "Usage:    " );
  225. 			Console.ForegroundColor = ConsoleColor.White;
  226. 			Console.Error.WriteLine( "TRASH.EXE  filespec  [ filespec  [ ... ] ] ]  [ /V ]" );
  227. 			Console.ResetColor( );
  228.  
  229. 			Console.Error.WriteLine( );
  230.  
  231. 			Console.Error.Write( "Where:    " );
  232. 			Console.ForegroundColor = ConsoleColor.White;
  233. 			Console.Error.Write( "filespec" );
  234. 			Console.ResetColor( );
  235. 			Console.Error.WriteLine( "   file(s) to be sent to the recycle bin (wildcards allowed)" );
  236.  
  237. 			Console.Error.WriteLine( );
  238.  
  239. 			Console.Error.Write( "Options:  " );
  240. 			Console.ForegroundColor = ConsoleColor.White;
  241. 			Console.Error.Write( "/C         C" );
  242. 			Console.ResetColor( );
  243. 			Console.Error.WriteLine( "ontinue on errors (default: abort on first error)" );
  244.  
  245. 			Console.ForegroundColor = ConsoleColor.White;
  246. 			Console.Error.Write( "          /D         D" );
  247. 			Console.ResetColor( );
  248. 			Console.Error.WriteLine( "ebug mode: files are listed only, not deleted" );
  249.  
  250. 			Console.ForegroundColor = ConsoleColor.White;
  251. 			Console.Error.Write( "          /V         V" );
  252. 			Console.ResetColor( );
  253. 			Console.Error.WriteLine( "erbose output: show file name(s), progress and error" );
  254.  
  255. 			Console.Error.WriteLine( "                     dialogs, and summary (default: error dialogs only)" );
  256.  
  257. 			Console.Error.WriteLine( );
  258.  
  259. 			Console.Error.WriteLine( "Note:     Return code (\"ErrorLevel\"):" );
  260.  
  261. 			Console.Error.WriteLine( "          * successful deletion of all files specified:                             0" );
  262.  
  263. 			Console.Error.Write( "          * invalid directory, failure to delete or no matching file (with    " );
  264. 			Console.ForegroundColor = ConsoleColor.White;
  265. 			Console.Error.Write( "/C" );
  266. 			Console.ResetColor( );
  267. 			Console.Error.WriteLine( "):  1" );
  268.  
  269. 			Console.Error.Write( "          * invalid directory, failure to delete or no matching file (without " );
  270. 			Console.ForegroundColor = ConsoleColor.White;
  271. 			Console.Error.Write( "/C" );
  272. 			Console.ResetColor( );
  273. 			Console.Error.WriteLine( "): -1" );
  274.  
  275. 			Console.Error.WriteLine( "          * invalid command line argument:                                         -1" );
  276.  
  277. 			Console.Error.WriteLine( );
  278.  
  279. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  280.  
  281. 			Console.Error.WriteLine( "https://www.robvanderwoude.com" );
  282.  
  283. 			#endregion Display Help Text
  284.  
  285.  
  286. 			return -1;
  287. 		}
  288. 	}
  289. }

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