我正在尝试编写一个将搜索的脚本:
- 具有特定名称的文件夹。
- 具有特定扩展名的文件,而
- 从搜索中排除某些目录。
执行搜索后,我希望删除所有 1 和 2 以及父文件夹(如果它们与 3 不匹配)。这是我目前所拥有的:
#Define list of computers to search for the files and folders in question
$ComputerList = Get-Content -Path "C:\computers.txt"
#Define a function for searching files and folders based on criteria:
function search {
PROCESS {
$srcFolder ="C:\test"
Get-ChildItem $srcFolder -ErrorAction SilentlyContinue -recurse -Force | Where-Object {`
$_.Name -eq “keyword1” -or`
$_.Name -eq “keyword2” -or`
-and $_.fullname -notmatch 'C:\\Windows'`
-and $_.fullname -notmatch 'C:\\Program Files'
} | foreach-object -process { _.FullName }
}
}
foreach ($strComputer in $ComputerList)
{
#Perform the search function on all the computers listed
foreach ($objItem in $colItems)
{
write-host "-------------------------$strComputer ----------------" -foregroundcolor "red"
write-host " Files Found " -foregroundcolor "yellow" -backgroundcolor "black"
$ComputerList | search
"Number of files and folders found: " +($ComputerList | search | Measure-Object | Select-Object -ExpandProperty Count)
write-host "------------------------------------------------------------------" -foregroundcolor "red"
}
if (($ComputerList | search).count -lt 1) {
write-host "No files found to delete"
}
else {
#Prompt if you want to delete the files
write-host "Do you want to delete these files?" -foregroundcolor "yellow" -backgroundcolor "black"
$ComputerList | search | Remove-Item -Force -confirm
}
}
Out-File -FilePath C:\results.txt
所以这是我遇到的问题:
我可以让脚本工作。但是,我不确定如何在保护排除的文件夹的同时删除父文件夹。
文件的输出不起作用。该文件已创建,但它是空白的。为什么?
查看后Get-ChildItem | Get-Member
,我意识到 Parent 属性是由 System.IO.DirectoryInfo Parent 指定的,所以如果我可以将其添加到要删除的项目列表中,那么它应该可以工作。
文件夹结构如下。为了以更清晰的方式重申,我想删除一个文件夹:
- 其名称或内容与关键字匹配
- 如果内容与关键字匹配,但文件夹名称不匹配 > 检查它是否被明确排除,例如 C:\Windows、C:\Program Files 等。 > 如果是,请不要理会。如果不是,请删除该文件夹。**
以下是纯 URL:
oi42.tinypic.com/2nulmz4.jpg
oi43.tinypic.com/fwlxd0.jpg
oi42.tinypic.com/315hdw9.jpg