sec*_*ost 7 script powershell ps1
我试图将以下代码作为 powershell 脚本运行,但我无法让它工作。第一,以下适用于一个-Include项目,但我似乎无法让它与多个项目一起使用。2,-Exclude周边似乎不起作用。我仍然从C:\Windows和C:\Program Files目录中获取文件。
$Include = "*.zip","*.rar","*.tar","*.7zip"
$exclude = "C:\Windows","C:\Program Files"
Get-ChildItem "C:\" -Include $Include -Exclude $Exclude -Recurse -Force -ErrorAction silentlycontinue | Select-Object -ExpandProperty FullName
Run Code Online (Sandbox Code Playgroud)
注意:此脚本的目的是查找系统上的所有压缩文件。我知道这可能真的很简单,但我似乎无法让它发挥作用。
该-Exclude参数从未真正正常工作过。它似乎与属性匹配Name,这通常不是很有用。您可能只需要自己进行过滤:
$Include = "*.zip","*.rar","*.tar","*.7zip"
Get-ChildItem "C:\" -Include $Include -Recurse -Force -ErrorAction silentlycontinue |
? { $_.FullName -notmatch "^C:\\Windows" -and $_.FullName -notmatch "^C:\\Program" } |
Select-Object -ExpandProperty FullName
Run Code Online (Sandbox Code Playgroud)
(顺便说一句,-Filter比 快得多-Include。缺点是你不能像使用 一样给它一组模式-Include。但即使你必须搜索四次,它仍然可能更快。我不能可以肯定地说。如果速度对您来说很重要,那么可能值得测试一下。)
我同意 dangph 的观点,即 -exclude 不能按预期工作。
使用 -notmatch 时,您可以使用 或 构建正则表达式模式|。
这适用于修订后的 $include:
$Include = @('*.zip','*.rar','*.tar','*.7zip')
$exclude = [RegEx]'^C:\\Windows|^C:\\Program Files'
Get-ChildItem "C:\" -Include $Include -Recurse -Force -EA 0|
Where FullName -notmatch $exclude|
Select-Object -ExpandProperty FullName
Run Code Online (Sandbox Code Playgroud)
编辑由于排除的文件夹是第一级,根本不迭代它们要快得多,所以两步法更有效:
$Include = @('*.zip','*.rar','*.tar','*.7zip')
$exclude = [RegEx]'^C:\\Windows|^C:\\Program Files'
Get-ChildItem "C:\" -Directory |
Where FullName -notmatch $exclude|ForEach {
Get-ChildItem -Path $_.FullName -Include $Include -Recurse -Force -EA 0|
Select-Object -ExpandProperty FullName
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8312 次 |
| 最近记录: |