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.

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!)


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)

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 & "\" & & " last modified: " & file.datelastmodified
on error resume next
' === to test this script but not actually delete files, comment out the next line ===
end if


for each folder in subfolders
recurse folder

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 :)

garry dam said...

Nice post.Another software alternative that works is "LongPathTool" for error messages: Cannot read from source file or disk, there has been a sharing violation, cannot delete file or folder, the file name you specified is not valid or too long, the source or destination file may be in use and many other file managing errors.

Jahid Hasan said...

GSCopy Pro v6.0 (RoboCopy Alternative) with Open File Agent
GSCopyPro is a single command-line tool (CLI) that can copy, replicate and move files from one folder to another. This folder can be on the same machine/ server or another server elsewhere. What makes GSCopyPro stand out from other competitors is the fact it works on 32-bit as well as 64-bit systems and has no restrictions. It can easily be scheduled to run as a scheduled task and fully automated. GSCopyPro also comes with an open file agent which can copy files that are locked/ opened by other processes. This feature is supported in all windows versions from widows XP/ 2003 and later.
Go To:>>

Steve Brown 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...


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

Basilio Tambalong, Jr. said...

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

change maxlad to maxage...

locker said...

I like your thoughts due to your skills.