Compare, Delete and copy

This topic contains 7 replies, has 2 voices, and was last updated by Profile photo of Mikhael Kaendera Mikhael Kaendera 2 years, 1 month ago.

  • Author
    Posts
  • #24414
    Profile photo of Mikhael Kaendera
    Mikhael Kaendera
    Participant

    Hi there,

    I am trying to put tohether a script that compares two folders.
    – If an item is found in the $fso folder but not in $fsoBU delele the item.
    -If an item is found in $fsoBU folder but not in the $fso folder copy the item.

    $fso = Get-ChildItem -Recurse -path "\\vfs04\Users$\kaendm\Desktop\Test"

    $fsoBU = Get-ChildItem -Recurse -path "\\vfs04\Users$\kaendm\Desktop\Old Files"

    # Compare the two folders
    $matches = compare-Object -ReferenceObject $fso -DifferenceObject $fsoBU -IncludeEqual

    foreach ($fso in $matches)
    {
    # If an item is $fso folder and not in $fsoBU folder delete the item.

    { if ($fso -NotMatch)
    Remove-Item \\vfs04\Users$\kaendm\Desktop\test\*.ini

    # if the an item is in fsoBU folder, but not in $fso folder copy the item to $fso. Copy the missing files.

    else
    Copy-Item fsoBU $fso -recurse -force
    }

  • #24419
    Profile photo of Rob Simmers
    Rob Simmers
    Participant
    $files = Get-ChildItem -Path C:\Users\rsimmers\Desktop -File | Select Name, FullName
    $files2 = Get-ChildItem -Path C:\Users\rsimmers\Desktop\Archive -File | Select Name, FullName
    
    Compare-Object -ReferenceObject $files -DifferenceObject $files2 -Property Name -PassThru |
    foreach {
       $fullPath = $_.FullName
       switch ($_.SideIndicator) {
           "=>"{Remove-Item -Path $fullPath -WhatIf}
           "< ="{Copy-Item -Path $fullPath -Destination "C:\Temp" -WhatIf}
       } 
    }
    
  • #24422
    Profile photo of Mikhael Kaendera
    Mikhael Kaendera
    Participant

    Rob,

    Were you able to get the script to run? When I run it I am get the attached error messages. See attached.

    $files = Get-ChildItem -Path "\\vfs04\Users$\kaendm\Desktop\Test" -File | Select Name, FullName
    $files2 = Get-ChildItem -Path "\\vfs04\Users$\kaendm\Desktop\Old Files" -File | Select Name, FullName

    Compare-Object -ReferenceObject $files -DifferenceObject $files2 -Property Name -PassThru |
    foreach {
    $fullPath = $_.FullName
    switch ($_.SideIndicator) {
    "=>"{Remove-Item -Path $fullPath -WhatIf}
    "<=""{Copy-Item -Path $fullPath -Destination "C:\Temp" -WhatIf} } }

  • #24424
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    There is an extra double quote before Copy-Item

  • #24425
    Profile photo of Mikhael Kaendera
    Mikhael Kaendera
    Participant

    Thank you

  • #24426
    Profile photo of Mikhael Kaendera
    Mikhael Kaendera
    Participant

    Rob ,

    I know there are so many powershell books on the market. What book would you recommend for beginners.

  • #24428
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    Personally, I have not read it, but Don Jones' book for Learn Powershell in a Month of Lunches. It's broken up into chunks to learn at a time, so it should be easy to pick up where you left off andcan learn at your own pace.

  • #24429
    Profile photo of Mikhael Kaendera
    Mikhael Kaendera
    Participant

    Thank you very much I will look it up.

You must be logged in to reply to this topic.