根据修改日期复制文件的脚本

根据修改日期复制文件的脚本

我需要运行一个脚本,根据修改日期将文件从一个目录复制到另一个目录。基本上,我每天都会将数据库转储到一个目录中,我希望能够将每月第一天的数据库复制到另一个目录,这样我就可以删除文件夹中的所有其他文件。

有人能帮助我吗?

这是我想要在 Windows Server 2003 上运行的脚本。

提前致谢。

答案1

使用 Robocopy,它是操作系统的内置功能,语法应该类似于此。

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

创建并将其保存为批处理文件,让计划任务每​​天运行 23 小时,然后终止该进程并重新启动它。

这是所有命令和功能的技术参考http://technet.microsoft.com/en-us/library/cc733145(WS.10).aspx

答案2

您可以安排批处理文件在每个月的第一天删除超过 1 天的文件(通过 delage32 来自http://home.mnet-online.de/horst.muc/wcon.htm) 并将剩余文件复制到另一个文件夹。

答案3

Powershell 也可以轻松做到这一点。下面是我使用的脚本的清理片段:

$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\
}

脚本的“动词”可以很容易地是 move-item 或 remove-item。

相关内容