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.
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!)
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!)
Comments
robocopy e:\empty e:\to_delete /MIR /E
e:\empty is a empty dir. its fast :-)
' 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
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
FORFILES /P C:\Source /D -14 /C "cmd /c Del @file"
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
change maxlad to maxage...
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.
ABC-TEMP-001_20180409-1201
I tried %COMPUTERNAME%_%datetimestamp%, but that end ups looking for the foldername with the current date/time stamp.
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.
rpa Training in Bangalore
RPA Training in Hyderabad
RPA Training in Chennai
RPA Training in Bangalore
Best Training and Real Time Support
Oracle Bpm Training
Oracle Osb Training
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
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
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
elasticsearch training institute in btm layout
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
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
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
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
acte reviews
acte velachery reviews
acte tambaram reviews
acte anna nagar reviews
acte porur reviews
acte omr reviews
acte chennai reviews
acte student reviews
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
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
Data Science
Selenium
ETL Testing
AWS
Python Online Classes
liveskye
Amazon Web Services Training in Chennai
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.
Python Classes in Bangalore
Nice article, good content are shared. Thanks for sharing the article.
Psg grant software
Accounting software Singapore
Ez accounting
Uncontested Divorce in VA
VA Uncontested Divorce
Selenium With Python Training
Powershell Training
<a href="https://viswaonlinetrainings.com/courses/windows-server-online-training/>Windows Server Training</a>