Delete files older than x-days - Cleanup Script

Script to delete files older than x-days. The script is built to be used as a scheduled task, it automatically generates a log file based on the copy location and the current date/time. There are various levels of logging available.

deleteold.ps1
 
 
 
 
 
4.6 Star
(136)
106,811 times
Add to favorites
Storage
4/12/2016
E-mail Twitter del.icio.us Digg Facebook
Sign in to ask a question


  • Script not working as expected
    1 Posts | Last post October 23, 2019
    • Hi, I am trying to delete older files in two folders, so I am running the following:
      
      DeleteOld.ps1 -FolderPath H:\myScripts -FileAge 30 -IncludePath "H:\myScripts\VBScript","H:\myScripts\CMD" -LogFile H:\ -AutoLog
      
      And it doesn't delete anything.
      Am I doing it wrong?
      
      Thanks,
  • KeepFile option being ignored
    2 Posts | Last post September 27, 2019
    • hi, i was using successully this option on a windows 2008 server. On Windows 2016 it appears bo be ignored. This is the output of the command wxecuted in list only. The KeepFile option is "read", but ignored.
      
      The script finds 8 files. I want 12 to be kept, but it will delete 4. Why?
      Please help me...
      
      Started  :   9/27/2019, 10:53:28 AM
       Folder   :   W:\test\PR
       Switches :
                    -FolderPath
                            W:\test\PR
                    -FileAge 3
                    -LogFile
                            C:\logs\deleteold-pr2-log.txt
                    -ExcludePath
                            W:\test\PR\nodelete
                    -KeepFile 12
                    -ListOnly
                    -VerboseLog
                    -AppendLog
      
            *** Running in Listonly mode, no files will be modified ***
      
      -----------------------------------------------------------------------------
      
       Retrieving list of files and folders from: W:\SAP\PR2
      
       Files        : 8
       Folders      : 1
       Old files    : 4
      
       Listing files...
       Finished listing files
      
      
      -----------------------------------------------------------------------------
      
       Files               : 4
       Filesize(MB)        : 1102528
       Files Failed        : 0
       Failedfile Size(MB) : 0
       Folders             : 0
       Folders Failed      : 0
      
       Finished Time       : 9/27/2019, 10:53:28 AM
       Total Time          : 00:00:00
    • P.S.: i am using Version: 2.0.5
  • How to submit a proposed update?
    1 Posts | Last post September 12, 2019
    • I wanted to include a friendly name in the From email field but found it would not except the format "friendlyname <email@abc.com>" because the "<" was reserved.  I added a parameter -EmailFromFriendly.
      
      I wanted the dynamic subject that gaves timestamps and folder but I wanted to customize it.  Providing a full subject with the -EmailSubject parameter does not allow access to the timestamps or folder.  I created a new -EmailSubjectParameter.
      
      I customized the code to use the parameters and added comments and an example.
      
      Can I submit my revision for your review?
      
      Thanks for the great tool!
      
      Cheers
      
      ravin@hungermountain.coop
  • Can force it to keep directory if all files are older than specified FileAge
    2 Posts | Last post March 13, 2019
    • What I didn't realise is if all files in the directory are older than the FileAge specified, it also removes the directory.
      
      How do I keep the directory intact ? (currently investigating script to see if it's embedded somewhere)
    • use the -NoFolder option
  • -Keepfile only works if there are more than that amount of files in the directory
    1 Posts | Last post October 30, 2018
    • -Keepfile only works if there are more than that amount of files in the directory. Otherwise it deletes them all. ex. if you have 5 files in a directory and you're -keepfiles is set at 6, it will delete them all. 
  • if folder doesn´t excist, the script looks one level up
    1 Posts | Last post September 17, 2018
    • Hi,
      I noticed that when i run the script with one folder:
      c:\script\deleteold.ps1 -FolderPath c:\logs2 -FileAge 14 -LogFile c:\script\log2.txt
      If c:\logs2 doesn't excist the script looks in c:\ and gives me a report of files/folders in the log2.txt 
      Is there any way I can make sure that the script doesn't do anything if a folder is missing?
  • KeepFile keeps oldest files first
    1 Posts | Last post August 14, 2018
    • 
      Hi,
      
      When using the KeepFile parameter, the command is actually keeping the oldest files first, not the newest files. Is it the desired behavior? On my part the proper behavior would be to keep the newest files, not the oldest files.
      See line 1045 to 1052:
                  $FileList = @($_.Group | Sort-Object -Property CustomTime     | Select-Object -Last ($_.Count-$KeepFile))
      
      Since the list is sorted by date and the -Last switch will keep the newest from the list, the oldest will be removed from the list, thus will be kept from being removed afterwards.
      This could be resolved easily by using the -First switch instead of the -Last switch.
      
      Thanks.
  • EmailBody parameter
    1 Posts | Last post August 08, 2018
    • Greetings,
      
      How can I use this parameter to grab information from the log file created and send it as emailbody instead of attachment?
      
      Thanks you
  • Long Path Issues
    1 Posts | Last post May 02, 2018
    • What can we do to use this script with long path names? And we cannot shorten the path...
      
      FAILED ACCESS	The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters. 
      
      Thank you!
  • problem with IncludeFileExtension
    1 Posts | Last post April 17, 2018
    • Hi,
      
      first of all. GREAT SCRIPT ! Thanks !!!
      
      Everything works fine but when I add: 
      
      -IncludeFileExtension '.imported' 
      
      it does not work. No files are matched. Also '.imp*' does not work. Could it be that there is a problem with file extension longer than three chars ?
      
      Best regards
1 - 10 of 177 Items