Excluding Folders with Get-ChildItem – 需要帮助调试脚本

huangapple go评论63阅读模式
英文:

Excluding Folders with Get-ChildItem - Need Help Debugging a Script

问题

我已经搜索了StackOverflow和SuperUser两个网站,试图找出问题的解决方法,但我仍然被一个我无法解决的问题困扰着。我知道这是一件简单的事情,但经过一个小时的尝试后,我仍然一筹莫展。简单的问题是:我怎么告诉Get-Childitem排除文件夹?

在这里,首先是不起作用的代码:

$sourceDir="E:\Deep Storage"
$targetDir="W:\Deep Storage"
$excludeThese = 'Projects2','Projects3','Projects4';

Get-ChildItem -Path $sourceDir -Directory -Recurse | 
  where {$_.fullname -notin $excludeThese} |
    Get-ChildItem -Path $sourceDir | ForEach-Object {
        $num=1
        $nextName = Join-Path -Path $targetDir -ChildPath $_.name
    
        while(Test-Path -Path $nextName)
        {
           $nextName = Join-Path $targetDir ($_.BaseName + "_$num" + $_.Extension)    
           $num+=1   
        }

        $_ | Move-Item -Destination $nextName -Force -Verbose -WhatIf
    }

这里的基本概念已经起作用:

$sourceDir="E:\Deep Storage"
$targetDir="W:\Deep Storage"

Get-ChildItem -Path $sourceDir -File -Recurse | ForEach-Object {
    $num=1
    $nextName = Join-Path -Path $targetDir -ChildPath $_.name

    while(Test-Path -Path $nextName)
    {
       $nextName = Join-Path $targetDir ($_.BaseName + "_$num" + $_.Extension)    
       $num+=1   
    }

    $_ | Copy-Item -Destination $nextName -Verbose
}

基本上,这两段代码的功能是将文件夹从一个地方移动到另一个地方,如果两个地方都存在文件,它会重命名进来的文件。这有助于保持我的归档驱动器清晰。但有三个文件夹我想要排除,因为我经常从中提取资源,所以我不需要移动这些文件。

因此,这两段代码示例之间的区别在于,在第一段中,我试图让Get-Childitem排除一个特定的文件夹三元组,而在第二段中,它只一次性获取所有内容。

我尝试过直接使用-Exclude和$excludeThese作为变量,但没有成功;我尝试跳过变量方法,直接在-Exclude之后放入文件夹名称。仍然不起作用。我还尝试将要排除的文件夹的整个路径放进去。无济于事 - 不管我做什么,-WhatIf 都显示脚本试图移动一切,包括我理论上要排除的文件夹。

我最后尝试的技巧是我在这里找到的一个,那就是首先用排除参数进行一次gci,然后再进行另一次gci。这仍然失败了,所以现在我必须求助于专家。

英文:

I've searched through both StackOverflow and SuperUser to try to figure this out, and I'm still getting plagued by a problem I can't figure out how to fix. I know it's something simple, but after playing with it for an hour I'm still stumped. Simple question: how the heck do I tell Get-Childitem to exclude folders?

Right up front here's the code that doesn't work:

$sourceDir="E:\Deep Storage"
$targetDir="W:\Deep Storage"
$excludeThese = 'Projects2','Projects3','Projects4';

Get-ChildItem -Path $sourceDir -Directory -Recurse | 
  where {$_.fullname -notin $excludeThese} |
    Get-ChildItem -Path $sourceDir | ForEach-Object {
        $num=1
        $nextName = Join-Path -Path $targetDir -ChildPath $_.name
    
        while(Test-Path -Path $nextName)
        {
           $nextName = Join-Path $targetDir ($_.BaseName + "_$num" + $_.Extension)    
           $num+=1   
        }

        $_ | Move-Item -Destination $nextName -Force -Verbose -WhatIf
    }
}

The underlying concept here already works:

$sourceDir="E:\Deep Storage"
$targetDir="W:\Deep Storage"

Get-ChildItem -Path $sourceDir -File -Recurse | ForEach-Object {
    $num=1
    $nextName = Join-Path -Path $targetDir -ChildPath $_.name

    while(Test-Path -Path $nextName)
    {
       $nextName = Join-Path $targetDir ($_.BaseName + "_$num" + $_.Extension)    
       $num+=1   
    }

    $_ | Copy-Item -Destination $nextName -Verbose
}

Basically what this does is it moves folders from one place to another, and if files exist in both places, it renames files coming in. It helps keep my archive drive clear. But there are three folders there that I want to exclude because I still pull assets from them regularly, so I don't need those files moved.

