0

I have a direct delete function (deletes the files direct) implemented in my script, and a trashbin delete function (moving it to the trashbin first).

The problem is that the trashbin delete doesn't work. I've tried this suggested post already but it doesn't seem to work.

My full script :

## Top of the script
param(
    [Parameter(Mandatory=$true)]
    [ValidateRange(0,99999)]
    [int]$minutes,

    [Parameter(Mandatory=$true)]
    [ValidateScript({Test-Path $_})]
    [string]$maplocation,

    [Parameter(Mandatory=$true)]
    [ValidateSet("Direct", "TrashBin")]
    [string]$consequence
)

## error notifications

## Variables
$file = Get-ChildItem -Path $maplocation | Get-Date
$time = Get-Date
$minutesconvert = (New-Timespan -Start $file -End $time).TotalMinutes

foreach ($file in $files)
{
    if ($minutes -lt $minutesconvert -and $consequence -eq "direct")
    {
        Write-Verbose "File Found $file" -Verbose
        Write-Verbose "Deleting $file" -Verbose
        Remove-Item $file.FullName
    }
    elseif ($minutes -lt $minutesconvert -and $consequence -eq "trashbin")
    {
        Add-Type -AssemblyName Microsoft.VisualBasic
        Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile($maplocation, 'OnlyErrorDialogs', 'SendToRecycleBin')
    }
    else
    {
        Write-Verbose -message  "txt" -verbose
    }
}

Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile($maplocation, 'OnlyErrorDialogs', 'SendToRecycleBin')

Error code in PowerShell console:

New-TimeSpan : Cannot convert 'System.Object[]' to the type 'System.DateTime' required
by parameter 'Start'. The method is not supported.
At C:\Users\david\Desktop\nieuw.ps1:21 char:39
+ $minutesconvert = (New-TimeSpan -Start <<<<  $file -End $time).TotalMinutes
    + CategoryInfo          : InvalidArgument: (:) [New-TimeSpan], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.NewTimeSpanCommand
Community
  • 1
  • 1

1 Answers1

2

This is your culprit:

$file = Get-ChildItem -Path $maplocation | Get-Date

The above statement will give you the current date and time for each file and folder in $maplocation. If $maplocation is not a single file the result is an array, which New-TimeSpan is not prepared to handle. The procedure is also very unlikely to be what you actually intended. You probably want the time difference between the last modification (creation) date of $maplocation (or its contents?). Besides, rather than calculating a timespan it's better to subtract the number of minutes from the current timestamp and use that as a reference date.

Also, depending on what you want to do in case $maplocation is a folder, you may need to process the item differently:

  • $maplocation is a folder and you want to delete the folder and everything in it, or $maplocation is a single file:

    $maxAge = (Get-Date).AddMinutes(-$minutes)
    $file   = Get-Item $maplocation
    if ($file.LastWriteTime -lt $maxAge) {
      switch ($consequence) {
        'direct'   { ... }
        'trashbin' { ... }
      }
    }
    
  • $maplocation is a folder and you want to delete only items from it that are older than the reference date:

    $maxAge = (Get-Date).AddMinutes(-$minutes)
    $files  = Get-ChildItem $maplocation -Recurse
    foreach ($file in $files) {
      if ($file.LastWriteTime -lt $maxAge) {
        switch ($consequence) {
          'direct'   { ... }
          'trashbin' { ... }
        }
      }
    }
    

Since the sample code from your question is incomplete further adjustments may be required.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328