Re: Command to Empty Recycle Bin with Perl?
by Athanasius (Archbishop) on Jul 09, 2014 at 06:36 UTC
|
A Google search for “empty recycle bin from command line” leads to this: http://stackoverflow.com/questions/9238953/how-to-empty-recyclebin-through-command-prompt. The proposed solutions include:
Run these from Perl using backticks or system. Disclaimer: I haven’t tried the approaches suggested. Note the caveats to the first solution.
Hope that helps,
Update: I downloaded and unzipped Frank P. Westlake’s recycle utility. No malware, and it appears to work as advertised. (I’m running Windows 8.1.)
| [reply] [d/l] [select] |
|
Hi Athanasius,
Do you know what rd and /s actually mean? Regarding %systemdrive% are you to sub a drive letter for systemdrive and type a percent mark on both side of the letter? Also, if your local hard drive is separated into multiple partitions, how do you know which drive letter is correct? My local hard disk is comprised of the C: drive and E: drive, and regardless of a file's original location (C: or E: drive) if I delete it, it moves to the Recycle Bin.
Thanks!
| [reply] [d/l] [select] |
|
Hello sphinxy09,
rd (a synonym for rmdir) is the MS-DOS command to remove a directory. Without the /s option, the directory must be empty. The /s option:
Removes all directories and files in the specified directory in addition to the directory itself. Used to remove a directory tree. (http://www.computerhope.com/rmdirhlp.htm)
No need to substitute a drive letter for %systemdrive%, that shell variable will already be set for the current command line session. Enter set systemdrive (or just set) from a command line to verify this; for example, I see:
0:11 >set systemdrive
SystemDrive=C:
0:11 >
My local hard disk is comprised of the C: drive and E: drive, and regardless of a file's original location (C: or E: drive) if I delete it, it moves to the Recycle Bin.
I can’t test this, but my understanding is that each partition (regardless of its physical location) has its own Recycle Bin. Since Windows Vista, this has the location: Drive:\$Recycle.Bin (see Trash_(computing)#Microsoft_Windows). The contents of the desktop recycle bin are not necessarily the same as the contents of any one of these Recycle Bin directories.
Hope that helps,
| [reply] [d/l] [select] |
Re: Command To Empty Recycle Bin With Perl?
by SimonPratt (Friar) on Jul 09, 2014 at 10:32 UTC
|
I use Perl and WinZip's command line interface to automatically move ("-m" operator adds to ZIP and deletes from original location) folders / files to ZIP files. In PERL v5.6.1 the "move" would permanently delete the folders / files but in PERL v5.16.1 the "move" only moves the files to the Recycle Bin
This doesn't sound right. From what you're saying, I assume you're doing something like this: system('winzip -m ...');
If that is the case, then the version of Perl has absolutely zero effect on how WinZip operates and the change in behaviour is due to something else changing on your system (I'm guessing you have switched to a different PC?). Probably the easiest fix will be to go into the Recycle Bin settings and disable the Recycle Bin.
| [reply] [d/l] |
|
Hi Simon,
I'm actually using system "wzzip -m ..." Nothing has changed on my PC besides the version of Perl, which was an upgrade from 5.6.1 to 5.16.1; I'm using the same PC and same version of WinZip, and no other applications on the PC have changed. Having the result of this upgrade change a permanent delete to a move to Recycle Bin was definitely unexpected. I don't want to disable the Recycle Bin because it needs to be available for other purposes, so any other suggestions?
Thanks!
| [reply] [d/l] |
|
Hi, Sphinxy09
I stand by what I said earlier - Perl does not interact with the recycle bin in this fashion (indeed, I am sceptical that any application could legitimately hijack the delete function in Windows like this). The method you are using to run WinZip is basically the same as you opening a command prompt and typing wzzip -m .... If you try this now, WinZip should exhibit exactly the same behaviour as when Perl calls it.
As for a fix, you can go with either Athanasius' suggestion, or with bulk88's code (though I would suggest changing line 13 to my $return = SHEmptyRecycleBin(0, "", 1); once you are certain it is doing what you want (the 1 tells Windows to run the command without prompting for confirmation)
| [reply] [d/l] [select] |
Re: Command To Empty Recycle Bin With Perl?
by bulk88 (Priest) on Jul 13, 2014 at 04:23 UTC
|
#!/usr/bin/perl -w
use strict;
use Win32::API;
Win32::API::Type->typedef( 'HRESULT', 'LONG' );
my $function = Win32::API::More->Import(
'shell32.dll', '
HRESULT SHEmptyRecycleBin(
HWND hwnd,
LPCTSTR pszRootPath,
DWORD dwFlags
);');
die "Error: $^E" if ! $function; #$^E is non-Cygwin only
my $return = SHEmptyRecycleBin(0, "", 0);
| [reply] [d/l] |