Not very sexy, but it some may find it useful.

The following code was tested on Window 11 using the 64-bit version of strawberry 5.30.3. It was assembled by extracting the relevant bits from the Win32::FileOp module and making a simple change to account for the fact that I am using a 64-bit version of perl.

use strict; use warnings; use Win32::API; sub FO_DELETE () { 0x03 } sub FOF_SILENT () { 0x0004 } # don't create progress/report sub FOF_NOCONFIRMATION () { 0x0010 } # Don't prompt the user. sub FOF_ALLOWUNDO () { 0x0040 } # recycle bin instead of delete sub FOF_NOERRORUI () { 0x0400 } # don't put up error UI sub Recycle { # a series of null-terminated pathnames, with a double null at the e +nd my $paths = join "\0", @_, "\0"; my $recycle = new Win32::API('shell32', 'SHFileOperation', 'P', 'I') +; my $options = FOF_ALLOWUNDO | FOF_NOCONFIRMATION | FOF_SILENT | FOF_ +NOERRORUI; # for everything except paths and options, pack with Q (rather than +L), since we're using 64-bit perl # my $opstruct = pack ('LLpLILLL', 0, FO_DELETE, $paths, 0, $options +, 0, 0, 0); my $opstruct = pack ('QQpQIQQQ', 0, FO_DELETE, $paths, 0, $options, +0, 0, 0); return $recycle->Call($opstruct); } my $file = "C:\\Users\\James\\fish"; my $rc = Recycle($file); print "RC: $rc\n";

Return codes are described here:

https://learn.microsoft.com/en-us/windows/win32/debug/system-error-codes--0-499-

Replies are listed 'Best First'.
Re: Sending items to the windows recycle bin
by johngg (Canon) on Aug 11, 2023 at 20:32 UTC

    I don't use MS Windows so can't judge the effectiveness of your script. This is just a comment on a couple of lines of your code.

    my $paths = join "\0", @_; $paths .= "\0\0";

    They could be condensed into one statement.

    my $paths = join "\0", @_, "\0";

    An example

    johngg@aleatico:~$ perl -Mstrict -Mwarnings -E ' my @p = qw{ a b c }; my $p = join "\0", @p, "\0"; print $p;' | hexdump -C 00000000 61 00 62 00 63 00 00 |a.b.c..| 00000007

    I hope this is of interest.

    Update: Corrected typo slash to backslash, thanks to all who pointed it out.

    Cheers,

    JohnGG

      Thanks!