1

I have this script that compares 2 directories with each other if it matches it copies it to the other directory. But i need 2 folders to be excluded on the source folder because there are old files there. I excluded one folder but i can't add an second one. Can someone help me out? (I'm a beginner in Powershell) I know the foreach loop is empty, this is for testing purposes.

$aDir = "C:\Replace TEST SCRIPT\A"
$bDir = "C:\Replace TEST SCRIPT\Y"

$aFiles = Get-ChildItem -Path "$bDir\" -Exclude "Folder1","Folder2" | Get-ChildItem -Path "$bDir\*.pdf" -Recurse -File | select -exp FullName
ForEach ($file in $aFiles) {
    $laatste = (Get-Item $file).LastWriteTime
    $filenaam = Split-Path -Path "$file" -Leaf
    if(Test-Path -Path "C:\Replace TEST SCRIPT\A\$filenaam") {
        Write-Output "$filenaam exists in $aDir. Copying."         
        Copy-Item -Path "$file" -Recurse -Destination "$aDir" 
    } else {        
    }
}


OwenM
  • 13
  • 1
  • 5
  • Does this answer your question? [Copy-item exclude Sub-folders](https://stackoverflow.com/questions/57858059/copy-item-exclude-sub-folders) – Theo Mar 05 '20 at 10:04
  • I'ts not a subfolder, it is just a folder in $bDir – OwenM Mar 05 '20 at 10:11
  • Then why the title _Exclude multiple folders_. If a folder is inside another folder, then... it **IS** a subfolder. – Theo Mar 05 '20 at 10:21

1 Answers1

2

You can do it the following way:

$aFiles = Get-ChildItem -Path "$bDir\" -Exclude "PDF","folder2" | Get-ChildItem -filter "*.pdf" -Recurse -File | select -exp FullName

Btw. There is already a post to your certain question: How can I exclude multiple folders using Get-ChildItem -exclude?

Please take a look at it and let us know.

Nicicalu
  • 759
  • 7
  • 16