Script to delete all files older




















Sign in to vote. Hi, I am a complete Powershell novice, so any hope is greatly appreciated! I also tried pointing it at a test folder containing only three old files, no change. Any advice on what I'm doing wrong? Thursday, July 31, PM. Hi AnnWY, Thanks for the advice, and for confirming the script works for you :.

Edit: Success!! Eh, probably user error : Thank-you for you help! Friday, August 1, PM. Hi Dixie, I have created a no hidden test folder, and run the script above without issue, I removed "-Hidden" paramter in the cmdlet "Get-Childitem", and the script can delete the files older than 30 days successfully.

Turn on suggestions. Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type. Showing results for. Show only Search instead for. Did you mean:. Sign In. Anthony Bartolo. Published Mar 26 AM Lets begin. LastWriteTime -lt Get-Date. Tags: clean. Version history. Last update:. Updated by:. This particular code also preserves exe and dll files, and cleans up subfolders as well. How about this modification on 7daysclean. The condition above contributed by J.

And Month GEQ 2 is also wrong because adding seconds for one more day must be done in a leap year only for the months March to December, but not for February.

A working code to take leap day into account - in current year only - in batch file 7daysclean. Might I add a humble contribution to this already valuable thread. I don't know if it's a bug, but the CmdExec within the step only recognizes the first line of code:.

Gosh, a lot of answers already. In this example it works by picking all files. If you have the XP resource kit, you can use robocopy to move all the old directories into a single directory, then use rmdir to delete just that one:. I think e. James's answer is good since it works with unmodified versions of Windows as early as Windows SP4 and possibly earlier , but it required writing to an external file.

Here is a modified version that does not create an external text file while maintaining the compatibility:. To be true to the original question, here it is in a script that does ALL the math for you if you call it with the number of days as the parameter:.

NOTE: The code above takes into account leap years, as well as the exact number of days in each month. NOTE: The math only goes one way; it cannot correctly get future dates from negative input it will try, but will likely go past the last day of the month. Originally suggested my Iman. It cleans anything older than 15 days out of my 'Temp' folder and 30 days for anything in my AutoCAD backup folder. I use variables because the line can get quite long and I can reuse them for other locations.

You just need to find the dos path to your recycle bin associated with your login. This is on a work computer for me and it works. You might be able to pull this off. You can take a look at this question , for a simpler example. The complexity comes, when you start comparing the dates. It may be easy to tell if the date is greater or not, but there are many situations to consider if you need to actually get the difference between two dates. Expanding on aku's answer, I see a lot of people asking about UNC paths.

Simply mapping the unc path to a drive letter will make forfiles happy. Mapping and unmapping of drives can be done programmatically in a batch file, for example. This will delete all files with a. If you want to make sure Z: isn't mapped to anything else before using it you could do something simple as.

This one did it for me. It works with a date and you can substract the wanted amount in years to go back in time:. The script will allow you to use measurements like days, minutes ,seconds or hours. To choose weather to filter the files by time of creation, access or modification To list files before or after a certain date or between two dates To choose if to show files or dirs or both To be recursive or not.

Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Batch file to delete files older than N days Ask Question. Asked 13 years, 4 months ago. Active 10 months ago. Viewed 1. Stephan Kibbee Kibbee Jeff Atwood answered this on Serverfault which I think should be documented here. A new method based in a.

EXE commands only has been posted here: stackoverflow. It even allows for a more sophisticated deletion scheme: in addition to keeping files of the last 7 days, it e. Jan-Philip Gehrcke. Add a comment. Active Oldest Votes. Later versions of Windows and Windows Server have it installed by default. For Windows 7 and newer including Windows 10 : The syntax has changed a little. FZs Russell: PATH is the full path, including name. FILE is only the name, so if you're dealing with subfolders, it won't work.

This answer works when "older than 7 days" is defined as "modified more than 7 days ago" rather than "created more than 7 days ago".

How can the latter be achieved? Show 22 more comments. David Tonhofer



0コメント

  • 1000 / 1000