Powershell deletion script for files with recurring date extension like .20181104, 20181105 etc










0















All,



This is my script to delete log files on one of our PTV servers. the issue here is that the logs files have been named in a way that shows different date extensions like "blah.log.20181102", "blah.log.20181103"



My dilemma is how to remove files with different dates at the end. I was want to get the number of files deleted so I am using the measure-object but it keeps giving me a count of "0" even after deletion. Below is my script:



$limit = (Get-Date).AddDays(-30)
$path = 'C:Usersuyr2f3dDownloadsHello1'
$path = 'C:Usersuyr2f3dDownloadsHello2'
$path = 'C:Usersuyr2f3dDownloadsHello3'

Get-Childitem -Path @(
'C:Usersuyr2f3dDownloadsHello1*'
'C:Usersuyr2f3dDownloadsHello2*'
'C:Usersuyr2f3dDownloadsHello3*'
) -Include '*.tmp', '*.log' -Force -Recurse |
Where-Object -not $_.PSIsContainer -and $_.LastWriteTime -lt $limit |
Remove-Item | ForEach-Object $_

Measure-Object
Get-Date
Read-Host -Prompt 'The files have been deleted successfully'


I want a prompt showing number of files deleted and when it was deleted. That is what the read-host prompt is for. I am open to simplifying and making this query better since its also deleting files from 3 different folders.