Hence the difference between the two code samples: in the first one, I'm trying to get Get-Childitem to exclude a specific trio of folders, while this second one just grabs everything all at once.

I tried just doing a straight -Exclude with $excludeThese as the variable, without success; I tried skipping the variable approach altogether and just putting the folder names in after -Exclude. Still didn't work. I also tried putting in the entire path to the folders I wanted to exclude. No good--no matter what I did, the -WhatIf showed that the script was trying to move everything, including the folders I was theoretically excluding.

The last trick I tried was one I came across here on SO, and that was to go a gci with the exclude argument first, then do another gci after it. That still failed, so now I have to turn to the experts for help.

答案1

得分: 1

以下是翻译好的部分:

# 使用从(已转义的)目录名称创建的正则表达式字符串来排除确保忽略这些文件夹中的文件。
# 同时,通过使用目标文件夹中所有已存在文件名的查找 Hashtable,可以快速确定是否已存在具有特定名称的文件。

$sourceDir    = 'E:\Deep Storage'
$targetDir    = 'W:\Deep Storage'
$excludeThese = 'Projects2','Projects3','Projects4';

# 使用所有要排除的文件夹名称创建正则表达式字符串,并使用正则表达式 OR(|)组合它们
$excludeDirs = ($excludeThese | ForEach-Object { [Regex]::Escape($_) }) -join '|';

# 创建查找 Hashtable,并存储目标文件夹中已存在的文件名
$existingFiles = @{}
Get-ChildItem -Path $targetDir -File | ForEach-Object { $existingFiles[$_.Name] = $true }

Get-ChildItem -Path $sourceDir -File -Recurse | 
Where-Object {$_.DirectoryName -notmatch $excludeDirs} |
ForEach-Object {
    # 构建新文件名,如果需要,附加索引号
    $newName = $_.Name
    $count   = 1
    while ($existingFiles.ContainsKey($newName)) {
        $newName = "{0}_{1}{2}" -f $_.BaseName, $count++, $_.Extension
    }
    # 将此新名称添加到 Hashtable 中,以便在下一次运行中存在
    $existingFiles[$newName] = $true
    # 使用 Join-Path 创建文件的 FullName
    $newFile = Join-Path -Path $targetDir -ChildPath $newName
    $_ | Move-Item -Destination $newFile -Force -Verbose -WhatIf
}
英文:

I would use a regex string created from the (escaped) directory names to exclude to make sure files withing these folders are ignored.
Also, by using a lookup Hashtable of all file names already present in the target folder, figuring out if a file with a certain name already exists is extremely fast.

$sourceDir    = 'E:\Deep Storage'
$targetDir    = 'W:\Deep Storage'
$excludeThese = 'Projects2','Projects3','Projects4';

# create a regex string with all folder names to exclude combined with regex OR (|)
$excludeDirs = ($excludeThese | ForEach-Object { [Regex]::Escape($_) }) -join '|'
# create a lookup Hashtable and store the filenames already present in the destination folder
$existingFiles = @{}
Get-ChildItem -Path $targetDir -File | ForEach-Object { $existingFiles[$_.Name] = $true }

Get-ChildItem -Path $sourceDir -File -Recurse | 
Where-Object {$_.DirectoryName -notmatch $excludeDirs} |
ForEach-Object {
    # construct the new filename by appending an index number if need be
    $newName = $_.Name
    $count   = 1
    while ($existingFiles.ContainsKey($newName)) {
        $newName = "{0}_{1}{2}" -f $_.BaseName, $count++, $_.Extension
    }
    # add this new name to the Hashtable so it exists in the next run
    $existingFiles[$newName] = $true
    # use Join-Path to create a FullName for the file
    $newFile = Join-Path -Path $targetDir -ChildPath $newName
    $_ | Move-Item -Destination $newFile -Force -Verbose -WhatIf
}

答案2

得分: 0

假设要排除的目录位于顶部:

$sourceDir = "E:\Deep Storage"
$excludeThese = 'Projects2', 'Projects3', 'Projects4'
get-childitem $sourceDir -exclude $excludeThese | get-childitem -recurse


<details>
<summary>英文:</summary>

Assuming the excluded directories are at the top:

$sourceDir="E:\Deep Storage"
$excludeThese = 'Projects2','Projects3','Projects4'
get-childitem $sourcedir -exclude $excludethese | get-childitem -recurse



</details>



huangapple
  • 本文由 发表于 2023年2月19日 10:25:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497630.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定