当前位置: 移动技术网 > IT编程>网络>Dos/Bat > 批处理备份文件夹和文件后发布文件夹至网站

批处理备份文件夹和文件后发布文件夹至网站

2017年12月08日  | 移动技术网IT编程  | 我要评论
先将"c:\网站"下对应"d:\工程1"下面的所有文件夹和文件备份到"d:\备份2012-9-1-52647",然后再把"d:\工程1"发布到"c:\网站"。
用法
backupandpublish.bat "d:\工程1"
复制代码 代码如下:

@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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网