Batch file for deleting old files


















For example, we will tell the batch file to delete. Create a text file and copy the below command line :. As a result, we will tell the cmd remove all files in folder that are located in the Test folder.

Also, you can use Task Scheduler to create scheduled task. Batch to delete file. Batch to delete all files. Prev Article. Similar things can be done in BASH in just a couple lines of code. It seems that something at least remotely easy could be done for batch files in Windows. I'm looking for a solution that works in a standard Windows command prompt, without any extra utilities.

Please no PowerShell or Cygwin either. See forfiles documentation for more details. Run the following commands :. 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. 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. Is there an equivalent of 'which' on the Windows command line?

Ignoring directories in Git repositories on Windows. Filename too long in Git for Windows. How do I get the application exit code from a Windows command line? How to upgrade Git on Windows to the latest version.



0コメント

  • 1000 / 1000