Batch file to remove old files


















In fact, we will delete files on specific folders. 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. United States English. Ask a question. Quick access. Search related threads. Remove From My Forums. Answered by:. Archived Forums. The Official Scripting Guys Forum! If your answer is "yes," you know what to do. Dive in and help somebody! If your answer is "no," welcome to our fun little world! We'd recommend that you first head over to the Script Center, get your feet wet, and then come back to either ask or answer questions.

This example uses 30 days as the required age but you can adjust this change both numbers in bold. Gonna need a bit more info, Marc. Give a read through Asking Better Questions and post back with some additional information. I've seen several PowerShell scripts in the community that do exactly this. Sureline Communications is an IT service provider. I had a script a few months back to look for a specific folder on a server and keep its contents under a specified size by flushing files older than 3 days, I'll see if I can find it for you.

I am trying to delete db backups in a folder on windows server with. I need to keep last 30 days backup in that folder. I need a batch file for it. But, somehow I am not getting the required results. Really, Marc, consider working with PowerShell for this. It supports some very cool date math. Here's a quick script that should do the trick for you.

Robocopy has options for deleting files by age. Being a common Windows program, it is an option. 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 Iman Iman 6 6 silver badges 2 2 bronze badges. For the most part, this is a fairly impractical answer.

If I have a directory with lots of data in it, there is will not work well. I'd go with one of the answer that does it "in place" — adamb0mb. As robocopy is really robust, it will in fact work for any directory size, obscure file names, basically arbitrary path lengths and include directories if you need it to - something which surely cannot be said about many other Windows utilities. Plus, robocopy does support UNC paths - which forfiles from the accepted answer won't do. My thoughts were more along the lines: "My files are already where I want them.

I don't want to have to move them. Don't overload Robocopy to do it" — adamb0mb.



0コメント

  • 1000 / 1000