Monday 31 August 2009

Using Robocopy to delete old files from folder

There are numerous occasions where you may want to keep a folder tidy and remove older files.

We needed a solution for a shared folder that we wanted to control so that users didn't use it as a lazy dumping ground for their files.

After a bit of searching I found a novel way of using robocopy to accomplish exactly what we wanted.
mkdir c:\delete
c:\robocopy.exe c:\Source c:\Delete /e /MOVE /MINAGE:14 /LOG+:c:\robocopy.log
rmdir c:\delete /s /q

This script creates a folder called 'delete'. Then uses robocopy to move files older than 14 days from the 'source' folder to the 'delete' folder. Lastly it deletes the 'delete' folder so that only files newer than 14 days is left in the 'source' folder.

Notes:
Remember to change the location of the robocopy executable, robocopy.log, your source and delete folders.
Add '/XF [filename]' to the robocopy line in order to exclude one or more files from being deleted (if required).

Original article can be found here. (Pure genius!)

47 comments:

Anonymous said...

To erase one complex dir i use the command:
robocopy e:\empty e:\to_delete /MIR /E

e:\empty is a empty dir. its fast :-)

Anonymous said...

Is there a way i can edit this script to copy files to a new locaiton versus deleting?

' Objective: To delete old files from a given folder and all subfolders below
'
' Created by: ANT Mar 28, 2012
'
' Format: cscript deloldfiles.vbs {DriveLetter:\FolderName} {#ofDays}
' or: cscript deloldfiles.vbs {\\servername\FolderName} {#ofDays}
' Example: cscript deloldfiles.vbs c:\dba\log 3
' (deletes files older than 3 days from the \dba\log file on drive C:)
Set objArgs = WScript.Arguments
FolderName =objArgs(0)
Days=objArgs(1)

set fso = createobject("scripting.filesystemobject")
set folders = fso.getfolder(FolderName)
datetoday = now()
newdate = dateadd("d", Days*-10, datetoday)
wscript.echo "Today:" & now()
wscript.echo "Started deleting files older than :" & newdate
wscript.echo "________________________________________________"
wscript.echo ""
recurse folders
wscript.echo ""
wscript.echo "Completed deleting files older than :" & newdate
wscript.echo "________________________________________________"

sub recurse( byref folders)
set subfolders = folders.subfolders
set files = folders.files
wscript.echo ""
wscript.echo "Deleting Files under the Folder:" & folders.path
wscript.echo "__________________________________________________________________________"
for each file in files
if file.datelastmodified < newdate then
wscript.echo "Deleting " & folders.path & "\" & file.name & " last modified: " & file.datelastmodified
on error resume next
' === to test this script but not actually delete files, comment out the next line ===
file.delete
end if

next

for each folder in subfolders
recurse folder
next

set subfolders = nothing
set files = nothing

end sub

Sony said...

In order to keep the moved files and not delete them you can simply ommit the last line of the script.

mkdir c:\delete
c:\robocopy.exe c:\Source c:\Delete /e /MOVE /MINAGE:14 /LOG+:c:\robocopy.log

and not do

rmdir c:\delete /s /q

Anonymous said...

To speed things up you can use the /CREATE option which will create zero-length files.

Anonymous said...

Brilliant! That's exactly what I need, thanks. And adding the /CREATE option is a clever touch :)

Unknown said...

To delete all files older than 14 days in folder C:\Source, the command below works in Microsoft Windows Version 6.3.9600 (and other versions). Good help and examples by running FORFILES /?

FORFILES /P C:\Source /D -14 /C "cmd /c Del @file"

Anonymous said...

All,

I created below script based on this tech note. I am trying delete files which are unused more than 10 days.. But it is not working..Can you please take a look and suggest what is wrong...

mkdir c:\delete
c:\robocopy.exe c:\Source c:\Delete /e /XF/C:\Source\Project /MOVE /MAXLAD:10 /LOG+:c:\robocopy.log
rmdir c:\delete /s /q

docker said...

c:\robocopy.exe c:\Source c:\Delete /e /XF/C:\Source\Project /MOVE /MAXLAD:10 /LOG+:c:\robocopy.log

change maxlad to maxage...

Jaxon Oakley said...

I like your thoughts due to your skills.

Unknown said...

