Skip to Content
Author's profile photo Ananda Theerthan

Delete Files Older than N days(UNC Path) in Data Services

There are scenarios where you are required to delete files older than certain days from archive folder using Data Services. Below script will make that happen which will work on UNC paths.

Replace 10 with number of days files to be kept, you can use variable. Any files older than this will be deleted.


$GV_FILES_DELETED =FN_DOS('PushD "[$GV_ARCHIVE_DIRECTORY]" && (forfiles -s -m *.* -d -10 -c "cmd /c echo @file") & PopD');
Print( 'Files that will be deleted from Archive folder are ' || $GV_FILES_DELETED );
$GV_FILES_DELETED =FN_DOS('PushD "[$GV_ARCHIVE_DIRECTORY]" && (forfiles -s -m *.* -d -10 -c "cmd /c del /q @path") & PopD');
Print('Files Older than ' || $GV_FILE_RETENTION_DAYS || ' days are being deleted now');

FN_DOS will have following code

/wp-content/uploads/2016/01/aa_868636.png

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.