locked
find files with same basename but different extension and remove RRS feed

  • Question

  • Hi guys, having just basic scripting skills I hope you can help me. Have seen so many great solutions here.

    My problem:

    I need to search all my drives/folders for files of the form "file.ext.abcdef" and lookup if a file with the same basename but without the 6 character extension exists e.g. "file.ext". If this condition is true the file with the 6 character extension should be deleted.

    Hope you can help me.

    Many thanks in advance

    Gerhard

     

    Tuesday, March 7, 2017 9:52 AM

Answers

  • gci R:\Test_temp\E-Drive\* -include *.*.?????? -recurse | foreach {
        if(test-path (join-path $_.directory $_.basename)) {
            remove-item $_ -whatif
        }
    }

    • Marked as answer by Golkun Wednesday, March 8, 2017 12:04 AM
    Tuesday, March 7, 2017 10:48 PM

All replies

  • Tuesday, March 7, 2017 10:39 AM
  • Fair enough, but I wouldn't have asked if didn't find many other post where similar questions have been asked and there where people who were eager to help. Never mind I'm just at the beginning to get into it and had expected to get some hints.

    For a start I have the following to get the files with the 6 character additional extension, but don't know how to compare with the files without the 6 character extension.

    $Path = "R:\Test_temp\E-Drive\*"

    ForEach ($File in (Get-ChildItem -Path $Path -Include "*.*.[a-z][a-z][a-z][a-z][a-z][a-z]" -Recurse))
        {
        $File

          }

    Any hint would be much appreciated

    regards

    Gerhard

    • Edited by Golkun Tuesday, March 7, 2017 10:31 PM
    Tuesday, March 7, 2017 10:18 PM
  • gci R:\Test_temp\E-Drive\* -include *.*.?????? -recurse | foreach {
        if(test-path (join-path $_.directory $_.basename)) {
            remove-item $_ -whatif
        }
    }

    • Marked as answer by Golkun Wednesday, March 8, 2017 12:04 AM
    Tuesday, March 7, 2017 10:48 PM
  • Hello Leif-Arne, you're awesome! Many thanks this works great.

    regards

    Gerhard

    Wednesday, March 8, 2017 12:01 AM