locked
find and remove similar file RRS feed

  • Question

  • hi,

    Like a title i want to find a files similar like this exemple :

    ART-20200616102346-1070821.xml

    but just the end of the file like that : 1070821.xml

    I start to write a script in power shell like that : Get-ChildItem -Path C:\Users\adfransquin\Desktop\test | Where-Object { !$_.PSIsContainer -and $_.name -like "ART**.xml" }

    but i don't find how to do for say in powershell to find a files who is the end of their name is similar.

    I don't know if what i say it is clerly.

    kind regards

    adrien

    Friday, July 24, 2020 9:53 AM

Answers

  • thanks, you too
    Please mark comment as an answer that solve your problem

    The opinion expressed by me is not an official position of Microsoft

    Thursday, July 30, 2020 10:19 AM

All replies

  • Please use CodeBlock button in your answer form

    Get-ChildItem .\art\ -File -Filter ART*.xml -Recurse |
       select Name, FullName, @{n='Appendix';e={($_.Name -split '-' -replace '\.xml')[-1]}} |
          Group-Object Appendix


    The opinion expressed by me is not an official position of Microsoft

    Friday, July 24, 2020 10:56 AM
  • thank's for your answer but i misspoke

    i want delete all of files with the same name in the end end keep the last file modify.

    but in my BDD i don't know how to say look fo the similar files and delete the old files.

    Friday, July 24, 2020 11:41 AM
  • thank's for your answer but i misspoke

    i want delete all of files with the same name in the end end keep the last file modify.

    but in my BDD i don't know how to say look fo the similar files and delete the old files.

    I gave you a code which will find similar files based on appendixes. Did you try that code? You left check that its working fine and delete what you need

    The opinion expressed by me is not an official position of Microsoft

    Friday, July 24, 2020 11:53 AM
  • i try in my folder and i just find one file it 's dont work .

    I need remove a part of your script to do works :

    c:\Users\adfransquin\Documents> Get-ChildItem -Path C:\Users\adfransquin\Desktop\test -Filter ART*.xml -Recurse | select Name, FullName, @{n='Appendix';e={($_.Name -split '-' -replace '\.xml')[-1]}} | Group-Object Appendix
    
    
    Count Name                      Group                                                                                                                                                                                                
    ----- ----                      -----                                                                                                                                                                                                
        6 4036147                   {@{Name=ART-20200306144349-4036147.xml; FullName=C:\Users\adfransquin\Desktop\test\ART-20200306144349-4036147.xml; Appendix=4036147}, @{Name=ART-20200311090926-4036147.xml; FullName=C:\Users\adf...
    
    
    
    PS C:\Users\adfransquin\Documents> ls test
    ls : Impossible de trouver le chemin d'accès « C:\Users\adfransquin\Documents\test », car il n'existe pas.
    Au caractère Ligne:1 : 1
    + ls test
    + ~~~~~~~
        + CategoryInfo          : ObjectNotFound: (C:\Users\adfransquin\Documents\test:String) [Get-ChildItem], ItemNotFoundException
        + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
     
    
    PS C:\Users\adfransquin\Documents> cd ..
    
    PS C:\Users\adfransquin> cd desktop
    
    PS C:\Users\adfransquin\desktop> cd test
    
    PS C:\Users\adfransquin\desktop\test> Get-ChildItem .\art\ -File -Filter ART*.xml -Recurse |
       select Name, FullName, @{n='Appendix';e={($_.Name -split '-' -replace '\.xml')[-1]}} |
          Group-Object Appendix
    Get-ChildItem : Impossible de trouver le chemin d'accès « C:\Users\adfransquin\desktop\test\a », car il n'existe pas.
    Au caractère Ligne:1 : 1
    + Get-ChildItem .\art\ -File -Filter ART*.xml -Recurse |
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (C:\Users\adfransquin\desktop\test\a:String) [Get-ChildItem], ItemNotFoundException
        + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
     
    
    PS C:\Users\adfransquin\desktop\test> Get-ChildItem -Path C:\Users\adfransquin\Desktop\test -Filter ART*.xml -Recurse | select Name, FullName, @{n='Appendix';e={($_.Name -split '-' -replace '\.xml')[-1]}} | Group-Object Appendix
    
    
    Count Name                      Group                                                                                                                                                                                                
    ----- ----                      -----                                                                                                                                                                                                
        6 4036147                   {@{Name=ART-20200306144349-4036147.xml; FullName=C:\Users\adfransquin\Desktop\test\ART-20200306144349-4036147.xml; Appendix=4036147}, @{Name=ART-20200311090926-4036147.xml; FullName=C:\Users\adf...
    
    
    
    PS C:\Users\adfransquin\desktop\test> 

    Friday, July 24, 2020 12:20 PM
  • i do like that  but it is not fonctional

    PS C:\Users\adfransquin\desktop> ls .\test\
    
    
        Répertoire : C:\Users\adfransquin\desktop\test
    
    
    Mode                 LastWriteTime         Length Name                                                                                                                                                                               
    ----                 -------------         ------ ----                                                                                                                                                                               
    d-----        24/07/2020     10:43                Nouveau dossier                                                                                                                                                                    
    -a----        06/03/2020     14:44        1447737 4036147-0.pdf                                                                                                                                                                      
    -a----        11/03/2020     09:10        1498241 4036147-A.pdf                                                                                                                                                                      
    -a----        21/07/2020     08:47        1488703 4036147-B.pdf                                                                                                                                                                      
    -a----        24/07/2020     09:01        1526414 4036147-C.pdf                                                                                                                                                                      
    -a----        06/03/2020     14:44           2745 ART-20200306144349-4036147.xml                                                                                                                                                     
    -a----        11/03/2020     09:10           2745 ART-20200311090926-4036147.xml                                                                                                                                                     
    -a----        24/07/2020     09:01           2787 ART-20200724090054-4036147.xml                                                                                                                                                     
    -a----        06/03/2020     14:45           9711 BOM-20200306144349-4036147.xml                                                                                                                                                     
    -a----        11/03/2020     09:11          10505 BOM-20200311090926-4036147.xml                                                                                                                                                     
    -a----        21/07/2020     08:48          10505 BOM-20200721084704-4036147.xml                                                                                                                                                     
    -a----        24/07/2020     09:02          10505 BOM-20200724090054-4036147.xml                                                                                                                                                     
    
    
    
    PS C:\Users\adfransquin\desktop> Get-ChildItem .\test\ -File -Filter ART*.xml -Recurse |
       select Name, FullName, @{n='Appendix';e={($_.Name -split '-' -replace '\.xml')[-1]}} |
          Group-Object Appendix
    
    Count Name                      Group                                                                                                                                                                                                
    ----- ----                      -----                                                                                                                                                                                                
        6 4036147                   {@{Name=ART-20200306144349-4036147.xml; FullName=C:\Users\adfransquin\desktop\test\ART-20200306144349-4036147.xml; Appendix=4036147}, @{Name=ART-20200311090926-4036147.xml; FullName=C:\Users\adf...
    
    
    
    PS C:\Users\adfransquin\desktop> Get-ChildItem -Path C:\Users\adfransquin\Desktop\test -Filter ART*.xml -Recurse | select Name, FullName, @{n='Appendix';e={($_.Name -split '-' -replace '\.xml')[-1]}} | Group-Object Appendix
    
    
    Count Name                      Group                                                                                                                                                                                                
    ----- ----                      -----                                                                                                                                                                                                
        6 4036147                   {@{Name=ART-20200306144349-4036147.xml; FullName=C:\Users\adfransquin\Desktop\test\ART-20200306144349-4036147.xml; Appendix=4036147}, @{Name=ART-20200311090926-4036147.xml; FullName=C:\Users\adf...
    
    
    
    PS C:\Users\adfransquin\desktop> 

    Friday, July 24, 2020 12:27 PM
  • you have find not 1 file but 6 files with the same appendix 4036147

    on a screen you see groups of files (now only 1), and this groups based on file appendixes

    if you need to do something with those files inside each group you can goes though all groups with foreach

    <#previous code#> | foreach {
       write-host "working with a group '$($_.Name)'"
       write-host "files in the group:"
       $_.Group.FullName
    }


    The opinion expressed by me is not an official position of Microsoft

    Friday, July 24, 2020 12:54 PM
  • just to be clearly i have 2 type of files boom and art.

    and i must delete old files of each type with same appendixes.

    Monday, July 27, 2020 7:31 AM
  • just to be clearly i have 2 type of files boom and art.

    and i must delete old files of each type with same appendixes.

    its still not clear because your first post contain different information

    my code have found 6 files with the same appendixes. Dud you try 2nd code fragment?

    Orovide full description from start to the end and give clear example.


    The opinion expressed by me is not an official position of Microsoft


    • Edited by Vector BCO Monday, July 27, 2020 7:42 AM
    Monday, July 27, 2020 7:40 AM
  • my last result with your seconds code.

    PS C:\Users\adfransquin\desktop> Get-ChildItem .\test\Nouveau\ -File -Filter ART*.xml -Recurse |
       select Name, FullName, @{n='Appendix';e={($_.Name -split '-' -replace '\.xml')[-1]}} |
          Group-Object Appendix | foreach {
       write-host "working with a group '$($_.Name)'"
       write-host "files in the group:"
       $_.Group.FullName
    }
    
    working with a group '4036147'
    files in the group:
    C:\Users\adfransquin\desktop\test\Nouveau\ART-20200306144349-4036147.xml
    C:\Users\adfransquin\desktop\test\Nouveau\ART-20200311090926-4036147.xml
    C:\Users\adfransquin\desktop\test\Nouveau\ART-20200724090054-4036147.xml
    
    PS C:\Users\adfransquin\desktop>

    i try to explain to you what do i want.

    I have a BDD with a lot files BOOM and ART older and younger, and i want to rename in .old all older files art and BOOM.

    is it more clearly ?

    Monday, July 27, 2020 9:12 AM
  • Get-ChildItem .\art\ -File -Filter *.xml -Recurse | where {$_.Name -match '^(ART|BOOM)'} |
        select Name, FullName, @{n='Identificator';e={"$(($_.Name -split '-')[0])_$(($_.Name -split '-' -replace '\.xml')[-1])"}} |
           Group-Object Identificator | foreach {
        write-host "working with a group '$($_.Name)'" -ForegroundColor Green
        write-host "files in the group:"
        $_.Group.FullName
    }


    so now you have files grouped by prefix_sufix (2 lines changed from previous code samples). What has left find old files to delete? Lets go:

    Get-ChildItem .\art\ -File -Filter *.xml -Recurse | where {$_.Name -match '^(ART|BOOM)'} |
        select Name, FullName, @{n='Identificator';e={"$(($_.Name -split '-')[0])_$(($_.Name -split '-' -replace '\.xml')[-1])"}} |
           Group-Object Identificator | foreach {
        write-host "working with a group '$($_.Name)'" -ForegroundColor Green
        write-host "files in the group:"
        $_.Group.FullName
        Write-Host "Files which should be deleted:" -ForegroundColor Red
       $_.Group.FullName | Sort-Object | Select-Object -SkipLast 1
    }

    *Orange NTD mean "nothing to do" in a group. Only one file exists with the same suffix

    So if you have expected output here you can simply remove everything you need:

    Get-ChildItem .\art\ -File -Filter *.xml -Recurse | where {$_.Name -match '^(ART|BOOM)'} |
        select Name, FullName, @{n='Identificator';e={"$(($_.Name -split '-')[0])_$(($_.Name -split '-' -replace '\.xml')[-1])"}} |
           Group-Object Identificator | foreach {
        write-host "working with a group '$($_.Name)'" -ForegroundColor Green
        write-host "files in the group:"
        $_.Group.FullName
        
        $_.Group.FullName | Sort-Object | Select-Object -SkipLast 1 | Remove-Item -WhatIF
    }

    Was this 1 line complicated?

    BTW, I have no idea what is BDD


    The opinion expressed by me is not an official position of Microsoft

    • Edited by Vector BCO Monday, July 27, 2020 9:53 AM
    Monday, July 27, 2020 9:51 AM
  • i try it is very good.

    sorry BDD is database ( french expression).

    your last code do it remove item ? because i don't see any delete in my folder is it normal ?

    PS C:\Users\adfransquin\desktop> Get-ChildItem .\test\ -File -Filter *.xml -Recurse | where {$_.Name -match '^(ART|BOM)'} |
        select Name, FullName, @{n='Identificator';e={"$(($_.Name -split '-')[0])_$(($_.Name -split '-' -replace '\.xml')[-1])"}} |
           Group-Object Identificator | foreach {
        write-host "working with a group '$($_.Name)'" -ForegroundColor Green
        write-host "files in the group:"
        $_.Group.FullName
        
        $_.Group.FullName | Sort-Object | Select-Object -SkipLast 1 | Remove-Item  -WhatIF
    }
    
    working with a group 'ART_4036147'
    files in the group:
    C:\Users\adfransquin\desktop\test\ART-20200306144349-4036147.xml
    C:\Users\adfransquin\desktop\test\ART-20200311090926-4036147.xml
    C:\Users\adfransquin\desktop\test\ART-20200724090054-4036147.xml
    WhatIf : Opération « Supprimer le fichier » en cours sur la cible « C:\Users\adfransquin\desktop\test\ART-20200306144349-4036147.xml ».
    WhatIf : Opération « Supprimer le fichier » en cours sur la cible « C:\Users\adfransquin\desktop\test\ART-20200311090926-4036147.xml ».
    working with a group 'BOM_4036147'
    files in the group:
    C:\Users\adfransquin\desktop\test\BOM-20200306144349-4036147.xml
    C:\Users\adfransquin\desktop\test\BOM-20200311090926-4036147.xml
    C:\Users\adfransquin\desktop\test\BOM-20200721084704-4036147.xml
    C:\Users\adfransquin\desktop\test\BOM-20200724090054-4036147.xml
    WhatIf : Opération « Supprimer le fichier » en cours sur la cible « C:\Users\adfransquin\desktop\test\BOM-20200306144349-4036147.xml ».
    WhatIf : Opération « Supprimer le fichier » en cours sur la cible « C:\Users\adfransquin\desktop\test\BOM-20200311090926-4036147.xml ».
    WhatIf : Opération « Supprimer le fichier » en cours sur la cible « C:\Users\adfransquin\desktop\test\BOM-20200721084704-4036147.xml ».
    
    PS C:\Users\adfransquin\desktop> 

    but i jus want just rename my file rather that delete this file

    do i do that : rename-item " .old" to put this in the end of the file ?

    Monday, July 27, 2020 10:30 AM
  • Take a look on this: Remove-Item (whatif parameter)

    So if you agreed with proposed results - just remove -whatif parameter

    If you need Rename-Item (Example 4 should be helpfull) you can try to do similar thing with whatif and if you will get any problems please provide detailed explanation what is the problem and what you tries to do for solving it


    The opinion expressed by me is not an official position of Microsoft

    Monday, July 27, 2020 10:44 AM
  • ok thank 's it good to delete .

    for exemple i want rename file : ART-20200724090054-4036147.xml in ART-20200724090054-4036147.xml.old but i don't find how to do in the exemples.

    Monday, July 27, 2020 1:09 PM
  • ok thank 's it good to delete .

    for exemple i want rename file : ART-20200724090054-4036147.xml in ART-20200724090054-4036147.xml.old but i don't find how to do in the exemples.

    What did you try?
    I provide you a link and exact example

    95% work - done, for 5% provided a link, would be great if you will finish this work.
    For sure i can do this but what the reason?


    The opinion expressed by me is not an official position of Microsoft

    Monday, July 27, 2020 1:24 PM
  • i try that :

    working with a group 'BOM_4036147'
    files in the group:
    C:\Users\adfransquin\Desktop\test\BOM-20200306144349-4036147.xml
    C:\Users\adfransquin\Desktop\test\BOM-20200311090926-4036147.xml
    C:\Users\adfransquin\Desktop\test\BOM-20200721084704-4036147.xml
    C:\Users\adfransquin\Desktop\test\BOM-20200724090054-4036147.xml
    Rename-Item : Impossible de lier l'argument au paramètre « NewName », car il s'agit d'une chaîne vide.
    Au caractère C:\Users\adfransquin\Desktop\Sans titre2.ps1:8 : 88
    + ... tem -NewName { $_.Group.FullName -replace '.xml','.xml.old' } -WhatIf
    +                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidData : (C:\Users\adfran...349-4036147.xml:PSObject) [Rename-Item], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.RenameItemCommand
     
    Rename-Item : Impossible de lier l'argument au paramètre « NewName », car il s'agit d'une chaîne vide.
    Au caractère C:\Users\adfransquin\Desktop\Sans titre2.ps1:8 : 88
    + ... tem -NewName { $_.Group.FullName -replace '.xml','.xml.old' } -WhatIf
    +                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidData : (C:\Users\adfran...926-4036147.xml:PSObject) [Rename-Item], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.RenameItemCommand
     
    Rename-Item : Impossible de lier l'argument au paramètre « NewName », car il s'agit d'une chaîne vide.
    Au caractère C:\Users\adfransquin\Desktop\Sans titre2.ps1:8 : 88
    + ... tem -NewName { $_.Group.FullName -replace '.xml','.xml.old' } -WhatIf
    +                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidData : (C:\Users\adfran...704-4036147.xml:PSObject) [Rename-Item], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.RenameItemCommand
     
    
    PS C:\Users\adfransquin\Desktop> 

    Monday, July 27, 2020 1:59 PM
  • did you check what storred in $_.group? how many objects storred there?

    you can try store $_.group in some non automatic variable and check at the end, like $tmpVar = $_.group


    The opinion expressed by me is not an official position of Microsoft

    Monday, July 27, 2020 2:23 PM
  • Get-ChildItem .\art\ -File -Filter *.xml -Recurse | where {$_.Name -match '^(ART|BOOM)'} |
        select Name, FullName, @{n='Identificator';e={"$(($_.Name -split '-')[0])_$(($_.Name -split '-' -replace '\.xml')[-1])"}} |
           Group-Object Identificator | foreach {
        write-host "working with a group '$($_.Name)'" -ForegroundColor Green
        write-host "files in the group:"
        $_.Group.FullName
        
        $_.Group | Sort-Object Fullname | Select-Object -SkipLast 1 | Foreach {Rename-Item -WhatIF -Path $_.Fullname -NewName ($_.Name -replace '\.xml$','.xml.old')}
    }

    The opinion expressed by me is not an official position of Microsoft

    • Proposed as answer by Vector BCO Thursday, July 30, 2020 9:07 AM
    • Edited by Vector BCO Thursday, July 30, 2020 10:00 AM fixed 1 line
    Monday, July 27, 2020 2:29 PM
  • Adrien, how is going with your task? Have you got success?

    The opinion expressed by me is not an official position of Microsoft

    Thursday, July 30, 2020 9:09 AM
  • no yet. i have block to rename my extention files .

    i try this : 

     $_.Group | Sort-Object Fullname | Select-Object -SkipLast 1 | Rename-Item -WhatIF -NewName {$_.Name -replace '\.xml$','.xml.old'}
    }

    but it is not fonctional.

    and i try others tries but again it is not good.


    Thursday, July 30, 2020 9:16 AM
  • try to use ($_.Name -replace ....) instead of {$_.Name -replace ....}

    The opinion expressed by me is not an official position of Microsoft

    Thursday, July 30, 2020 9:19 AM
  • same result :

    working with a group 'BOM_4036147'
    files in the group:
    C:\Users\adfransquin\desktop\test\BOM-20200311090926-4036147.xml
    C:\Users\adfransquin\desktop\test\BOM-20200721084704-4036147.xml
    C:\Users\adfransquin\desktop\test\BOM-20200724090054-4036147.xml
    Rename-Item : Lecteur introuvable. Il n'existe aucun lecteur nommé « @{Name=BOM-20200311090926-4036147.xml; FullName=C ».
    Au caractère C:\Users\adfransquin\Documents\Sans titre3.ps1:7 : 71
    + ... kipLast 1 | Rename-Item -WhatIF -NewName ($_.Name -replace '\.xml$',' ...
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (@{Name=BOM-2020...xml; FullName=C:String) [Rename-Item], DriveNotFoundException
        + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.RenameItemCommand
     
    Rename-Item : Lecteur introuvable. Il n'existe aucun lecteur nommé « @{Name=BOM-20200721084704-4036147.xml; FullName=C ».
    Au caractère C:\Users\adfransquin\Documents\Sans titre3.ps1:7 : 71
    + ... kipLast 1 | Rename-Item -WhatIF -NewName ($_.Name -replace '\.xml$',' ...
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (@{Name=BOM-2020...xml; FullName=C:String) [Rename-Item], DriveNotFoundException
        + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.RenameItemCommand
     
    i don't know what to do. 

    Thursday, July 30, 2020 9:39 AM
  • ok, try this:

    $_.Group | Sort-Object Fullname | Select-Object -SkipLast 1 | foreach {Rename-Item -Path $_.fullname -WhatIF -NewName($_.Name -replace '\.xml$','.xml.old')}

    previous answer updated


    The opinion expressed by me is not an official position of Microsoft

    • Edited by Vector BCO Thursday, July 30, 2020 10:01 AM
    Thursday, July 30, 2020 9:55 AM
  • amazing. 

    it is fonctional thank you for everythink.

    have a good day VECTOR.

    Thursday, July 30, 2020 10:16 AM
  • thanks, you too
    Please mark comment as an answer that solve your problem

    The opinion expressed by me is not an official position of Microsoft

    Thursday, July 30, 2020 10:19 AM
  • Hallo Adrien,

    pls try it. May it help you. Thanks.

    you need to think about your $pathh

    $pathh = "C:\Users\Username\Desktop\three"
    $del = gci -Path "$pathh" -Filter "art*.xml" | Where-Object {$_.name -match "1070821"}

    $del | Remove-Item

    Arif


    Monday, August 10, 2020 9:06 AM