(view source code of trash.cs as plain text)
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.VisualBasic.FileIO;
namespace RobvanderWoude
{
internal class Trash
{
static readonly string progver = "1.01";
static int Main( string[] args )
{
bool continueonerrors = false;
bool debugmode = false;
bool verbose = false;
UIOption dialogs = UIOption.OnlyErrorDialogs;
int rc = 0;
int deleted = 0;
int failed = 0;
if ( args.Length == 0 || args.Contains( "/?" ) )
{
return ShowHelp( );
}
foreach ( string arg in args )
{
if ( arg.ToUpper( ) == "/C" )
{
if ( continueonerrors )
{
return ShowHelp( "Duplicate command line switch /C" );
}
continueonerrors = true;
}
else if ( arg.ToUpper( ) == "/D" )
{
if ( debugmode )
{
return ShowHelp( "Duplicate command line switch /D" );
}
debugmode = true;
}
else if ( arg.ToUpper( ) == "/V" )
{
if ( verbose )
{
return ShowHelp( "Duplicate command line switch /V" );
}
verbose = true;
dialogs = UIOption.AllDialogs;
}
else if ( arg[0] == '/' )
{
return ShowHelp( "Invalid command line switch {0}", arg );
}
else if ( !Directory.Exists( Directory.GetParent( arg ).FullName ) )
{
if ( continueonerrors )
{
rc = 1;
}
else
{
return ShowHelp( "File \"{0}\" not found", arg );
}
}
}
verbose = verbose || debugmode;
List<string> filestodelete = new List<string>( );
foreach ( string arg in args )
{
if ( arg[0] != '/' )
{
DirectoryInfo directory = Directory.GetParent( arg );
FileInfo[] files = directory.GetFiles( Path.GetFileName( arg ) );
foreach ( FileInfo file in files )
{
filestodelete.Add( file.FullName );
}
}
}
if ( filestodelete.Count == 0 )
{
if ( continueonerrors )
{
rc = 1;
Console.WriteLine( "No files to delete" );
}
else
{
return ShowHelp( "No files to delete" );
}
}
foreach ( string file in filestodelete )
{
try
{
if ( verbose )
{
Console.Write( "Deleting \"{0}\" . . . ", file );
}
if ( !debugmode )
{
FileSystem.DeleteFile( file, dialogs, RecycleOption.SendToRecycleBin );
}
if ( verbose )
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine( "success" );
Console.ResetColor( );
}
deleted++;
}
catch ( Exception e )
{
if ( continueonerrors )
{
rc = 1;
if ( verbose )
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine( "failed" );
Console.ResetColor( );
}
failed++;
}
else
{
return ShowHelp( e.Message );
}
}
}
if ( verbose )
{
if ( deleted == 1 )
{
Console.Write( "\nSuccessfully deleted 1 file, " );
}
else
{
Console.Write( "\nSuccessfully deleted {0} files, ", deleted );
}
if ( failed == 1 )
{
Console.WriteLine( "1 file failed" );
}
else
{
Console.WriteLine( "{0} files failed", failed );
}
}
return rc;
}
public static int ShowHelp( params string[] errmsg )
{
#region Error Message
if ( errmsg.Length > 0 )
{
List<string> errargs = new List<string>( errmsg );
errargs.RemoveAt( 0 );
Console.Error.WriteLine( );
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.Write( "ERROR:\t" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
Console.ResetColor( );
}
#endregion Error Message
#region Help Text
/*
Trash.exe, Version 1.01
Send specified file(s) to the recycle bin
Usage: TRASH.EXE filespec [ filespec [ ... ] ] ] [ options ]
Where: filespec file(s) to be sent to the recycle bin (wildcards allowed)
Options: /C Continue on errors (default: abort on first error)
/D Debug mode: files are listed only, not deleted
/V Verbose output: show file name(s), progress and error
dialogs, and summary (default: error dialogs only)
Note: Return code ("ErrorLevel"):
* successful deletion of all files specified: 0
* invalid directory, failure to delete or no matching file (with /C): 1
* invalid directory, failure to delete or no matching file (without /C): -1
* invalid command line argument: -1
Written by Rob van der Woude
https://www.robvanderwoude.com
*/
#endregion Help Text
#region Display Help Text
Console.Error.WriteLine( );
Console.Error.WriteLine( "Trash.exe, Version {0}", progver );
Console.Error.WriteLine( "Send specified file(s) to the recycle bin" );
Console.Error.WriteLine( );
Console.Error.Write( "Usage: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( "TRASH.EXE filespec [ filespec [ ... ] ] ] [ /V ]" );
Console.ResetColor( );
Console.Error.WriteLine( );
Console.Error.Write( "Where: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "filespec" );
Console.ResetColor( );
Console.Error.WriteLine( " file(s) to be sent to the recycle bin (wildcards allowed)" );
Console.Error.WriteLine( );
Console.Error.Write( "Options: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "/C C" );
Console.ResetColor( );
Console.Error.WriteLine( "ontinue on errors (default: abort on first error)" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( " /D D" );
Console.ResetColor( );
Console.Error.WriteLine( "ebug mode: files are listed only, not deleted" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( " /V V" );
Console.ResetColor( );
Console.Error.WriteLine( "erbose output: show file name(s), progress and error" );
Console.Error.WriteLine( " dialogs, and summary (default: error dialogs only)" );
Console.Error.WriteLine( );
Console.Error.WriteLine( "Note: Return code (\"ErrorLevel\"):" );
Console.Error.WriteLine( " * successful deletion of all files specified: 0" );
Console.Error.Write( " * invalid directory, failure to delete or no matching file (with " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "/C" );
Console.ResetColor( );
Console.Error.WriteLine( "): 1" );
Console.Error.Write( " * invalid directory, failure to delete or no matching file (without " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "/C" );
Console.ResetColor( );
Console.Error.WriteLine( "): -1" );
Console.Error.WriteLine( " * invalid command line argument: -1" );
Console.Error.WriteLine( );
Console.Error.WriteLine( "Written by Rob van der Woude" );
Console.Error.WriteLine( "https://www.robvanderwoude.com" );
#endregion Display Help Text
return -1;
}
}
}
page last modified: 2024-04-16; loaded in 0.0089 seconds