PoshCode Archive  Artifact [b5681803f7]

Artifact b5681803f7bc81739e12584394975ed2a2235ceed29b2a40716652ede38adff2:

  • File Recycle-Bin-object.ps1 — part of check-in [597c369559] at 2018-06-10 13:52:46 on branch trunk — Correctly works with reparse points (user: Robespierre size: 2078)

# encoding: ascii
# api: powershell
# title: Recycle Bin object
# description: Correctly works with reparse points
# version: 0.1
# type: function
# author: Robespierre
# license: CC0
# function: Calculate-Items
# x-poshcode-id: 5267
# x-archived: 2017-05-25T20:12:20
# x-published: 2017-06-28T11:42:00
#
# Usage:
# $RecycleBin.Items()
# $RecycleBin.Count()
# $RecycleBin.Clear()
#
$method = @{}
$method['clear'] = {
	$this.Items() | foreach -Process {
		# for the first we must remove all Junction-points
		if ($_.IsLink) { (gi $_.Path).Delete() } # directly (1)
		ls $_.Path -Recurse -Force -Directory | foreach {
			$d_item = gi -LiteralPath $_.FullName
			if ($d_item.Attributes -match 'ReparsePoint') { $d_item.Delete() } # and recursively (2)
		}
		ri $_.Path -Recurse -Force -Confirm:$false # and now the actual cleanup (3)
	}
} # /method.clear
$method['count'] = {
	Set-Variable -Name links,files,folders,size -Value 0 -Option AllScope
	function Calculate-Items ($ItemsList)
	{
		function count ($folder, $list)
		{
			if (-not $list) { $list = Get-Item "$($folder)\*" }
			$list | foreach {
				if ($_.Attributes -match 'ReparsePoint') { ++$links }
				elseif ($_.Attributes -notmatch 'Directory') { ++$files;  $size += $_.Length }
				else { ++$folders; count -folder $_.FullName } # recursively call
			}
		}
		count -list $ItemsList # initial call
	} # gci w/o links
	$upper_items_list = @()
	$this.Items() | foreach -Process { $upper_items_list += gi -LiteralPath $_.Path }
	if ($upper_items_list) { Calculate-Items -ItemsList $upper_items_list }
	return New-Object -TypeName psobject -Property @{
			Links = $links; Files = $files; Folders = $folders; Length = $size
	}
} # /method.count
$bin = (New-Object -ComObject 'Shell.Application').Namespace(0xA)
$bin | Add-Member -MemberType 'ScriptMethod' -Name 'Clear' -Value $method.clear
$bin | Add-Member -MemberType 'ScriptMethod' -Name 'Count' -Value $method.count
New-Variable -Name RecycleBin -Value $Bin -Option Constant
Remove-Variable -Name method, bin