Delete files older than x days in batch file
Linux PinePhone Pro. Google Green Messages. Use Your iPhone as a Webcam. Hide Private Photos on iPhone. All Microsoft's PowerToys for Windows. Take Screenshot by Tapping Back of iPhone. Windows 11 Default Browser. Browse All Windows Articles.
Windows 10 Annual Updates. OneDrive Windows 7 and 8. Copy and Paste Between Android and Windows. Protect Windows 10 From Internet Explorer. Mozilla Fights Double Standard. Connect to a Hidden Wi-Fi Network. Change the Size of the Touch Keyboard.
Reader Favorites Take Screenshot on Windows. Mount an ISO image in Windows. Boot Into Safe Mode. Where to Download Windows Legally. Find Your Lost Product Keys. 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 :.
February : is hard-coded to 28 days. Bissextile years is a hell to add, really. It would need to be adapted for other settings or a run-time evaluation read sShortTime, user-bound configuration, configure proper field order in a filter and use the filter to extract the correct data from the argument.
Did I mention I hate this editor's auto-formating? So all the complicated batches could be in the trash bin. Also forfiles don't support UNC paths, so make a network connection to a specific drive. Have a look at my answer to a similar question :. This deletes files older than a given date. I'm sure it can be modified to go back seven days from the current date.
I recommend using his version instead. All this regarding forfiles. But if you are using Windows server forfiles. That is why I should modify command. But command line interpreter cmd. Lots of good working solutions using additional console applications or scripts have been posted already here, on other pages of Stack Overflow and on other websites. Very common is UTC. But any later day could be also used depending on the date range required to support for a specific task. Jay posted 7daysclean.
But it does not take leap years correct into account. This very fast table method can be used also in batch code using FOR command. Note: Leap seconds are not taken into account as the Windows file systems also do not support leap seconds. Calculating the seconds for year to with epoch beginning is only possible with using an unsigned bit variable, i.
But cmd. The format of date and time strings depends on Windows region and language settings. Now the last 10 characters from date string are passed to function GetSeconds and therefore it does not matter if date string of environment variable DATE is with or without weekday as long as day and month are always with 2 digits in expected order, i.
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.
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. View all posts by Sergey Tkachenko. The solution using forfiles. Thus it is not really suitable for use in task scheduler. But it is not able to delete the file of subfolders. Can you please give command to delete the files for subfolders as well. Your email address will not be published. Skip to content Advertisement. Contents hide. Support us Winaero greatly relies on your support. Excellent, Very nice work, Great help.
Can you also share the command to do the same list or delete folders older then X days. Leave a Reply Cancel reply Your email address will not be published. We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. Read More Do not sell my personal information. Close Privacy Overview This website uses cookies to improve your experience while you navigate through the website.
0コメント