none
Script to delete all files only (not folders) older than 30 days exept some extensions like PDF and BAT files RRS feed

  • Question

  • Hi,

    My below script is to delete all files only (not folders) older than 30 days except some extensions like PDF and BAT files.

    It works but it deletes all bat and PDF recursively.wich mean it doesn't delete pdf and bat in the root folder where I put script but it deletes them inside other subdirectories.please how to resolve this issue?

    @echo off
    
    setlocal ENABLEDELAYEDEXPANSION
    
    set today=!date:/=-!
    set now=!time::=-!
    
    forfiles /S /C "cmd /C (if not @ext==\"pdf\" if not @ext==\"bat\" del /S /Q @path)" /D -30 > C:\Logs\log-!today!_!now!.txt 
    Thanks in advance

    Friday, May 12, 2017 10:03 AM

Answers

  • I've tried this code and it works now

    Get-ChildItem -path C:\wsusoffline -exclude *.pdf,*.bat ,*.ps1 -recurse -file -erroraction silentlycontinue | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-0)} | remove-item -force

    how can I add log file with date/time of deletion?

    Are those files recoverable? I mean if this is same as SDelete?

    I want to make sure files are not recoverable

    Thanks


    • Edited by BTSINTIC Friday, May 12, 2017 1:31 PM
    • Marked as answer by BTSINTIC Friday, May 12, 2017 8:52 PM
    Friday, May 12, 2017 12:19 PM
  • No.  "sdelete" erases the contents.  Delete a file does not.

    To learn how to use PowerShell I recommend starting with one of the many good books available.

    To erase a file you will have to use sdelete instead of Remove-Item

    You asked how to delete a file.  The question was answered.  If you have new questions please start a new topic.


    \_(ツ)_/

    • Marked as answer by BTSINTIC Friday, May 12, 2017 8:52 PM
    Friday, May 12, 2017 3:45 PM

All replies

  • Get-ChildItem . -include *.pdf,*.bat -Recurse | Remove-Item -WhatIf

    \_(ツ)_/

    Friday, May 12, 2017 10:15 AM
  • You can also just do this:

    Remove-Item $pwd\* -include *.pdf,*.bat -Recurse -WhatIf


    \_(ツ)_/

    Friday, May 12, 2017 10:17 AM
  • Hi

    Thanks for your reply,

    Please Where to add exactly this line in my batch (script.bat) ?

    Thanks again

    Friday, May 12, 2017 10:21 AM
  • Don't use batch as it is obsolete.  This is a PowerShell script and you will find PowerShell is much easier to use.


    \_(ツ)_/

    Friday, May 12, 2017 10:25 AM
  • OK thanks for the help

    however, my requirement is:

    To delete all files (not folders) older than 30 days EXCEPT for PDF and BAT files which should not be deleted from the root folder and its subdirectories

    root folder: C\training

    Subfolders: C\training\accounting 

                     C\training\IT

                     C\training\Sales

    ...............................

    how can I achieve the goal with Powershell, please?

    Thanks again!

    Friday, May 12, 2017 10:31 AM
  • Get-ChildItem . -Exclude *.pdf,*.bat -Recurse  -file| Remove-Item -WhatIf

    \_(ツ)_/

    Friday, May 12, 2017 10:42 AM
  • date is not specified in your script , how to specify older than 30 days?

    thanks

    Friday, May 12, 2017 10:44 AM
  • Get-ChildItem . -Exclude *.pdf,*.bat -Recurse  -file| 
         Where{$_.LastWrtieTime -lt [datatime]::Today.AddDays(-30)}|
         Remove-Item -WhatIf
    


    \_(ツ)_/

    Friday, May 12, 2017 10:51 AM
  • Here if the error when I run the script

    PS C:\training> .\Script.PS1
    Get-ChildItem : A parameter cannot be found that matches parameter name 'file'.
    At C:\training\Script.PS1:1 char:53
    + Get-ChildItem . -Exclude *.pdf,*.bat -Recurse  -file <<<< |
        + CategoryInfo          : InvalidArgument: (:) [Get-ChildItem], ParameterB
       indingException
        + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Comm
       ands.GetChildItemCommand



    • Edited by BTSINTIC Friday, May 12, 2017 11:02 AM
    Friday, May 12, 2017 11:00 AM
  • You should upgrade PS t a supported version.


    \_(ツ)_/

    Friday, May 12, 2017 11:44 AM
  • Thanks, yes After upgrade  to PowerShell 3, the script runs

    here is the output, it doesn't delete any files

    PS C:\training> C:\training\S5.PS1
    What if: Performing operation "Remove file" on Target "C:\training\training1091\Migration next.xlsx"
    .
    What if: Performing operation "Remove file" on Target "C:\training\training1091\New Text Document.tx
    t".
    What if: Performing operation "Remove file" on Target "C:\training\Migration next.xlsx".
    What if: Performing operation "Remove file" on Target "C:\training\New Text Document.txt".
    
    PS C:\training> 



    • Edited by BTSINTIC Friday, May 12, 2017 11:58 AM
    Friday, May 12, 2017 11:58 AM
  • I've tried this code and it works now

    Get-ChildItem -path C:\wsusoffline -exclude *.pdf,*.bat ,*.ps1 -recurse -file -erroraction silentlycontinue | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-0)} | remove-item -force

    how can I add log file with date/time of deletion?

    Are those files recoverable? I mean if this is same as SDelete?

    I want to make sure files are not recoverable

    Thanks


    • Edited by BTSINTIC Friday, May 12, 2017 1:31 PM
    • Marked as answer by BTSINTIC Friday, May 12, 2017 8:52 PM
    Friday, May 12, 2017 12:19 PM
  • No.  "sdelete" erases the contents.  Delete a file does not.

    To learn how to use PowerShell I recommend starting with one of the many good books available.

    To erase a file you will have to use sdelete instead of Remove-Item

    You asked how to delete a file.  The question was answered.  If you have new questions please start a new topic.


    \_(ツ)_/

    • Marked as answer by BTSINTIC Friday, May 12, 2017 8:52 PM
    Friday, May 12, 2017 3:45 PM