相关疑难解决方法(0)

如何使用Get-ChildItem -exclude排除多个文件夹?

我需要为我们的Pro/Engineer CAD系统生成配置文件.我需要一个来自我们服务器上特定驱动器的文件夹的递归列表.但是我需要在其中排除任何带有"ARCHIVE"的文件夹,包括各种不同的情况.

我写了以下哪些有效但除了它不排除文件夹!!

$folder = "T:\Drawings\Design\*"
$raw_txt = "T:\Design Projects\Design_Admin\PowerShell\raw.txt"
$search_pro = "T:\Design Projects\Design_Admin\PowerShell\search.pro"
$archive = *archive*,*Archive*,*ARCHIVE*

Get-ChildItem -Path $folder -Exclude $archive -Recurse  | where {$_.Attributes -match 'Directory'}  | ForEach-Object {$_.FullName} > $search_pro   
Run Code Online (Sandbox Code Playgroud)

directory powershell list

41
推荐指数
7
解决办法
11万
查看次数

如何防止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工作,还是我不能写自己的递归遍历?

powershell filepath get-childitem

6
推荐指数
2
解决办法
1328
查看次数

如何直接通过管道传输到 Copy-Item 而不是在 ForEach-Object 内

由于使用 -Recurse 标志时,Get-ChildItem 的 -Exclude 参数不会对子文件夹进行过滤,因此请参阅使用 get-childitem-exclude-parameter-in-powershell 中的其他无法排除目录的内容

但 -Exclude 参数可用于过滤掉根级别的文件夹

我写了自己的递归函数:

function Get-ChildItem-Recurse() {
    [cmdletbinding()]
    Param(
      [parameter(ValueFromPipelineByPropertyName = $true)]
      [alias('FullName')]
      [string[]] $Path,
      [string] $Filter,
      [string[]] $Exclude,
      [string[]] $Include,
      [switch] $Recurse = $true,
      [switch] $File = $false
    )

    Process {
      ForEach ( $P in $Path ) {
        Get-ChildItem -Path $P -Filter $Filter -Include $Include -Exclude $Exclude | ForEach-Object {
        if ( -not ( $File -and $_.PSIsContainer ) ) {
          $_
        }
        if ( $Recurse -and $_.PSIsContainer ) {
          $_ …
Run Code Online (Sandbox Code Playgroud)

powershell pipeline copy-item foreach-object

2
推荐指数
1
解决办法
915
查看次数