@echo off
setlocal enabledelayedexpansion
::超过几个月
set mon=0
::目标路径
set target="d:\del\"
::当前路径
set level1=%~dp0
::遍历所有文件
for /r %%i in (*) do (
rem 将文件名中的%号转换
set location="%%i"
set location=!location:%%=%%%%!
rem 判断文件创建日期是否大于指定月数
call :getTime !location! fileTime
set /a diff=%date:~0,2%-!fileTime:~0,2!
rem 如果日期大于等于指定月份,移动文件
if !diff! geq %mon% (
rem 设置目标路径
set subDir=%%~dpi
set subDir="%target:"=%!subDir:%~dp0=!"
xcopy "%%i" !subDir! /y
rem 如果不是本文件,删除文件
if %%~nxi neq %~nx0 del "%%i"
)
)
pause & exit
::得到创建时间
:getTime
for /f "skip=4 tokens=1" %%a in ('dir %1 /tc') do set %2=%%a & goto :eof
只转移bat所在的文件夹(包括子文件夹)的所有文件,然后删除.