标题: [文件操作] 如何自动清除回收站中指定“删除日期”前的文件? [打印本页]
作者: locoman 时间: 2019-2-24 15:09 标题: 如何自动清除回收站中指定“删除日期”前的文件?
如何自动清除回收站中指定“删除日期”前的文件?
需求:
1. 回收站中的文件需要保存一段之间待恢复使用。
2. 但又希望回收站不存在太多的文件。
——因此,希望编个批处理能实现:每天能自动清除进入回收站内3天前的文件,“删除日期”在3天内的文件不清除。
谢谢大神的帮助!
作者: ivor 时间: 2019-2-24 15:59
本帖最后由 ivor 于 2019-2-24 16:03 编辑
https://gallery.technet.microsof ... in-Recycle-6d1fbb75- #---------------------------------------------------------------------------------
- #The sample scripts are not supported under any Microsoft standard support
- #program or service. The sample scripts are provided AS IS without warranty
- #of any kind. Microsoft further disclaims all implied warranties including,
- #without limitation, any implied warranties of merchantability or of fitness for
- #a particular purpose. The entire risk arising out of the use or performance of
- #the sample scripts and documentation remains with you. In no event shall
- #Microsoft, its authors, or anyone else involved in the creation, production, or
- #delivery of the scripts be liable for any damages whatsoever (including,
- #without limitation, damages for loss of business profits, business interruption,
- #loss of business information, or other pecuniary loss) arising out of the use
- #of or inability to use the sample scripts or documentation, even if Microsoft
- #has been advised of the possibility of such damages
- #---------------------------------------------------------------------------------
-
- #requires -version 2.0
-
- Import-LocalizedData -BindingVariable Message
-
- Function Get-oscRecycleBin
- {
- <#
- .SYNOPSIS
- Get-oscRecycleBin is an advanced function which can be used to list items and delete items from recycle bin.
- .DESCRIPTION
- Get-oscRecycleBin is an advanced function which can be used to list items and delete items from recycle bin.
- .PARAMETER <ListAllItems>
- Lists all of the items of Recycle Bin.
- .PARAMETER <DeleteDaysBefore>
- Delete of specified items in Recycle Bin.
- .PARAMETER <ListDaysBefore>
- Lists of specified items in Recycle Bin.
- .EXAMPLE
- C:\PS> Get-oscRecycleBin -ListAllItems
-
- Name Type Size Deleted Days Deleted Date
- ---- ---- ---- ------------ ------------
- EnumerateInstalledPr... File folder 0.000 MB 13 Days 2/21/2012 4:44:00 PM
- edit_x64 File folder 0.000 MB 13 Days 2/21/2012 4:45:00 PM
- TestReview.zip WinRAR ZIP 0.213 MB 10 Days 2/22/2012 9:49:00 AM
- TestReview File folder 0.000 MB 10 Days 2/22/2012 10:16:00 AM
- TestReview File folder 0.000 MB 5 Days 2/22/2012 11:22:00 AM
-
- A Total of [5] items in Recycle Bin.
- .EXAMPLE
- C:\PS> Get-oscRecycleBin -DeleteDaysBefore 11
-
- Name Type Size Deleted Days Action
- ---- ---- ---- ------------ ------------
- TestReview.zip WinRAR ZIP 0.213 MB 10 Days Deleted
- TestReview File folder 0.000 MB 10 Days Deleted
- .EXAMPLE
- C:\PS> Get-oscRecycleBin -ListDaysBefore 7
-
- Name Type Size Deleted Days Deleted Date
- ---- ---- ---- ------------ ------------
- TestReview File folder 0.000 MB 5 Days 2/22/2012 11:22:00 AM
- #>
- [CmdletBinding(SupportsShouldProcess=$true)]
- Param
- (
- [Parameter(Mandatory=$true,Position=0,ParameterSetName='DeleteSpecified', `
- HelpMessage="Delete of specified items in Recycle Bin")]
- [Alias("deld")][Int32]$DeleteDaysBefore,
- [Parameter(Mandatory=$true,Position=0,ParameterSetName='ListSpecified', `
- HelpMessage="Lists of specified items in Recycle Bin")]
- [Alias("ld")][Int32]$ListDaysBefore,
- [Parameter(Mandatory=$true,Position=0,ParameterSetName='ListAll', `
- HelpMessage="Lists all of the items of Recycle Bin")]
- [Alias("all")][switch]$ListAllItems
- )
-
- BEGIN
- {
- $Objs = @()
- $Shell = New-Object -ComObject Shell.Application
- $Recycler = $Shell.NameSpace(0xa)
- }
-
- PROCESS
- {
- foreach($item in $Recycler.Items())
- {
- $Name = $item.Name
- $Type = $item.Type
- $Path = $item.Path
- $Size = "{0:N3}" -f $($item.Size/1MB)+" MB"
- $Deleted_Date = [DateTime]($Recycler.GetDetailsOf($item,2) -replace "\u200f|\u200e","")
- $Deleted_Days = "{0:N0}" -f [System.Math]::Abs((New-TimeSpan -Start `
- $(Get-Date) -End $Deleted_Date).Days) +" Days"
-
- $object = New-Object -TypeName PSObject
- $object | Add-Member -MemberType NoteProperty -Name "Name" -Value $Name
- $object | Add-Member -MemberType NoteProperty -Name "Type" -Value $Type
- $object | Add-Member -MemberType NoteProperty -Name "Size" -Value $Size
- $object | Add-Member -MemberType NoteProperty -Name "Path" -Value $Path
- $object | Add-Member -MemberType NoteProperty -Name "Deleted Days" -Value $Deleted_Days
- $object | Add-Member -MemberType NoteProperty -Name "Deleted Date" -Value $Deleted_Date
-
- $Objs += $object
- }
-
- #list all of the items in recycle bin.
- if($ListAllItems)
- {
- $PSCmdlet.WriteVerbose($Message.ListAllMsg)
- if($PSCmdlet.ShouldProcess("Recycle Bin","List all of items"))
- {
- $Objs|Select-Object "Name","Type","Size","Deleted Days","Deleted Date"| `
- Sort-Object "Deleted Date"|Format-Table
- Write-Host "A Total of [$($Objs.Count)] items in Recycle Bin.`r`n"
- }
- }
- #List of specified items in the recycle bin.
- if ($ListDaysBefore -gt 0)
- {
- $PSCmdlet.WriteVerbose($Message.ListMsg)
- if($PSCmdlet.ShouldProcess("Recycle Bin","List of specified items"))
- {
- $RecyclerItems = $Objs | Where-Object{$_."Deleted Date" -ge `
- (Get-Date).AddDays(-$ListDaysBefore)}
- if($RecyclerItems.Count -eq $null)
- {
- Write-Warning -Message "The item not found."
- }
- else
- {
- $RecyclerItems|Select-Object "Name","Type","Size","Deleted Days","Deleted Date"| `
- Sort-Object "Deleted Date"|Format-Table
- Write-Host "A Total of [$($RecyclerItems.Count)] items were deleted before $ListDaysBefore days.`r`n"
- }
- }
- }
- # Delete items that have been in the Recycle Bin more than 10 days.
- if($DeleteDaysBefore -gt 0)
- {
- $PSCmdlet.WriteVerbose($Message.DeleteMsg)
- if($PSCmdlet.ShouldProcess("Recycle Bin","Delete of specified items"))
- {
- $RecyclerItems = $Objs | Where-Object{$_."Deleted Date" -ge `
- (Get-Date).AddDays(-$DeleteDaysBefore)}
- if($RecyclerItems.Count -eq $null)
- {
- Write-Warning -Message "The item not found."
- }
- else
- {
- $RecyclerItems|Select-Object "Name","Type","Size","Deleted Days",`
- @{Name="Action";Expression={Remove-Item -Path $_.Path -Confirm:$false -Force -Recurse;
- if(Test-Path -Path $_.Path)
- {"Not Deleted"}
- else
- {"Deleted"}
- }}|Format-Table
- }
- }
- }
- }
- }
-
- Get-oscRecycleBin -DeleteDaysBefore 3
复制代码
作者: locoman 时间: 2019-3-6 11:03
回复 2# ivor
谢谢您的热情帮助!!
但是,复制代码保存为.bat运行报错,且回收站并未删除。
报错如下:
F:\test>自动删除回收站3天前的文件.bat
F:\test>#-----------------------------------------------------------------------
----------
'#------------------------------------------------------------------------------
---' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
F:\test>#The sample scripts are not supported under any Microsoft standard suppo
rt
'#The' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
F:\test>#program or service. The sample scripts are provided AS IS without warra
nty
'#program' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
F:\test>#of any kind. Microsoft further disclaims all implied warranties includi
ng,
'#of' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
F:\test>#without limitation, any implied warranties of merchantability or of fit
ness for
'#without' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
F:\test>#a particular purpose. The entire risk arising out of the use or perform
ance of
'#a' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
F:\test>#the sample scripts and documentation remains with you. In no event shal
l
'#the' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
F:\test>#Microsoft, its authors, or anyone else involved in the creation, produc
tion, or
'#Microsoft' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
F:\test>#delivery of the scripts be liable for any damages whatsoever (including
,
'#delivery' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
F:\test>#without limitation, damages for loss of business profits, business inte
rruption,
'#without' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
F:\test>#loss of business information, or other pecuniary loss) arising out of t
he use
'#loss' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
F:\test>#of or inability to use the sample scripts or documentation, even if Mic
rosoft
'#of' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
F:\test>#has been advised of the possibility of such damages
'#has' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
F:\test>#-----------------------------------------------------------------------
----------
'#------------------------------------------------------------------------------
---' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
F:\test>#requires -version 2.0
'#requires' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
F:\test>Import-LocalizedData -BindingVariable Message
'Import-LocalizedData' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
F:\test>Function Get-oscRecycleBin
'Function' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
F:\test>{
'{' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
命令语法不正确。
F:\test><#
作者: ivor 时间: 2019-3-6 11:55
powershell代码 和 bat混编参考:http://www.bathome.net/redirect. ... 1776&pid=216458
作者: Batcher 时间: 2019-3-6 13:21
回复 3# locoman
怎样执行 PowerShell 代码?
http://bbs.bathome.net/thread-31071-1-1.html
作者: locoman 时间: 2019-3-16 15:46
本帖最后由 locoman 于 2019-3-16 15:56 编辑
回复 5# Batcher
谢谢您这么多年一直在大家最需要的时候,无私的有力的支撑帮助大家!!非常的感谢和敬佩!!
为了实现主贴需求,也按照您的指点一步一步的操作,但2楼的代码运行后还是报错并不能实现需求——
一、将2楼的代码保存为D:\test.ps1文件;
二、按照以下步骤执行:
第1步:打开 CMD 命令行窗口,执行以下命令进入 PowerShell 提示符模式:
powershell
第2步:在 PowerShell 提示符下执行命令:
Set-ExecutionPolicy "RemoteSigned"
exit
第3步:在 CMD 命令行窗口里面执行ps1文件命令:
powershell -f "D:\test.ps1"
——以下是报错信息的部分,看似还是在对回收站文件的删除时间在判断,只是不能真正按照主贴需求删除文件:
所在位置 D:\test.ps1:84 字符: 20
+ $(Get-Date) -End <<<< $Deleted_Date).Days) +" Days"
+ CategoryInfo : WriteError: ( [New-TimeSpan], ParameterBinding
Exception
+ FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.PowerShell.Comm
ands.NewTimeSpanCommand
无法将值“2019-2-24 上午 4:18”转换为类型“System.DateTime”。错误:“该字符串未
被识别为有效的 DateTime。有一个从索引 10 处开始的未知字。”
所在位置 D:\test.ps1:82 字符: 30
+ $Deleted_Date = [DateTime] <<<< ($Recycler.GetDetailsOf($item,2)
-replace "\u200f|\u200e","")
+ CategoryInfo : NotSpecified: (:) [], RuntimeException
+ FullyQualifiedErrorId : RuntimeException
New-TimeSpan : 无法将参数“End”绑定到目标。设置“End”时发生异常:“未将对象引
用设置到对象的实例。”
所在位置 D:\test.ps1:84 字符: 20
+ $(Get-Date) -End <<<< $Deleted_Date).Days) +" Days"
+ CategoryInfo : WriteError: (:) [New-TimeSpan], ParameterBinding
Exception
+ FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.PowerShell.Comm
ands.NewTimeSpanCommand
无法将值“2019-3-16 下午 2:50”转换为类型“System.DateTime”。错误:“该字符串未
被识别为有效的 DateTime。有一个从索引 10 处开始的未知字。”
所在位置 D:\test.ps1:82 字符: 30
+ $Deleted_Date = [DateTime] <<<< ($Recycler.GetDetailsOf($item,2)
-replace "\u200f|\u200e","")
+ CategoryInfo : NotSpecified: (:) [], RuntimeException
+ FullyQualifiedErrorId : RuntimeException
……,……以下省略……,……
@Batcher
还是请求您的热心和实力继续帮助我实现工作需求,谢谢拜托您了!!
一、主要需求功能已经在主贴中说明了;
二、采用powershell的话,即使是按照您指导的三步骤能执行实现需求,但是,针对那些女同事们拿给她们也是搞不定的。能否搞成象批处理文件那样一点击就执行完成才是最好的!
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |