本帖最后由 aa77dd@163.com 于 2015-7-20 11:14 编辑
- @echo off & setlocal enabledelayedexpansion
- if not exist "BAK\" md "BAK\"
- set "dttm=%date:/=_%%time:~0,2%%time:~3,2%%time:~6,2%"
- md "BAK\%dttm%\"
- for /f "delims=" %%i in ('dir /b /s *.txt^&echo;0') do (
- for %%a in ("%%i\..\..") do if /i "%%~na" neq "BAK" (
- if "%%~spi" neq "!lastdir!" (
- if "!lastdir!!max!" neq "" (
- echo copy "!lastdir!!max!.txt" "BAK\%dttm%\!lastdir:\=_!!max!.txt"
- copy "!lastdir!!max!.txt" "BAK\%dttm%\!lastdir:\=_!!max!.txt"
- )
- set "max=0"
- )
- set /a "t=max-%%~ni>>31, max = t&%%~ni|~t&max"
- set "lastdir=%%~spi"
- )
- )
- pause
复制代码 把批处理文件放在总目录中运行
在总目录中会建立一个 BAK 的目录, 用于存放 备份的文件, 每一次运行 都会在 BAK 下建立一个当前时间命名的新目录, 然后在其中存放备份的文件
为避免备份时重名文件的覆盖问题, 备份的文件都带有一个原始短路径的前缀 |