2

I am writing a simple DLL copying script to help my dev team in setting up their local environments. I check the build devdrop folder and get a list of DLL files. I then look at the local folder and copy any newer DLLs over.

My problem is when the local folder is empty (i.e. the first time the script runs) it all falls apart.

$newFiles = Get-ChildItem -recurse -path "$drop\$latest\ship" -name
$oldFiles = Get-ChildItem -recurse -path $bin -name

$files = Compare-Object $newFiles $oldFiles -IncludeEqual -PassThru
$files | Where-Object { $_.SideIndicator -eq '<=' -or $_.SideIndicator -eq '=='} | % {
    Copy-Item -Path "$drop\$latest\ship\$_" -Destination $bin -Force
}

When $bin is empty the gci call leaves $oldFiles as null, giving me a nice error:

Compare-Object : Cannot bind argument to parameter 'DifferenceObject' because it is null.

I have searched around and all how-tos for Compare-Object don't seem to deal with this. I could check if the folder is empty and then copy all the files over, but I was wondering if there was a better way.

Cheers.

2
  • 1
    What's the problem with checking that a folder is empty? It's just one extra line of code. Commented Nov 22, 2012 at 2:42
  • Mainly because I want to use the Compare-Object's SideIndicator. Later on in the script I need to identify DLLs in the local folder which are no longer shipped (i.e. check for =>). Hopefully this question may help someone in future who wants to do something similar. Commented Nov 22, 2012 at 3:51

1 Answer 1

6
$newFiles = @(Get-ChildItem -recurse -path "$drop\$latest\ship" -name)
$oldFiles = @(Get-ChildItem -recurse -path $bin -name)
$files = Compare-Object -ReferenceObject $newFiles -DifferenceObject $oldFiles -IncludeEqual -PassThru
Sign up to request clarification or add additional context in comments.

2 Comments

That is so simple it is embarrassing I didn't think of it.
+1. At some point I wrote one page of sync script. This could have saved me some time. :)

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.