Powershell 将文件移动到垃圾箱
我在脚本中实现了一个直接删除函数(直接删除文件)和一个垃圾箱删除函数(首先将其移动到垃圾箱) 问题是垃圾箱删除不起作用。我已经试过了,但似乎不起作用 我的完整脚本:Powershell 将文件移动到垃圾箱,powershell,Powershell,我在脚本中实现了一个直接删除函数(直接删除文件)和一个垃圾箱删除函数(首先将其移动到垃圾箱) 问题是垃圾箱删除不起作用。我已经试过了,但似乎不起作用 我的完整脚本: ## Top of the script param( [Parameter(Mandatory=$true)] [ValidateRange(0,99999)] [int]$minutes, [Parameter(Mandatory=$true)] [ValidateScript({Tes
## 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')
PowerShell控制台中的错误代码:
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
新时间跨度:无法将“System.Object[]”转换为所需的“System.DateTime”类型
通过参数“Start”。不支持该方法。
在C:\Users\david\Desktop\nieuw.ps1:21 char:39
+$minutesconvert=(新的时间跨度-开始这是你的罪魁祸首:
$file = Get-ChildItem -Path $maplocation | Get-Date
上面的语句将为您提供$maplocation
中每个文件和文件夹的当前日期和时间。如果$maplocation
不是单个文件,则结果是一个数组,而New TimeSpan
不准备处理该数组。该过程也不太可能是您实际想要的。您可能需要时间d$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' { ... } } } }
$file = Get-ChildItem -Path $maplocation | Get-Date
上面的语句将为您提供$maplocation
中每个文件和文件夹的当前日期和时间。如果$maplocation
不是单个文件,则结果是一个数组,而New TimeSpan
不准备处理该数组。该过程也不太可能是您实际想要的。您可能需要时间d$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' { ... } } } }
由于您问题中的示例代码不完整,可能需要进一步调整。您是否尝试了?请提供比“不起作用”更详细的信息。发生了什么?错误?文件被完全删除?更改
$maplocation=“C:\Temp”
@JamesC.我的错是我的错。@MartinBrandl Yes接受的答案也不起作用,因为它会打开一个类似“你确定要将此文件移动到垃圾箱”的消息提示。我需要立即完成。你试过了吗?请提供比“不起作用”更详细的信息.发生了什么?错误?文件被完全删除?更改$maplocation=“C:\Temp”
@JamesC。我的错是我的打字错误。@MartinBrandl“是”接受的答案也不起作用,因为它会打开一条消息提示,类似“你确定要将此文件移到垃圾箱吗?”谢谢你的回答,你帮了我很多。谢谢你的回答,你帮了我很多。