如何使用 Get-ChildItem -exclude 排除多个文件夹? [英] How can I exclude multiple folders using Get-ChildItem -exclude?
问题描述
我需要为我们的 Pro/Engineer CAD 系统生成一个配置文件.我需要来自我们服务器上特定驱动器的文件夹的递归列表.但是,我需要排除任何包含ARCHIVE"的文件夹,包括各种不同的情况.
I need to generate a configuration file for our Pro/Engineer CAD system. I need a recursive list of the folders from a particular drive on our server. However I need to EXCLUDE any folder with 'ARCHIVE' in it including the various different cases.
我写了以下内容,但不排除文件夹!
I've written the following which works except it doesn't exclude the folders !!
$folder = "T:DrawingsDesign*"
$raw_txt = "T:Design ProjectsDesign_AdminPowerShell
aw.txt"
$search_pro = "T:Design ProjectsDesign_AdminPowerShellsearch.pro"
$archive = *archive*,*Archive*,*ARCHIVE*
Get-ChildItem -Path $folder -Exclude $archive -Recurse | where {$_.Attributes -match 'Directory'} | ForEach-Object {$_.FullName} > $search_pro
推荐答案
我的 KISS 跳过某些文件夹的方法是链接 Get-ChildItem
调用.这不包括根级别文件夹,但不包括更深级别的文件夹(如果这是您想要的).
My KISS approach to skip some folders is chaining Get-ChildItem
calls. This excludes root level folders but not deeper level folders if that is what you want.
Get-ChildItem -Exclude folder1,folder2 | Get-ChildItem -Recurse | ...
- 开始排除您不想要的文件夹
- 然后进行递归搜索,排除不需要的文件夹.
- Start excluding folders you don't want
- Then do the recursive search with non desired folders excluded.
我喜欢这种方法的一点是它简单易记.如果您不想在第一次搜索中混合文件夹和文件,则需要使用过滤器.
What I like from this approach is that it is simple and easy to remember. If you don't want to mix folders and files in the first search a filter would be needed.
这篇关于如何使用 Get-ChildItem -exclude 排除多个文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!