Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to run a script which will copy a file from one directory to another based on the date it was modified. Basically I have a database dumped into a directory on a daily basis and I want to be able to copy the database from the first of each month to a different directory so that I can delete have all the other files in the folder deleted.

Can anybody help me out?

This is a script which I would like to run on Windows Server 2003.

Thanks in advance.

share|improve this question

Use Robocopy it's a built in function of the operating system the syntax should be something along the lines of this.

Robocopy “\source” "\destination" /mir /mon:1 /mot:1 /R:3 /W:10 /COPY:DAT /log:c:\tools\robolog.txt /NDL /NP /Z

create and save this as a batch file have a scheduled task run it 23 hours a day and the process kill and restart it afterwards.

here is the technet reference for all commands and functions

share|improve this answer

You could schedule a batch file the first of each month to delete the files older than 1 day (via delage32 from and copy the remaining file to another folder.

share|improve this answer

Powershell can also do this easily. Here's a sanitized snippet of a script I use:

$BaseDirectory = C:\temp
$SelectionFilter = "*.tmp"
$intFileAge = 7
$DueDate = (get-date).addDays(-$intFileAge)

# Create an array of files where CreationDate is before DueDate 
# and it's not a directory

$arrFiles = get-childitem -recurse -path $BaseDirectory -Filter $SelectionFilter | `
Where-object {$_.CreationTime -le $DueDate -and $_.psIsContainer -ne $true}

Foreach ($oldfile in $arrFiles) {

    Copy-Item $oldFile.fullName -destination c:\elsewhere\

The "verb" of the script could as easily be move-item or remove-item.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.