none
PowerShell - Delete file with extension if further file with extension exists RRS feed

  • Question

  • Hi there,

    is there a way to delte a file with specific extension if this file also exists with further extension?

    Example:

    Folder holds:

    123.txt

    123.txt.log

    456.txt

    Run delete 123.txt when 123.txt.log also exists

    Folder holds:

    456.txt

    Thursday, November 10, 2016 7:54 AM

Answers

  • So you might change your question.  ;-)

    With Get-Item or Get-ChildItem you get a file system object and with this you can seperate the actual file name and the extension.

    $File = Get-Item -Path <your Path>\<your FileName>
    $File.BaseName
    $File.Extension
    With that you can compare whatever you need to compare and act accordingly.



    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    • Marked as answer by RenéSchell Monday, November 14, 2016 10:27 AM
    Thursday, November 10, 2016 1:02 PM

All replies

  • del 123.txt


    \_(ツ)_/

    Thursday, November 10, 2016 8:48 AM
  • I guess you don't understand. File *.txt should be deleted only if the *.txt.log for this file exists.
    Thursday, November 10, 2016 9:23 AM
  • If you had asked that I would have answered it.

    if(Test-Path 123.txt.log){del 123.txt }


    \_(ツ)_/

    Thursday, November 10, 2016 9:28 AM
  • That is not what I am searching for..but thanks anyway.
    Thursday, November 10, 2016 9:39 AM
  • So you might change your question.  ;-)

    With Get-Item or Get-ChildItem you get a file system object and with this you can seperate the actual file name and the extension.

    $File = Get-Item -Path <your Path>\<your FileName>
    $File.BaseName
    $File.Extension
    With that you can compare whatever you need to compare and act accordingly.



    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    • Marked as answer by RenéSchell Monday, November 14, 2016 10:27 AM
    Thursday, November 10, 2016 1:02 PM
  • cd C:\temp
    
    # Get all files with extension .loaded within the folder
    $FileLoad = Get-Item -Path *.loaded
    
    # Delete specific file within the folder
    del *.discarded
    
    # Get all files with extension .txt within the folder for each file with extesion .loaded
    ForEach($objLoad in $FileLoad){
        $FileOri = Get-Item -Path *.txt
        
        # For each file with extension .txt check if file (.loaded) with equal basename exists and delete
        ForEach($objOri in $FileOri){
            if($objOri.Name -eq $objLoad.BaseName){
                Remove-Item $objOri.Name 
                Remove-Item $objLoad.Name
            }
        }
    }
    Thanks a lot. That is what I came up with. Maybe you've got some corrections, but it works.

    Friday, November 11, 2016 4:01 PM
  • New one..old one was to complicated

    cd C:\temp
    
    # Get all files with extension .loaded within the folder
    $FileLoad = Get-Item -Path *.loaded
    
    # Delete specific file within the folder
    Remove-Item *.discarded
    Remove-Item *.available
    Remove-Item *.bulkstatement
    Remove-Item *.bulkformat
    
    # Delete all files with extension .loaded and basename (includes .csv)
    ForEach($objLoad in $FileLoad){
        Remove-Item $objLoad.BaseName
        Remove-Item $objLoad
    }


    • Edited by RenéSchell Friday, November 11, 2016 4:53 PM
    Friday, November 11, 2016 4:51 PM