Scripting help: Deleting files more than 3 days old

Posted on 2008-11-19
Last Modified: 2013-11-25
I have the following script (SEE BELOW) running to automate my backups using 2003 Server.  The script makes th ebackup, then copies the bkf to a remote server.

The last peice of the puzzle is to do some cleanup

1. delete the bkf files that reside in the source folder one they are copied to the source folder
2.  Delete any bkfs older than3 days so I always have the last 3 backups availlable for restore.
@for /f "Tokens=1-4 Delims=/ " %%i in ('date /t') do  @set dt=%%i-%%j-%%k-%%l
@set dt=%dt%
@echo Copying backup set:  %dt%.
@echo Please wait...
@ntbackup.exe backup "@C:\Documents and Settings\Administrator\Local Settings\Application Data\Microsoft\Windows

NT\NTBackup\data\backup set.bks" /a /d "STVPCSG01 %dt%" /v:no /r:no /rs:no /hc:off /m normal /j "%dt%" /l:s /f

"c:\Auto Backups\STVPCSG01 %dt%.bkf"

@for /f "Tokens=1-4 Delims=/ " %%i in ('date /t') do  @set dt=%%i-%%j-%%k-%%l
@set dt=%dt%
@echo Sending backup file to STVPDPM01. Please wait...
@xcopy "c:\Auto Backups\STVPCSG01 %dt%.bkf" "\\STVPDPM01\DMZ$" /Y
@echo Finished.

Question by:pflecha

    Expert Comment

    try the FORFILES.EXE server resource kit tool - works on NT, 2000, XP, 2003, etc

    FORFILES -pc:\foldername -s -m*.* -d-30 -c"CMD /C del @FILE"

    -p = path
    -s = include subdirs
    -m = match filetype
    -d = age in days (can also be set as an absolute date ie DDMMYYYY)
    -c = command to execute

    Note there are no spaces between the switches and their arguments.
    info here:
    Download it here:


    Author Comment

    It is deleting all files regardless of the date of the file

    FORFILES /p "D:\DMZ$" /D -5 /M *.vhd -c "CMD /c del *.vhd"

    Does the -5 mean to delete any files older than 5 days?  That is my goal.

    Author Comment

    Does anybody have experience using the FORFILES to delete files older than a specific date?  I need to automate the capacity management of a specific folder.   Any files older than 5 days need to be deleted.

    Author Comment

    The command I am trying is

    FORFILES /p "c:\backups" /D -5 "cmd /c *.bkf"

    Author Comment

    Does anybody know how I can  automate the deletion of these files?

    Accepted Solution

    OK I figured it out.

    Forfiles -p D:\FOLDER -m *.bkf -d -10 -c "CMD /C del @file"

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    VM backups can be lost due to a number of reasons: accidental backup deletion, backup file corruption, disk failure, lost or stolen hardware, malicious attack, or due to some other undesired and unpredicted event. Thus, having more than one copy of …
    VM backup deduplication is a method of reducing the amount of storage space needed to save VM backups. In most organizations, VMs contain many duplicate copies of data, such as VMs deployed from the same template, VMs with the same OS, or VMs that h…
    This tutorial will walk an individual through locating and launching the BEUtility application to properly change the service account username and\or password in situation where it may be necessary or where the password has been inadvertently change…
    This tutorial will walk an individual through the process of installing the necessary services and then configuring a Windows Server 2012 system as an iSCSI target. To install the necessary roles, go to Server Manager, and select Add Roles and Featu…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now