share|improve this question




























    0















    All,



    This is my script to delete log files on one of our PTV servers. the issue here is that the logs files have been named in a way that shows different date extensions like "blah.log.20181102", "blah.log.20181103"



    My dilemma is how to remove files with different dates at the end. I was want to get the number of files deleted so I am using the measure-object but it keeps giving me a count of "0" even after deletion. Below is my script:



    $limit = (Get-Date).AddDays(-30)
    $path = 'C:Usersuyr2f3dDownloadsHello1'
    $path = 'C:Usersuyr2f3dDownloadsHello2'
    $path = 'C:Usersuyr2f3dDownloadsHello3'

    Get-Childitem -Path @(
    'C:Usersuyr2f3dDownloadsHello1*'
    'C:Usersuyr2f3dDownloadsHello2*'
    'C:Usersuyr2f3dDownloadsHello3*'
    ) -Include '*.tmp', '*.log' -Force -Recurse |
    Where-Object -not $_.PSIsContainer -and $_.LastWriteTime -lt $limit |
    Remove-Item | ForEach-Object $_

    Measure-Object
    Get-Date
    Read-Host -Prompt 'The files have been deleted successfully'


    I want a prompt showing number of files deleted and when it was deleted. That is what the read-host prompt is for. I am open to simplifying and making this query better since its also deleting files from 3 different folders.










    share|improve this question


























      0












      0








      0








      All,



      This is my script to delete log files on one of our PTV servers. the issue here is that the logs files have been named in a way that shows different date extensions like "blah.log.20181102", "blah.log.20181103"



      My dilemma is how to remove files with different dates at the end. I was want to get the number of files deleted so I am using the measure-object but it keeps giving me a count of "0" even after deletion. Below is my script:



      $limit = (Get-Date).AddDays(-30)
      $path = 'C:Usersuyr2f3dDownloadsHello1'
      $path = 'C:Usersuyr2f3dDownloadsHello2'
      $path = 'C:Usersuyr2f3dDownloadsHello3'

      Get-Childitem -Path @(
      'C:Usersuyr2f3dDownloadsHello1*'
      'C:Usersuyr2f3dDownloadsHello2*'
      'C:Usersuyr2f3dDownloadsHello3*'
      ) -Include '*.tmp', '*.log' -Force -Recurse |
      Where-Object -not $_.PSIsContainer -and $_.LastWriteTime -lt $limit |
      Remove-Item | ForEach-Object $_

      Measure-Object
      Get-Date
      Read-Host -Prompt 'The files have been deleted successfully'


      I want a prompt showing number of files deleted and when it was deleted. That is what the read-host prompt is for. I am open to simplifying and making this query better since its also deleting files from 3 different folders.










      share|improve this question
















      All,



      This is my script to delete log files on one of our PTV servers. the issue here is that the logs files have been named in a way that shows different date extensions like "blah.log.20181102", "blah.log.20181103"



      My dilemma is how to remove files with different dates at the end. I was want to get the number of files deleted so I am using the measure-object but it keeps giving me a count of "0" even after deletion. Below is my script:



      $limit = (Get-Date).AddDays(-30)
      $path = 'C:Usersuyr2f3dDownloadsHello1'
      $path = 'C:Usersuyr2f3dDownloadsHello2'
      $path = 'C:Usersuyr2f3dDownloadsHello3'

      Get-Childitem -Path @(
      'C:Usersuyr2f3dDownloadsHello1*'
      'C:Usersuyr2f3dDownloadsHello2*'
      'C:Usersuyr2f3dDownloadsHello3*'
      ) -Include '*.tmp', '*.log' -Force -Recurse |
      Where-Object -not $_.PSIsContainer -and $_.LastWriteTime -lt $limit |
      Remove-Item | ForEach-Object $_

      Measure-Object
      Get-Date
      Read-Host -Prompt 'The files have been deleted successfully'


      I want a prompt showing number of files deleted and when it was deleted. That is what the read-host prompt is for. I am open to simplifying and making this query better since its also deleting files from 3 different folders.







      powershell scripting






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 22:12









      TheIncorrigible1

      9,83731434




      9,83731434










      asked Nov 14 '18 at 21:24









      walter dokuwalter doku

      1




      1






















          1 Answer
          1






          active

          oldest

          votes


















          0














          i don't know what you mean by "when it was deleted" ... it was deleted when the script was run. [grin]



          this will accept a list of files, parse the extension for the date, compare the age in days to the cutoff age, and then delete or not as per the test. it then shows the total count and the deleted count.



          # save the old InfoPref
          $OldInfoPref = $InformationPreference
          # enable Info output
          $InformationPreference = 'Continue'

          # fake reading in a list of files
          # in real life, use Get-ChildItem
          $FileList = @(
          [System.IO.FileInfo]'One.log.20180101'
          [System.IO.FileInfo]'Two.log.20180202'
          [System.IO.FileInfo]'Three.log.20180303'
          [System.IO.FileInfo]'Four.log.20180404'
          [System.IO.FileInfo]'Five.log.20180505'
          [System.IO.FileInfo]'Ten.log.20181010'
          [System.IO.FileInfo]'Nine.log.20180909'
          [System.IO.FileInfo]'Eight.log.20180808'
          [System.IO.FileInfo]'Eleven.log.20181111'
          )

          $MaxDaysOld = 30
          $Today = (Get-Date).Date

          $DeletedFileList = foreach ($FL_Item in $FileList)

          $DaysOld = ($Today - [datetime]::ParseExact($FL_Item.Extension.Trim('.'), 'yyyyMMdd', $Null)).Days
          if ($DaysOld -gt $MaxDaysOld)

          Write-Information ('The file named 0 is too old [1 days].' -f $FL_Item.Name, $DaysOld)
          Write-Information ' ----- The file would be deleted -----.'
          Write-Information ''

          # put your delete command on the next line


          # send the deleted file name to the deleted file collection
          $FL_Item

          else

          Write-Information ('The file named 0 is only 1 days old.' -f $FL_Item.Name, $DaysOld)
          Write-Information ' The file would NOT be deleted.'
          Write-Information ''



          '=' * 50
          'Total file count = 0' -f $FileList.Count
          'Deleted file count = 0' -f $DeletedFileList.Count

          # restore the old InfoPref
          $InformationPreference = $OldInfoPref


          output ...



          The file named One.log.20180101 is too old [317 days].
          ----- The file would be deleted -----.

          The file named Two.log.20180202 is too old [285 days].
          ----- The file would be deleted -----.

          The file named Three.log.20180303 is too old [256 days].
          ----- The file would be deleted -----.

          The file named Four.log.20180404 is too old [224 days].
          ----- The file would be deleted -----.

          The file named Five.log.20180505 is too old [193 days].
          ----- The file would be deleted -----.

          The file named Ten.log.20181010 is too old [35 days].
          ----- The file would be deleted -----.

          The file named Nine.log.20180909 is too old [66 days].
          ----- The file would be deleted -----.

          The file named Eight.log.20180808 is too old [98 days].
          ----- The file would be deleted -----.

          The file named Eleven.log.20181111 is only 3 days old.
          The file would NOT be deleted.

          ==================================================
          Total file count = 9
          Deleted file count = 8





          share|improve this answer






















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308947%2fpowershell-deletion-script-for-files-with-recurring-date-extension-like-2018110%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            i don't know what you mean by "when it was deleted" ... it was deleted when the script was run. [grin]



            this will accept a list of files, parse the extension for the date, compare the age in days to the cutoff age, and then delete or not as per the test. it then shows the total count and the deleted count.



            # save the old InfoPref
            $OldInfoPref = $InformationPreference
            # enable Info output
            $InformationPreference = 'Continue'

            # fake reading in a list of files
            # in real life, use Get-ChildItem
            $FileList = @(
            [System.IO.FileInfo]'One.log.20180101'
            [System.IO.FileInfo]'Two.log.20180202'
            [System.IO.FileInfo]'Three.log.20180303'
            [System.IO.FileInfo]'Four.log.20180404'
            [System.IO.FileInfo]'Five.log.20180505'
            [System.IO.FileInfo]'Ten.log.20181010'
            [System.IO.FileInfo]'Nine.log.20180909'
            [System.IO.FileInfo]'Eight.log.20180808'
            [System.IO.FileInfo]'Eleven.log.20181111'
            )

            $MaxDaysOld = 30
            $Today = (Get-Date).Date

            $DeletedFileList = foreach ($FL_Item in $FileList)

            $DaysOld = ($Today - [datetime]::ParseExact($FL_Item.Extension.Trim('.'), 'yyyyMMdd', $Null)).Days
            if ($DaysOld -gt $MaxDaysOld)

            Write-Information ('The file named 0 is too old [1 days].' -f $FL_Item.Name, $DaysOld)
            Write-Information ' ----- The file would be deleted -----.'
            Write-Information ''

            # put your delete command on the next line


            # send the deleted file name to the deleted file collection
            $FL_Item

            else

            Write-Information ('The file named 0 is only 1 days old.' -f $FL_Item.Name, $DaysOld)
            Write-Information ' The file would NOT be deleted.'
            Write-Information ''



            '=' * 50
            'Total file count = 0' -f $FileList.Count
            'Deleted file count = 0' -f $DeletedFileList.Count

            # restore the old InfoPref
            $InformationPreference = $OldInfoPref


            output ...



            The file named One.log.20180101 is too old [317 days].
            ----- The file would be deleted -----.

            The file named Two.log.20180202 is too old [285 days].
            ----- The file would be deleted -----.

            The file named Three.log.20180303 is too old [256 days].
            ----- The file would be deleted -----.

            The file named Four.log.20180404 is too old [224 days].
            ----- The file would be deleted -----.

            The file named Five.log.20180505 is too old [193 days].
            ----- The file would be deleted -----.

            The file named Ten.log.20181010 is too old [35 days].
            ----- The file would be deleted -----.

            The file named Nine.log.20180909 is too old [66 days].
            ----- The file would be deleted -----.

            The file named Eight.log.20180808 is too old [98 days].
            ----- The file would be deleted -----.

            The file named Eleven.log.20181111 is only 3 days old.
            The file would NOT be deleted.

            ==================================================
            Total file count = 9
            Deleted file count = 8





            share|improve this answer



























              0














              i don't know what you mean by "when it was deleted" ... it was deleted when the script was run. [grin]



              this will accept a list of files, parse the extension for the date, compare the age in days to the cutoff age, and then delete or not as per the test. it then shows the total count and the deleted count.



              # save the old InfoPref
              $OldInfoPref = $InformationPreference
              # enable Info output
              $InformationPreference = 'Continue'

              # fake reading in a list of files
              # in real life, use Get-ChildItem
              $FileList = @(
              [System.IO.FileInfo]'One.log.20180101'
              [System.IO.FileInfo]'Two.log.20180202'
              [System.IO.FileInfo]'Three.log.20180303'
              [System.IO.FileInfo]'Four.log.20180404'
              [System.IO.FileInfo]'Five.log.20180505'
              [System.IO.FileInfo]'Ten.log.20181010'
              [System.IO.FileInfo]'Nine.log.20180909'
              [System.IO.FileInfo]'Eight.log.20180808'
              [System.IO.FileInfo]'Eleven.log.20181111'
              )

              $MaxDaysOld = 30
              $Today = (Get-Date).Date

              $DeletedFileList = foreach ($FL_Item in $FileList)

              $DaysOld = ($Today - [datetime]::ParseExact($FL_Item.Extension.Trim('.'), 'yyyyMMdd', $Null)).Days
              if ($DaysOld -gt $MaxDaysOld)

              Write-Information ('The file named 0 is too old [1 days].' -f $FL_Item.Name, $DaysOld)
              Write-Information ' ----- The file would be deleted -----.'
              Write-Information ''

              # put your delete command on the next line


              # send the deleted file name to the deleted file collection
              $FL_Item

              else

              Write-Information ('The file named 0 is only 1 days old.' -f $FL_Item.Name, $DaysOld)
              Write-Information ' The file would NOT be deleted.'
              Write-Information ''



              '=' * 50
              'Total file count = 0' -f $FileList.Count
              'Deleted file count = 0' -f $DeletedFileList.Count

              # restore the old InfoPref
              $InformationPreference = $OldInfoPref


              output ...



              The file named One.log.20180101 is too old [317 days].
              ----- The file would be deleted -----.

              The file named Two.log.20180202 is too old [285 days].
              ----- The file would be deleted -----.

              The file named Three.log.20180303 is too old [256 days].
              ----- The file would be deleted -----.

              The file named Four.log.20180404 is too old [224 days].
              ----- The file would be deleted -----.

              The file named Five.log.20180505 is too old [193 days].
              ----- The file would be deleted -----.

              The file named Ten.log.20181010 is too old [35 days].
              ----- The file would be deleted -----.

              The file named Nine.log.20180909 is too old [66 days].
              ----- The file would be deleted -----.

              The file named Eight.log.20180808 is too old [98 days].
              ----- The file would be deleted -----.

              The file named Eleven.log.20181111 is only 3 days old.
              The file would NOT be deleted.

              ==================================================
              Total file count = 9
              Deleted file count = 8





              share|improve this answer

























                0












                0








                0







                i don't know what you mean by "when it was deleted" ... it was deleted when the script was run. [grin]



                this will accept a list of files, parse the extension for the date, compare the age in days to the cutoff age, and then delete or not as per the test. it then shows the total count and the deleted count.



                # save the old InfoPref
                $OldInfoPref = $InformationPreference
                # enable Info output
                $InformationPreference = 'Continue'

                # fake reading in a list of files
                # in real life, use Get-ChildItem
                $FileList = @(
                [System.IO.FileInfo]'One.log.20180101'
                [System.IO.FileInfo]'Two.log.20180202'
                [System.IO.FileInfo]'Three.log.20180303'
                [System.IO.FileInfo]'Four.log.20180404'
                [System.IO.FileInfo]'Five.log.20180505'
                [System.IO.FileInfo]'Ten.log.20181010'
                [System.IO.FileInfo]'Nine.log.20180909'
                [System.IO.FileInfo]'Eight.log.20180808'
                [System.IO.FileInfo]'Eleven.log.20181111'
                )

                $MaxDaysOld = 30
                $Today = (Get-Date).Date

                $DeletedFileList = foreach ($FL_Item in $FileList)

                $DaysOld = ($Today - [datetime]::ParseExact($FL_Item.Extension.Trim('.'), 'yyyyMMdd', $Null)).Days
                if ($DaysOld -gt $MaxDaysOld)

                Write-Information ('The file named 0 is too old [1 days].' -f $FL_Item.Name, $DaysOld)
                Write-Information ' ----- The file would be deleted -----.'
                Write-Information ''

                # put your delete command on the next line


                # send the deleted file name to the deleted file collection
                $FL_Item

                else

                Write-Information ('The file named 0 is only 1 days old.' -f $FL_Item.Name, $DaysOld)
                Write-Information ' The file would NOT be deleted.'
                Write-Information ''



                '=' * 50
                'Total file count = 0' -f $FileList.Count
                'Deleted file count = 0' -f $DeletedFileList.Count

                # restore the old InfoPref
                $InformationPreference = $OldInfoPref


                output ...



                The file named One.log.20180101 is too old [317 days].
                ----- The file would be deleted -----.

                The file named Two.log.20180202 is too old [285 days].
                ----- The file would be deleted -----.

                The file named Three.log.20180303 is too old [256 days].
                ----- The file would be deleted -----.

                The file named Four.log.20180404 is too old [224 days].
                ----- The file would be deleted -----.

                The file named Five.log.20180505 is too old [193 days].
                ----- The file would be deleted -----.

                The file named Ten.log.20181010 is too old [35 days].
                ----- The file would be deleted -----.

                The file named Nine.log.20180909 is too old [66 days].
                ----- The file would be deleted -----.

                The file named Eight.log.20180808 is too old [98 days].
                ----- The file would be deleted -----.

                The file named Eleven.log.20181111 is only 3 days old.
                The file would NOT be deleted.

                ==================================================
                Total file count = 9
                Deleted file count = 8





                share|improve this answer













                i don't know what you mean by "when it was deleted" ... it was deleted when the script was run. [grin]



                this will accept a list of files, parse the extension for the date, compare the age in days to the cutoff age, and then delete or not as per the test. it then shows the total count and the deleted count.



                # save the old InfoPref
                $OldInfoPref = $InformationPreference
                # enable Info output
                $InformationPreference = 'Continue'

                # fake reading in a list of files
                # in real life, use Get-ChildItem
                $FileList = @(
                [System.IO.FileInfo]'One.log.20180101'
                [System.IO.FileInfo]'Two.log.20180202'
                [System.IO.FileInfo]'Three.log.20180303'
                [System.IO.FileInfo]'Four.log.20180404'
                [System.IO.FileInfo]'Five.log.20180505'
                [System.IO.FileInfo]'Ten.log.20181010'
                [System.IO.FileInfo]'Nine.log.20180909'
                [System.IO.FileInfo]'Eight.log.20180808'
                [System.IO.FileInfo]'Eleven.log.20181111'
                )

                $MaxDaysOld = 30
                $Today = (Get-Date).Date

                $DeletedFileList = foreach ($FL_Item in $FileList)

                $DaysOld = ($Today - [datetime]::ParseExact($FL_Item.Extension.Trim('.'), 'yyyyMMdd', $Null)).Days
                if ($DaysOld -gt $MaxDaysOld)

                Write-Information ('The file named 0 is too old [1 days].' -f $FL_Item.Name, $DaysOld)
                Write-Information ' ----- The file would be deleted -----.'
                Write-Information ''

                # put your delete command on the next line


                # send the deleted file name to the deleted file collection
                $FL_Item

                else

                Write-Information ('The file named 0 is only 1 days old.' -f $FL_Item.Name, $DaysOld)
                Write-Information ' The file would NOT be deleted.'
                Write-Information ''



                '=' * 50
                'Total file count = 0' -f $FileList.Count
                'Deleted file count = 0' -f $DeletedFileList.Count

                # restore the old InfoPref
                $InformationPreference = $OldInfoPref


                output ...



                The file named One.log.20180101 is too old [317 days].
                ----- The file would be deleted -----.

                The file named Two.log.20180202 is too old [285 days].
                ----- The file would be deleted -----.

                The file named Three.log.20180303 is too old [256 days].
                ----- The file would be deleted -----.

                The file named Four.log.20180404 is too old [224 days].
                ----- The file would be deleted -----.

                The file named Five.log.20180505 is too old [193 days].
                ----- The file would be deleted -----.

                The file named Ten.log.20181010 is too old [35 days].
                ----- The file would be deleted -----.

                The file named Nine.log.20180909 is too old [66 days].
                ----- The file would be deleted -----.

                The file named Eight.log.20180808 is too old [98 days].
                ----- The file would be deleted -----.

                The file named Eleven.log.20181111 is only 3 days old.
                The file would NOT be deleted.

                ==================================================
                Total file count = 9
                Deleted file count = 8






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 14 '18 at 23:45









                Lee_DaileyLee_Dailey

                2,194189




                2,194189





























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308947%2fpowershell-deletion-script-for-files-with-recurring-date-extension-like-2018110%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    How to read a connectionString WITH PROVIDER in .NET Core?

                    In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

                    Museum of Modern and Contemporary Art of Trento and Rovereto