@echo off set SourceDir=%1 set now=%date:~0,4%-%date:~5,2%-%date:~8,2%-%time:~0,2%%time:~3,2%%time:~6,2% ::参数 set DestDir="C:/网站" set BackDir="D:/备份%now%" echo. echo 正在备份 call:Backup %SourceDir% %DestDir% %BackDir% echo. echo 正在发布 call:Publish %SourceDir% %DestDir% goto :eof :: 主程序 :Backup setlocal enabledelayedexpansion set SourceDir=%~1 set DestDir=%~2 set BackDir=%~3 :: 计算源路径长度 set SourceDirLen=0 set SourceDirStr=%SourceDir% :next1 if not "%SourceDirStr%"=="" ( set /a SourceDirLen+=1 set "SourceDirStr=%SourceDirStr:~1%" goto :next1 ) :: 备份 for /r "%SourceDir%" %%i in (*.*) do ( set BackPath=%%~dpi set BackPath=%BackDir%!BackPath:~%SourceDirLen%! set DestPath=%%i set DestPath=%DestDir%!DestPath:~%SourceDirLen%! set BackFilePath=%%i set BackFilePath=%BackDir%!BackFilePath:~%SourceDirLen%! echo !DestPath! xcopy "!DestPath!" "!BackPath!" /r /y >nul 2>nul if not exist "!BackFilePath!" (echo 备份复制失败) ) for /r "%SourceDir%" %%i in (/) do ( set SourcePath=%%i set SourcePath=!SourcePath:~0,-1! set BackPath=%BackDir%!SourcePath:~%SourceDirLen%! md "!BackPath!" >nul 2>nul ) setlocal disabledelayedexpansion goto :eof :: 发布 :Publish set SourceDir=%1 set DestDir=%2 xcopy %SourceDir% %DestDir% /r /y /i /e goto :eof