hello, your website is really good. We do appreciate your give good results toptenreviews.com

vikas khandola said...

use robocopy

Unknown said...
This comment has been removed by the author.
Unknown said...

After a bit of searching I found a novel way of using robocopy to accomplish exactly what we wanted.
mkdir c:\delete
c:\robocopy.exe c:\Source c:\Delete /e /MOVE /MINAGE:14 /LOG+:c:\robocopy.log
rmdir c:\delete /s /q

This script creates a folder called 'delete'. Then uses robocopy to move files older than 14 days from the 'source' folder to the 'delete' folder. Lastly it deletes the 'delete' folder so that only files newer than 14 days is left in the 'source' folder...but in my opinion some other tools are better then robocopy...currently i am using GS Richcopy 360 for these type of work.

Anonymous said...

Is there a way to use robocopy to move a folder that is named after the machine name and then date/time stamped to another folder? Folder currently look like this:

ABC-TEMP-001_20180409-1201

I tried %COMPUTERNAME%_%datetimestamp%, but that end ups looking for the foldername with the current date/time stamp.

Unknown said...

"""After a bit of searching I found a novel way of using robocopy to accomplish exactly what we wanted.
mkdir c:\delete
c:\robocopy.exe c:\Source c:\Delete /e /MOVE /MINAGE:14 /LOG+:c:\robocopy.log
rmdir c:\delete /s /q

This script creates a folder called 'delete'. Then uses robocopy to move files older than 14 days from the 'source' folder to the 'delete' folder. Lastly it deletes the 'delete' folder so that only files newer than 14 days is left in the 'source' folder..,however in my opinion there are other tools are which are way better then robocopy.Currently i am using GS Richcopy 360 for these type of work.

kevingeorge said...

Thanks a lot very much for the high quality and results-oriented help. I won’t think twice to endorse your blog post to anybody who wants and needs support about this area.



rpa Training in Bangalore


Maha said...
This comment has been removed by the author.
Maha said...

Lovely Article. Very Helpful story. Please keep posting.

RPA Training in Hyderabad
RPA Training in Chennai
RPA Training in Bangalore

Bin Li said...

Cool! it works like a champ!

MOUNIKA said...

I get a lot of great information from this blog. Recently I did oracle certification course at a leading academy. If anyone interested to learn best Oracle Course in Hyderabad visit Go Trainings Academy which offer SQL Training in Hyderabad. 
Best Training and Real Time Support
Oracle Bpm Training
Oracle Osb Training

LindaJasmine said...

The information given is extra-ordinary. Looking forward to read more . Thanks for sharing.
IELTS Coaching in Chennai
IELTS Training in Chennai
IELTS classes in Chennai
IELTS Training in Tambaram
Spoken English Classes in Chennai
Best Spoken English Classes in Chennai
SAS Training in Chennai
SAS Course in Chennai

anitha said...
This comment has been removed by the author.
VenuBharath2010@gmail.com said...

Excellent Post. Extra-ordinary work to share with. Thanks for posting.
Node JS Training in Chennai
Node JS Course in Chennai
Node JS Training Institutes in chennai
Node JS Course
Node JS Training in Anna Nagar
Node JS Training in Porur
Node JS Training in Adyar

easylearn said...

Hi,
Best article, very useful and well explanation. Your post is extremely incredible.Good job & thank you very much for the new information, i learned something new. Very well written. It was sooo good to read and usefull to improve knowledge. Who want to learn this information most helpful. One who wanted to learn this technology IT employees will always suggest you take Best Training institutes for python course in BTM Layout

easylearn said...

Very useful post,thanks for sharing.Very clear and understandable content.ElasticSearch is a search engine based on Lucene Library
elasticsearch training institute in btm layout

deiva said...

Thanks a lot very much for the high quality and results-oriented help. I won’t think twice to endorse your blog post to anybody who wants and needs support about this area.
java training in chennai

java training in omr

aws training in chennai

aws training in omr

python training in chennai

python training in omr

selenium training in chennai

selenium training in omr

Jayalakshmi said...

I really glad to read your great post and I like your written style. I am waiting for your numerous post...
web designing training in chennai

web designing training in tambaram

digital marketing training in chennai

digital marketing training in tambaram

rpa training in chennai

rpa training in tambaram

tally training in chennai

tally training in tambaram

jeni said...

I just loved your article on the beginners guide to starting a blog.If somebody take this blog article seriously in their life, he/she can earn his living by doing blogging.thank you for thizs article.
java training in chennai

java training in velachery

aws training in chennai

aws training in velachery

python training in chennai

python training in velachery

selenium training in chennai

selenium training in velachery

shiny said...

Excellent Post. Extra-ordinary work to share with. Thanks for posting. Best article to read.

hardware and networking training in chennai

hardware and networking training in annanagar

xamarin training in chennai

xamarin training in annanagar

ios training in chennai

ios training in annanagar

iot training in chennai

iot training in annanagar

vivekvedha said...

Thanks fro the great informative post keep shring!.
acte reviews

acte velachery reviews

acte tambaram reviews

acte anna nagar reviews

acte porur reviews

acte omr reviews

acte chennai reviews

acte student reviews

Suryaprakash said...

Good blog, it's really very informative, do more blog under good concepts.
Digital Marketing Course in OMR
Digital Marketing Course in T Nagar
Digital Marketing Course in Anna Nagar
Digital Marketing Course in Velachery
Digital Marketing Course in Tambaram

Patell Priya said...

Thank you much more for sharing the wonderful post. Keep updating here...
Excel Training in Chennai
Excel Advanced course
Pega Training in Chennai
Linux Training in Chennai
Tableau Training in Chennai
Unix Training in Chennai
Oracle Training in Chennai

Technogeekscs said...

Wow, it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and I got more information from your blog.
Data Science
Selenium
ETL Testing
AWS
Python Online Classes

borav said...

Welcome! Exceptionally supportive counsel inside this article! It is the little changes that produce the biggest changes. Much obliged for sharing!
liveskye

Angulajs Course in Pune said...

This is very interesting post about coding which is really helpful for Angularjs programming.

Aishwariya said...

Awesome post.Really nice blog, i enjoyed your infomations. Thank you and i will expect more in future..keep it up!!
Amazon Web Services Training in Chennai

Village Talkies said...

Informative blog! it was very useful for me.Thanks for sharing. Do share more ideas regularly.
Village Talkies a top-quality professional corporate video production company in Bangalore and also best explainer video company in Bangalore & animation video makers in Bangalore, Chennai, India & Maryland, Baltimore, USA provides Corporate & Brand films, Promotional, Marketing videos & Training videos, Product demo videos, Employee videos, Product video explainers, eLearning videos, 2d Animation, 3d Animation, Motion Graphics, Whiteboard Explainer videos Client Testimonial Videos, Video Presentation and more for all start-ups, industries, and corporate companies. From scripting to corporate video production services, explainer & 3d, 2d animation video production , our solutions are customized to your budget, timeline, and to meet the company goals and objectives.
As a best video production company in Bangalore, we produce quality and creative videos to our clients.

salome said...

Very interesting to read and useful article Thanks for sharing your work . keep up the good work Angular training in Chennai

Anonymous said...

Python Training in Bangalore

Python Classes in Bangalore

EZaccounting said...


Nice article, good content are shared. Thanks for sharing the article.
Psg grant software
Accounting software Singapore
Ez accounting

Back linker said...

iMyFone LockWiper (8.5.3) Crack is encryption tool for iOS, Android, passwords. Register imyfone lockwiper by serial key 2023 free download. ImyFone Lockwiper Android Crack Download

Sruthi Karan said...

Thanks for the Valuable information and it is really helpful information for me. Once again Thank you so much for sharing with us.
Uncontested Divorce in VA
VA Uncontested Divorce

haseeb said...

Christmas Wishes for Kids · Christmas is so much more fun when I get to celebrate with you! · Merry Christmas to the coolest kid I know! · Nothing .Christmas Greetings For Teenage Grandson

arjun said...

Innovations in packaging design

MeenaKK said...

Great post! I really enjoyed reading this and found it quite informative.Best AWS Training in Pune with Placement

Chaitanya said...

Appreciate you sharing, great article post. Really looking forward to read more. Cool.
Selenium With Python Training
Powershell Training
<a href="https://viswaonlinetrainings.com/courses/windows-server-online-training/>Windows Server Training</a>

lemive5303 said...

list of best job portals in india