2017-02-02 30 views
0

私は自分のスクリプトに実装されている直接削除機能(直接ファイルを削除する)とゴミ箱削除機能(ゴミ箱に移動する)を持っています。ファイルをゴミ箱に移動

問題はゴミ箱の削除が機能しないことです。私はすでにthis suggested postを試しましたが、うまくいかないようです。

私の完全なスクリプト:PowerShellコンソールで

## 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') 

エラーコード:

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
+0

あなたは[受け入れられた回答](http://stackoverflow.com/a/503768/1163423)を試しましたか? –

+0

「うまくいかない」より詳細を入力してください。何が起こるのですか?エラー?ファイルは完全に削除されますか? –

+0

'$ maplocation =" C:\ Temp "' –

答えて

2

は、これはあなたの犯人である:

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

上記の文はあなたに現在の日付を与えると、各ファイルとフォルダの時刻は$maplocationです。 $maplocationが単一のファイルでない場合、結果は配列になります。New-TimeSpanは処理できません。この手順はまた、あなたが実際に意図したものであることはほとんどありません。おそらく、最後の変更(作成)日である$maplocation(またはその内容)との時差が必要です。さらに、タイムパンを計算するのではなく、現在のタイムスタンプから分数を減算し、それを基準日として使用する方が良いでしょう。また

、あなたはケース$maplocationで何をしたいかに依存して、あなたが違ったアイテムを処理する必要があるかもしれません、フォルダです:

  • $maplocationはフォルダであり、あなたは内のフォルダとすべてのものを削除したいですそれ、または$maplocationは、単一のファイルです:

    $maxAge = (Get-Date).AddMinutes(-$minutes) 
    $file = Get-Item $maplocation 
    if ($file.LastWriteTime -lt $maxAge) { 
        switch ($consequence) { 
        'direct' { ... } 
        'trashbin' { ... } 
        } 
    } 
    
  • $maplocationフォルダですし、基準日より古いそれからアイテムのみを削除したい:

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

あなたの質問からサンプルコードは不完全で、さらに調整が必要な場合がありますされているので。

+0

の完全なコードを追加しました。答えをいただきありがとうございました。 – davidchinees

関連する問題