For optimal performance it would be best to remove all comments, i. And the arrays can be made also smaller, i. For even more information about date and time formats and file time comparisons on Windows see my answer on Find out if file is older than 4 hours in batch file with lots of additional information about file times.
IMO, JavaScript is gradually becoming a universal scripting standard: it is probably available in more products than any other scripting language in Windows, it is available using the Windows Scripting Host.
I have to clean out old files in lots of folders, so here is a JavaScript function to do that:. For each folder to clear, just add another call to the clearFolder function. 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.
Let's review them. This command selects a file or a set of files and executes a command on that file. For example, means over a year ago, means a month ago. Command strings should be wrapped in double quotes.
The following variables can be used in the command string: file - returns the name of the file. Substitute the folder path and the amount of days with desired values and you are done. For example, to remove the files older than a month from the Downloads folder, use the following command:.
This trick works in all modern versions of Windows including Windows 7, Windows 8, Windows 8. If the Get-ChildItem cmdlet finds any files that are older than a month, then the Remove-Item cmdlet will be called for each file to remove it. Winaero greatly relies on your support. You can help the site keep bringing you interesting and useful content and software by using these options:. If you like this article, please share it using the buttons below. It won't take a lot from you, but it will help us grow.
Thanks for your support! Sergey Tkachenko is a software developer from Russia who started Winaero back in On this blog, Sergey is writing about everything connected to Microsoft, Windows and popular software. Follow him on Telegram , Twitter , and YouTube. Blorgbeard Blorgbeard I used this command, and the folders have been deleted.
Add a comment. Ross Johnson Ross Johnson 1 1 silver badge 2 2 bronze badges. Zsolt Hidasi Zsolt Hidasi 1 1 silver badge 2 2 bronze badges. The Overflow Blog. Podcast Making Agile work for data science. Stack Gives Back Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually.
HOW TO? 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.
Ok was bored a bit and came up with this, which contains my version of a poor man's Linux epoch replacement limited for daily usage no time retention : 7daysclean. Change : if! I hope this helps. Top 3 video Explaining windows - Batch file to delete files older than N days.
0コメント