Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for deltrash.cs

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Runtime.InteropServices;
  5.  
  6.  
  7. namespace RobvanderWoude
  8. {
  9. 	class DelTrash
  10. 	{
  11. 		static string progver = "1.03";
  12.  
  13.  
  14. 		static int Main( string[] args )
  15. 		{
  16. 			#region Initialize Variables
  17.  
  18. 			int rc = 0;
  19. 			long totalsize = 0;
  20. 			int deleteditems = 0;
  21. 			int accessdenieddirs = 0;
  22. 			bool accessdenied = false;
  23. 			bool confirm = false;
  24. 			bool listonly = false;
  25. 			bool progress = false;
  26. 			bool quiet = false;
  27. 			bool verbose = false;
  28. 			RecycleFlags flags = RecycleFlags.RecycleNoSound;
  29. 			string drive = null; // default: All drives
  30. 			string windrive = Path.GetPathRoot( Environment.SystemDirectory );
  31.  
  32. 			#endregion Initialize Variables
  33.  
  34.  
  35. 			#region Parse Command Line
  36.  
  37. 			if ( args.Length > 3 )
  38. 			{
  39. 				return ShowHelp( );
  40. 			}
  41.  
  42. 			foreach ( string arg in args )
  43. 			{
  44. 				switch ( arg.ToUpper( ) )
  45. 				{
  46. 					case "/?":
  47. 						return ShowHelp( );
  48. 					case "/C":
  49. 						if ( confirm )
  50. 						{
  51. 							return ShowHelp( "Duplicate command line switch /C" );
  52. 						}
  53. 						confirm = true;
  54. 						break;
  55. 					case "/L":
  56. 						if ( listonly )
  57. 						{
  58. 							return ShowHelp( "Duplicate command line switch /L" );
  59. 						}
  60. 						listonly = true;
  61. 						break;
  62. 					case "/P":
  63. 						if ( progress )
  64. 						{
  65. 							return ShowHelp( "Duplicate command line switch /P" );
  66. 						}
  67. 						progress = true;
  68. 						break;
  69. 					case "/Q":
  70. 						if ( quiet )
  71. 						{
  72. 							return ShowHelp( "Duplicate command line switch /Q" );
  73. 						}
  74. 						if ( verbose )
  75. 						{
  76. 							return ShowHelp( "Switches /V and /Q are mutually exclusive" );
  77. 						}
  78. 						verbose = true;
  79. 						break;
  80. 					case "/V":
  81. 						if ( verbose )
  82. 						{
  83. 							return ShowHelp( "Duplicate command line switch /V" );
  84. 						}
  85. 						if ( quiet )
  86. 						{
  87. 							return ShowHelp( "Switches /V and /Q are mutually exclusive" );
  88. 						}
  89. 						verbose = true;
  90. 						break;
  91. 					case "/W":
  92. 						if ( drive != null )
  93. 						{
  94. 							if ( drive == windrive )
  95. 							{
  96. 								return ShowHelp( "Either specify a drive letter or use the /W switch, not both" );
  97. 							}
  98. 							else
  99. 							{
  100. 								return ShowHelp( "Duplicate drive specification {0} and {1}", drive, arg.ToUpper( ) );
  101. 							}
  102. 						}
  103. 						drive = windrive;
  104. 						break;
  105. 					default:
  106. 						bool validdrive = false;
  107. 						DriveInfo[] alldrives = DriveInfo.GetDrives( );
  108. 						foreach ( DriveInfo drvinf in alldrives )
  109. 						{
  110. 							if ( drvinf.Name == arg.ToUpper( ) + "\\" )
  111. 							{
  112. 								validdrive = true;
  113. 								drive = arg.ToUpper( );
  114. 							}
  115. 						}
  116. 						if ( !validdrive )
  117. 						{
  118. 							return ShowHelp( "Invalid command line argument \"{0}\"", arg );
  119. 						}
  120. 						break;
  121. 				}
  122. 			}
  123.  
  124. 			verbose = verbose || listonly;
  125.  
  126. 			if ( !confirm )
  127. 			{
  128. 				flags |= RecycleFlags.RecycleNoConfirmation;
  129. 			}
  130. 			if ( !progress )
  131. 			{
  132. 				flags |= RecycleFlags.RecycleNoProgressUI;
  133. 			}
  134.  
  135. 			#endregion Parse Command Line
  136.  
  137.  
  138. 			#region Verbose Output
  139.  
  140. 			if ( verbose )
  141. 			{
  142. 				foreach ( DriveInfo diskdrive in DriveInfo.GetDrives( ) )
  143. 				{
  144. 					if ( diskdrive.DriveType == DriveType.Fixed ) // local harddisks only
  145. 					{
  146. 						string recyclebin = Path.Combine( diskdrive.Name, "$RECYCLE.BIN" );
  147. 						if ( Directory.Exists( recyclebin ) )
  148. 						{
  149. 							long size = 0;
  150. 							int items = 0;
  151. 							string[] files = new string[0];
  152. 							string[] folders = Directory.GetDirectories( recyclebin );
  153. 							foreach ( string folder in folders )
  154. 							{
  155. 								try
  156. 								{
  157. 									// assuming any access denied will occur on the first directory level
  158. 									files = Directory.GetFiles( folder, "*.*", SearchOption.AllDirectories );
  159. 									foreach ( string file in files )
  160. 									{
  161. 										size += new FileInfo( file ).Length;
  162. 										items++;
  163. 									}
  164. 								}
  165. 								catch ( UnauthorizedAccessException )
  166. 								{
  167. 									accessdenied = true;
  168. 									accessdenieddirs++;
  169. 								}
  170. 							}
  171. 							if ( items == 1 )
  172. 							{
  173. 								Console.WriteLine( "Marking 1 file ({1}) from drive {2} for deletion", items, FormatBytes( size ), diskdrive.Name.Remove( 2 ) );
  174. 								totalsize += size;
  175. 								deleteditems += items;
  176. 							}
  177. 							else if ( items > 1 )
  178. 							{
  179. 								Console.WriteLine( "Marking {0} files ({1}) from drive {2} for deletion", items, FormatBytes( size ), diskdrive.Name.Remove( 2 ) );
  180. 								totalsize += size;
  181. 								deleteditems += items;
  182. 							}
  183. 						}
  184. 					}
  185. 				}
  186. 			}
  187.  
  188. 			#endregion Verbose Output
  189.  
  190.  
  191. 			#region Actual Deletion
  192.  
  193. 			if ( listonly )
  194. 			{
  195. 				Console.WriteLine( "\n{0} items with a total size of {1} would have been deleted.", deleteditems, FormatBytes( totalsize ) );
  196. 			}
  197. 			else
  198. 			{
  199. 				uint result = SHEmptyRecycleBin( IntPtr.Zero, drive, flags );
  200. 				if ( result == 0 )
  201. 				{
  202. 					if ( !quiet )
  203. 					{
  204. 						Console.WriteLine( "\n{0} items with a total size of {1} have been permanently deleted.", deleteditems, FormatBytes( totalsize ) );
  205. 						if ( accessdenied )
  206. 						{
  207. 							if ( accessdenieddirs == 1 )
  208. 							{
  209. 								Console.WriteLine( "Not all items could be removed permanently because access was denied to 1 directory." );
  210. 							}
  211. 							else
  212. 							{
  213. 								Console.WriteLine( "Not all items could be removed permanently because access was denied to {0} directories.", accessdenieddirs );
  214. 							}
  215. 						}
  216. 					}
  217. 					rc = 0;
  218. 				}
  219. 				else
  220. 				{
  221. 					if ( !quiet )
  222. 					{
  223. 						Console.WriteLine( "\nNot all items could be removed permanently." );
  224. 					}
  225. 					rc = 1;
  226. 				}
  227. 			}
  228.  
  229. 			#endregion Actual Deletion
  230.  
  231.  
  232. 			return rc;
  233. 		}
  234.  
  235.  
  236. 		static string FormatBytes( long bytes )
  237. 		{
  238. 			int KB = 1024;
  239. 			int MB = 1024 * KB;
  240. 			int GB = 1024 * MB;
  241. 			if ( bytes < KB )
  242. 			{
  243. 				return bytes.ToString( ) + " B";
  244. 			}
  245. 			else if ( bytes < MB )
  246. 			{
  247. 				return ( bytes / KB ).ToString( ) + " KB";
  248. 			}
  249. 			else if ( bytes < GB )
  250. 			{
  251. 				return ( bytes / MB ).ToString( ) + " MB";
  252. 			}
  253. 			else
  254. 			{
  255. 				return ( bytes / GB ).ToString( ) + " GB";
  256. 			}
  257. 		}
  258.  
  259.  
  260. 		static int ShowHelp( params string[] errmsg )
  261. 		{
  262. 			#region Error Message
  263.  
  264. 			if ( errmsg.Length > 0 )
  265. 			{
  266. 				List<string> errargs = new List<string>( errmsg );
  267. 				errargs.RemoveAt( 0 );
  268. 				Console.Error.WriteLine( );
  269. 				Console.ForegroundColor = ConsoleColor.Red;
  270. 				Console.Error.Write( "ERROR:\t" );
  271. 				Console.ForegroundColor = ConsoleColor.White;
  272. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  273. 				Console.ResetColor( );
  274. 			}
  275.  
  276. 			#endregion Error Message
  277.  
  278.  
  279. 			#region Help Text
  280.  
  281. 			/*
  282. 			DelTrash.exe,  Version 1.03
  283. 			Empty the Recycle Bin
  284.  
  285. 			Usage:   DelTrash.exe  [ drive: | /W ]  [ /C ]  [ /P ]  [ /L | /Q | /V ]
  286.  
  287. 			Where:   drive:  empty recycle bin on this drive only (default: all drives)
  288. 			         /W      empty recycle bin on Windows' drive only (default: all drives)
  289. 			         /C      prompt for Confirmation
  290. 			         /L      List only, do not delete
  291. 			         /P      show Progress bar
  292. 			         /Q      Quiet mode, no screen output
  293. 			         /V      Verbose mode, show number of files and total size per disk drive
  294.  
  295. 			Notes:   Switches /L and /Q and /V are mutually exclusive.
  296. 			         Return code is 0 if the Recycle Bin was emptied successfully, or 1
  297. 			         if there was nothing to delete or in case of (command line) errors.
  298.  
  299. 			Credits: Based on code by Vinoth Kumar
  300. 			         www.codeproject.com/Articles/20172/Empty-the-Recycle-Bin-using-C
  301. 			         Number of items and size calculation based on 
  302.  
  303. 			Written by Rob van der Woude
  304. 			https://www.robvanderwoude.com
  305. 			*/
  306.  
  307. 			#endregion Help Text
  308.  
  309.  
  310. 			#region Display Help Text
  311.  
  312. 			Console.Error.WriteLine( );
  313.  
  314. 			Console.Error.WriteLine( "DelTrash.exe,  Version {0}", progver );
  315.  
  316. 			Console.Error.WriteLine( "Empty the Recycle Bin" );
  317.  
  318. 			Console.Error.WriteLine( );
  319.  
  320. 			Console.Error.Write( "Usage:   " );
  321. 			Console.ForegroundColor = ConsoleColor.White;
  322. 			Console.Error.WriteLine( "DelTrash.exe  [ drive: | /W ]  [ /C ]  [ /P ]  [ /L | /Q | /V ]" );
  323. 			Console.ResetColor( );
  324.  
  325. 			Console.Error.WriteLine( );
  326.  
  327. 			Console.Error.Write( "Where:   " );
  328. 			Console.ForegroundColor = ConsoleColor.White;
  329. 			Console.Error.Write( "drive:" );
  330. 			Console.ResetColor( );
  331. 			Console.Error.WriteLine( "  empty recycle bin on this drive only (default: all drives)" );
  332.  
  333. 			Console.ForegroundColor = ConsoleColor.White;
  334. 			Console.Error.Write( "         /W" );
  335. 			Console.ResetColor( );
  336. 			Console.Error.Write( "      empty recycle bin on " );
  337. 			Console.ForegroundColor = ConsoleColor.White;
  338. 			Console.Error.Write( "W" );
  339. 			Console.ResetColor( );
  340. 			Console.Error.WriteLine( "indows' drive only (default: all drives)" );
  341.  
  342. 			Console.ForegroundColor = ConsoleColor.White;
  343. 			Console.Error.Write( "         /C" );
  344. 			Console.ResetColor( );
  345. 			Console.Error.Write( "      prompt for " );
  346. 			Console.ForegroundColor = ConsoleColor.White;
  347. 			Console.Error.Write( "C" );
  348. 			Console.ResetColor( );
  349. 			Console.Error.WriteLine( "onfirmation" );
  350.  
  351. 			Console.ForegroundColor = ConsoleColor.White;
  352. 			Console.Error.Write( "         /L      L" );
  353. 			Console.ResetColor( );
  354. 			Console.Error.WriteLine( "ist only, do not delete" );
  355.  
  356. 			Console.ForegroundColor = ConsoleColor.White;
  357. 			Console.Error.Write( "         /P" );
  358. 			Console.ResetColor( );
  359. 			Console.Error.Write( "      show " );
  360. 			Console.ForegroundColor = ConsoleColor.White;
  361. 			Console.Error.Write( "P" );
  362. 			Console.ResetColor( );
  363. 			Console.Error.WriteLine( "rogress bar" );
  364.  
  365. 			Console.ForegroundColor = ConsoleColor.White;
  366. 			Console.Error.Write( "         /Q      Q" );
  367. 			Console.ResetColor( );
  368. 			Console.Error.WriteLine( "uiet mode, no screen output" );
  369.  
  370. 			Console.ForegroundColor = ConsoleColor.White;
  371. 			Console.Error.Write( "         /V      V" );
  372. 			Console.ResetColor( );
  373. 			Console.Error.WriteLine( "erbose mode, show number of files and total size per disk drive" );
  374.  
  375. 			Console.Error.WriteLine( );
  376.  
  377. 			Console.Error.Write( "Notes:   Switches " );
  378. 			Console.ForegroundColor = ConsoleColor.White;
  379. 			Console.Error.Write( "/L" );
  380. 			Console.ResetColor( );
  381. 			Console.Error.Write( " and " );
  382. 			Console.ForegroundColor = ConsoleColor.White;
  383. 			Console.Error.Write( "/Q" );
  384. 			Console.ResetColor( );
  385. 			Console.Error.Write( " and " );
  386. 			Console.ForegroundColor = ConsoleColor.White;
  387. 			Console.Error.Write( "/V" );
  388. 			Console.ResetColor( );
  389. 			Console.Error.WriteLine( " are mutually exclusive." );
  390.  
  391. 			Console.Error.WriteLine( "         Return code is 0 if the Recycle Bin was emptied successfully, or 1" );
  392.  
  393. 			Console.Error.WriteLine( "         if there was nothing to delete or in case of (command line) errors." );
  394.  
  395. 			Console.Error.WriteLine( );
  396.  
  397. 			Console.Error.WriteLine( "Credits: Based on code by Vinoth Kumar" );
  398.  
  399. 			Console.ForegroundColor = ConsoleColor.DarkGray;
  400. 			Console.Error.WriteLine( "         www.codeproject.com/Articles/20172/Empty-the-Recycle-Bin-using-C" );
  401. 			Console.ResetColor( );
  402.  
  403. 			Console.Error.WriteLine( );
  404.  
  405. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  406.  
  407. 			Console.Error.WriteLine( "https://www.robvanderwoude.com" );
  408.  
  409. 			#endregion Display Help Text
  410.  
  411.  
  412. 			return 1;
  413. 		}
  414.  
  415.  
  416. 		public enum RecycleFlags : uint
  417. 		{
  418. 			RecycleNoConfirmation = 0x00000001,
  419. 			RecycleNoProgressUI = 0x00000002,
  420. 			RecycleNoSound = 0x00000004
  421. 		}
  422.  
  423.  
  424. 		[DllImport( "Shell32.dll", CharSet = CharSet.Unicode )]
  425. 		static extern uint SHEmptyRecycleBin( IntPtr hwnd, string pszRootPath, RecycleFlags dwFlags );
  426. 	}
  427. }

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