Add a comment. Active Oldest Votes. Improve this answer. Ken White k 13 13 gold badges silver badges bronze badges. Varad Varad 1 1 silver badge 10 10 bronze badges. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Making Agile work for data science. Stack Gives Back Featured on Meta.
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. 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. Improve this question. My solution works if you either have a touch utility on your system which I do or can afford to temporarily change system date using date command which is probably not the case, but who knows Here's the idea: In the target directory, create a signal file with a unique name , and with timestamp equal to your deletion threshold time.
This seems like an incredibly risky approach compared to the other answers here. Dan If you are referring to the possibility that something will mess with the signal file, it can be guarded against by splitting 2 into two stages: 2. Otherwise, abort. Anyway, this solution is a kludge, of course. I was just thinking if you accidentally ordered the files incorrectly you might delete thousands of files before reaching the signal file that you didn't want to delete. I don't like the idea of having a signal file vs.
I also don't quite understand how your approach deals with subdirectories not sure if that was a requirement for OP, but it is for me. Dan IMO, ordering the files incorrectly is no greater danger than mistyping the cutoff date, with same result. Subdirectories can be handled by applying the whole procedure recursively. Though I'm not sure why I'm trying to keep this approach viable in an expanding set of requirements. Niklas J. MacDowall Niklas J. MacDowall 13 13 silver badges 27 27 bronze badges.
The website is up for sale now I've updated the URL. No way that would work in DOS. So he uses cmd, actually which is still a very sane choice even if cumbersome , since it's the only thing that works on every Windows except WSH, but that can be locked down with group policies. Vilx- Vilx- k 84 84 gold badges silver badges bronze badges. See stackoverflow. I didn't give you that downvote.
But I think it's because this is more like a comment than an answer. Derek - Oh, ok then.
0コメント