jpm*_*c26 6 powershell filepath get-childitem
首先我要说的是,我看过无法使用Powershell中的Get-ChildItem -Exclude参数排除目录,如何使用Get-ChildItem -exclude排除多个文件夹?.这些都没有解决我的问题的答案.
我需要递归搜索具有特定扩展名的文件的目录.为简单起见,我们只想说我需要找到*.txt
.通常,这个命令就足够了:
Get-ChildItem -Path 'C:\mysearchdir\' -Filter '*.txt' -Recurse
Run Code Online (Sandbox Code Playgroud)
但我有一个重大问题.内部有一个node_modules
目录C:\mysearchdir\
,NPM创建了非常深的嵌套目录.(它的详细信息是NPM托管目录,这一点很重要,因为这意味着深度超出了我的控制范围.)这会导致以下错误:
Get-ChildItem : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.
Run Code Online (Sandbox Code Playgroud)
我相信这个错误来自.NET IO库中的限制.
我不能很容易地搜索周围的其他目录.它不在目录的顶部; 它更深入,比如说C:\mysearchdir\dir1\dir2\dir3\node_modules
,并且我需要在所有这些级别搜索目录.因此,只需添加更多文件和目录,只需搜索其周围的其他目录就会很麻烦,而且维护得不够.
我试过-Exclude
参数没有任何成功.这并不奇怪,因为我刚刚看了那个-Exclude
之后的结果是牵强仅适用.我找不到任何有关使用的真实信息-Filter
(如本答案中所述).
有什么方法可以开始Get-ChildItem
工作,还是我不能写自己的递归遍历?
哦,伙计,我感到愚蠢.我遇到了和你一样的问题.当我进入"-EA SilentlyContinue"部分时,我正在使用@ DarkLite1的答案,尝试解析它.
捂脸!
这就是你所需要的!
这对我有用,试一试:
Get-ChildItem -Path 'C:\mysearchdir\' -Filter '*.txt' -Recurse -ErrorAction SilentlyContinue
Run Code Online (Sandbox Code Playgroud)
注意:这不会从搜索中排除node_modules,只是隐藏遍历长路径生成的任何错误.如果您需要完全排除它,您将需要一个更复杂的解决方案.
也许你可以尝试这样的事情:
$Source = 'S:\Prod'
$Exclude = @('S:\Prod\Dir 1', 'S:\Prod\Dir 2')
Get-ChildItem -LiteralPath $Source -Directory -Recurse -PipelineVariable Dir -EV e -EA SilentlyContinue |
Where {($Exclude | Where {($Dir.FullName -eq "$_") -or ($Dir.FullName -like "$_\*")}).count -eq 0}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1328 次 |
最近记录: |