0

I'm not sure how I should be excluding the following: .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++
}

Thank you =]

2 Answers 2

1

a couple of notes: * Try using wildcards, to apply exclusion, like: *.env * Copy-Item parameter Source, allows using collection of type String. Using collection should be faster, than processing sequential with foreach! * If you need only the files, you may consider using Get-ChildItem -File

You may try something like:

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

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

Hope it helps!

Sign up to request clarification or add additional context in comments.

Comments

1

To exclude both certain files like '*.env' and 'web.config' AND also exclude a folder with a certain name, you could do this:

$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++
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.