locked
Powershell shows as if it works - deletes duplicates with different ext but in fact doesn't delete anything RRS feed

  • Question

  • Hi

    I’m using PowerShell for the first time to delete duplicates with the ext. tif whenever theres already a PDF with the same name (but without deleting any tif without a duplicate).

    I found a script online and modified it.

    Here’s the script I ran.

    $Path = "e:\pts"

    ForEach ($File in (Get-ChildItem $Path\*.tif -Recurse))

    {   If (-not (Test-Path "$Path\$($File.Basename).pdf"))

        {   $File | Remove-Item -WhatIf

        }

    }

     

     

    The results I got seem to confirm the deletion of the duplicates but upon checking the directories and files themselves no tif files seem to have been deleted. Here is a sample of the results:

    PS C:\Users\me> E:\various files\powershell\delete tif if identical pdf exists 2 recurse.ps1

    What if: Performing the operation "Remove File" on target "E:\pts\00055162][1234567][20-06-2017-11-53]\654321\006051697_50.tif".

    What if: Performing the operation "Remove File" on target "E:\pts\00055162][1234567][20-06-2017-11-53]\654321\006051698_500.tif".

    What if: Performing the operation "Remove File" on target "E:\pts\00055162][1234567][20-06-2017-11-53]\654321\006051699_5009.tif".

    What if: Performing the operation "Remove File" on target "E:\pts\00055162][1234567][20-06-2017-11-53]\654321\006051700_50094א.tif".

     

     

    (I have Bitdefender Total Security on my home computer).

    What am I doing wrong?

    Thanks

    smplusr

    Saturday, August 12, 2017 9:26 AM

All replies

  • remove "whatif"


    \_(ツ)_/

    Saturday, August 12, 2017 9:33 AM
  • remove "whatif"


    \_(ツ)_/

    I would remove -not as well.   The logic i see: 

    If PDF doesnt exist, remove TIF.   Not sure this is what OP wants to :)

    Saturday, August 12, 2017 12:19 PM
  • Thanks

    Unfortunately it worked too well. All tif files were removed (with the "-not" option) instead of deleting only identical file names with the pdf ext and leaving those that do not have an identical name with a pdf ext.

    Any ideas how to make it delete just the duplicate tif's that have an identical pdf name while leaving those tif files that do not have an identical pdf name?


    Thanks again.

    Saturday, August 12, 2017 4:03 PM
  • Hi Smplusr 

    The below should remove any .tif files that have a .pdf file with the same fullname. If you put a # in front off | Remove-item it will just display the files that are going to be removed just to be sure it is removing what you want. 

    $files = Get-ChildItem -Recurse -File -Path e:\pts| Group-Object -Property Directory,BaseName | Where-Object Count -gt 1 | Select -Expand Group | Select FullName
    foreach ($file in $files) {
    $Dels = $file | ?{$_.FullName -like "*.tif"}
    foreach ($Del in $Dels){
    $Del.FullName | Remove-Item
    }
    }



    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.


    Saturday, August 12, 2017 6:37 PM
  • remove -not
    Saturday, August 12, 2017 6:40 PM
  • Thanks

    Unfortunately it worked too well. All tif files were removed (with the "-not" option) instead of deleting only identical file names with the pdf ext and leaving those that do not have an identical name with a pdf ext.

    Any ideas how to make it delete just the duplicate tif's that have an identical pdf name while leaving those tif files that do not have an identical pdf name?


    Thanks again.

    That happens when u just copy script from somewhere and dont understand the basics..
    Saturday, August 12, 2017 6:49 PM
  • Thank you!
    Sunday, August 13, 2017 12:12 AM
  • Hi,

    Was your issue resolved?

    And if the replies as above are helpful, we would appreciate you to mark them as answers, and if you resolve it using your own solution, please share your experience and solution here. It will be greatly helpful to others who have the same question.

    Appreciate for your feedback.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Friday, August 25, 2017 11:46 AM