繁体   English   中英

在Powershell中使用get-childitem时排除文件夹和文件

[英]Exclude folder and files when using get-childitem in powershell

我不确定应该如何排除以下内容: .env, web.config, node_modules

$sourceRoot = "C:\Users\Wade Aston\Desktop\STEMuli\server"
$destinationRoot = "C:\Users\Wade Aston\Desktop\STEMuli/server-sandbox"
$dir = get-childitem $sourceRoot -Exclude .env, web.config, node_modules <-- How do I exclude these       
$i=1
$dir| %{
    [int]$percent = $i / $dir.count * 100
    Write-Progress -Activity "Copying ... ($percent %)" -status $_  -PercentComplete $percent -verbose
    $_ | copy -Destination $destinationRoot  -Recurse -Force
    $i++
}

谢谢=]

一些注意事项:*尝试使用通配符来应用排除,例如:* .env * Copy-Item参数Source,允许使用String类型的集合。 与使用foreach顺序处理相比,使用collection应该更快。 *如果仅需要文件,则可以考虑使用Get-ChildItem -File

您可以尝试以下方法:

$Source = Get-ChildItem -Path C:\TEMP -Exclude dism.log, *.csv 
$dest = 'C:\temp2'

Copy-Item -Path $Source -Destination $dest -Force

希望能帮助到你!

要排除“ * .env”和“ web.config”之类的某些文件并排除具有特定名称的文件夹,可以执行以下操作:

$sourceRoot      = "C:\Users\Wade Aston\Desktop\STEMuli\server"
$destinationRoot = "C:\Users\Wade Aston\Desktop\STEMuli\server-sandbox"

$dir = Get-ChildItem -Path $sourceRoot -Recurse -File -Exclude '*.env', 'web.config' | 
       Where-Object{ $_.DirectoryName -notmatch 'node_modules' }

$i = 1
$dir | ForEach-Object {
    [int]$percent = $i / $dir.count * 100
    Write-Progress -Activity "Copying ... ($percent %)" -Status $_  -PercentComplete $percent -Verbose

    $target = Join-Path -Path $destinationRoot -ChildPath $_.DirectoryName.Substring($sourceRoot.Length)
    # create the target destination folder if it does not already exist
    if (!(Test-Path -Path $target -PathType Container)) {
        New-Item -Path $target -ItemType Directory | Out-Null
    }
    $_ | Copy-Item -Destination $target -Force
    $i++
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM