排除文件夹及其内容时使用 get-childitems -exclude 不起作用。

huangapple go评论50阅读模式
英文:

Excluding folder and contents when using get-child items -exclude not working

问题

我想让脚本通过特定路径,并排除一个特定文件夹及其内容。然后,我想让它删除所有早于3天的.pdf文件。过滤和删除的部分都按预期工作,但是我在排除我想保留数据的文件夹方面遇到了问题。

$older_than = "3" #当前日期之前的天数
$path = "E:\Bookmark\Bookmarkers" #文件路径
$excluded = "E:\Bookmark\Bookmarkers\VBMSArchive" #vbms文档文件夹
$ext = "*.pdf" #要删除的文件扩展名
$cut_off = (Get-Date).AddDays(-$older_than) #计算截止日期
Get-ChildItem -Path $path -Exclude $excluded -Recurse -File | Where-Object { $_.Name -like $ext } | Where-Object { $_.LastWriteTime -lt $cut_off } | Remove-Item -Force -Verbose #删除所需的文件

脚本仍然会删除排除文件夹中早于3天的.pdf文件。

英文:

I want the script to go through a specific path and exclude one specific folder and its contents. I then want it to delete all .pdf files older than 3 days. The filtering and deletion works as expected, however i an having issues excluding the folder that I want to keep data in.

$older_than = "3" #days before current date
$path = "E:\Bookmark\Bookmarkers" #file path 
$excluded = "E:\Bookmark\Bookmarkers\VBMSArchive" #vbms documentation folder 
$ext = "*.pdf"  #file ext. to delete.
$cut_off = (Get-Date).AddDays(-$older_than) #Calculates the cut off date
Get-ChildItem -Path $path  -exclude $excluded -Recurse -File | Where-Object { $_.Name -like $ext }| Where-Object {$_.LastWriteTime -lt $cut_off} | Remove-Item -Force -Verbose #remove the desired files. 

The script still deleted the .pdf files in the excluded folder that are older than 3 days.

答案1

得分: 0

以下是我解决问题的方法:
我将筛选后的文件夹列表设置为一个变量,然后使用该变量来枚举文件并删除它们。

$older_than = "7" #距离当前日期的天数
$path = "E:\Bookmarking\Bookmarkers" #文件路径
Set-Location -Path $path #将工作目录设置为e:\bookmark\bookmarkers
$excluded = "VBMSArchive" #vbms文档文件夹
$ext = "*.pdf"  #要删除的文件扩展名。
$cut_off = (Get-Date).AddDays(-$older_than) #计算截止日期
$filteredList = Get-ChildItem -Path $path | where-Object {$_.Name -notlike $excluded}
Get-ChildItem $filteredList -Recurse  -File | Where-Object { $_.Name -like $ext }| Where-Object {$_.LastWriteTime -lt $cut_off} | Remove-Item -Force -Verbose #删除所需的文件。
英文:

Here is how I resolved my issue.
I set the filtered list of folders to a variable, then used the variable to enumerate the files and delete them.

$older_than = "7" #days before current date
$path = "E:\Bookmarking\Bookmarkers" #file path 
Set-Location -Path $path #sets working directory to e:\bookmark\bookmarkers
$excluded = "VBMSArchive" #vbms documentation folder 
$ext = "*.pdf"  #file ext. to delete.
$cut_off = (Get-Date).AddDays(-$older_than) #Calculates the cut off date
$filteredList = Get-ChildItem -Path $path | where-Object {$_.Name -notlike $excluded}
Get-ChildItem $filteredList -Recurse  -File | Where-Object { $_.Name -like $ext }| Where-Object {$_.LastWriteTime -lt $cut_off} | Remove-Item -Force -Verbose #remove the desired files. 

huangapple
  • 本文由 发表于 2023年6月27日 21:26:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76565370.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定