@echo off rem 格式化日期 rem date出来的日期是"2006-02-22 星期三",不能直接拿来使用,所以应该先格式化一下 rem 变成我们想要的。date:~0,4的意思是从0开始截取4个字符 set d=%date:~0,4%%date:~5,2%%date:~8,2% rem 设定压缩程序路径,这里用的是WINRAR的rar.exe进行打包的 setpath=C:\Program Files\WinRAR rem 设定要备份的目录 set srcDir=D:\databasc rem 设定备份文件所在目录 set dstDir=E:\temp\backup rem 设定备份文件的前缀,目前为temp,前缀为backup set webPrefix= rem 如果文件不存在,开始备份 ifnotexist%dstDir%%webPrefix%%d%.rar start Rar a -r %dstDir%%webPrefix%%d%.rar %srcDir% @echo on
@echo off rem ****************************** rem * 按时间删除文件目录的批处理 * rem ****************************** rem 设置临时目录的路径 set tempDir=%tmp%\remove_%date:~0,10% ifnotexist%tempDir%md%tempDir% rem 设置处理日期的脚本文件的路径 set scriptFile=%tempDir%\get_date.vbs rem 获得要保留的天数 set days=%~1 if "%days%" == "" goto printUsage rem 获得目标目录的路径 set dirPath=%~2 if "%dirPath%" == "" set dirPath=. rem 获得要操作的文件形式 set fileSpec=%~3 if "%fileSpec%" == "" set fileSpec=*.* rem 生成计算日期的脚本文件并获得删除的截止日期 echo d=date()-%1 > %scriptFile% echo s=right("0000" ^& year(d),4) ^& "-" ^& right("00" ^& month(d),2) ^& "-" ^& right("00" ^& day(d),2) >> %scriptFile% echo wscript.echo s >> %scriptFile% for /f %%iin ('cscript /nologo %scriptFile%') doset lastDate=%%i rem 处理目标目录里的每个对象 for /f "tokens=1,2,3* delims=<> " %%iin ('dir "%dirPath%\%fileSpec%" /a /-c /tc') docall :proc "%%i" "%%j" "%%k" "%%l" goto :done rem 处理目标目录里对象的过程 :proc rem 获得对象的创建日期并判断是否为有效格式 set fileDate=%~1 echo%fileDate% | findstr "[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]" > nul iferrorlevel1goto end rem 获得对象的类型 set fileType=%~3 if "%fileType%" == "" goto end rem 获得对象的名称 set fileName=%~4 if "%fileName%" == "" goto end if "%fileName%" == "." goto end if "%fileName%" == ".." goto end if "%fileName%" == "字节" goto end if "%fileName%" == "可用字节" goto end rem 判断对象日期是否小于或等于删除的截止日期 if "%fileDate:~0,10%" leq "%lastDate%" ( echo deleting "%fileName%" ... if "%fileType%" == "DIR" ( rd /s /q "%dirPath%\%fileName%" ) else ( del /q /f "%dirPath%\%fileName%" ) ) goto end :error echo An error occurred during backuping. :done rd /s /q %tempDir% goto end :printUsage echo Usage: %0 ^<Days^> [Work directory] [Target file specification (can include wildcards)] goto end :end
@echo off ::年月日字符串 set str_date=%date:~0,4%%date:~5,2%%date:~8,2% ::星期几 set str_week_val=%date:~-1% ::本地要备份的文件夹路径 set str_webpath_local=D:\wwwroot ::本地备份文件存放路径 set str_path_local=D:\web_bak ::网站压缩包名称前缀 set str_rar_name=MH_WEB ::异地 备份路径 set str_path_day=z:\最新7天网站备份 ::异地 备份路径 set str_path_weekend=z:\最新4个周末的网站备份 ::异地 备份路径 set str_path_month=z:\每个月底的网站备份
::取两位月份数字 set m=%date:~5,2% ::第一位为0则只取个位数 if %m:~0,1%==0 set /a m=%m:~1,1%*1 ::取四位年份数字(这个命令用不到5位数那年吧) set /a x=%date:~0,4% ::闰年判断条件1 %%代表取余 set /a y=%x%*1%%4 set /a y2=%x%*1%%100 ::闰年判断条件2 %%代表取余 set /a y3=%x%*1%%400 ::默认2月份只有28天 set ld=28 ::满足闰年条件1 if %y%==0 ( if %y2% NEQ 0 set ld=29 ) ::满足闰年条件2 if %y3%==0 set ld=29 for %%i in (135781012)do (if %m%==%%i set /a ld=31) for %%i in (46911)do (if %m%==%%i set /a ld=30) ::echo 日期:%ld% ::pause
::当月月底的年月日字符串 set str_monthend_val=%date:~0,4%%date:~5,2%%ld%%