簡體   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