批处理备份文件夹和文件后发布文件夹至网站
程序员文章站
2022-04-29 17:13:24
先将"c:\网站"下对应"d:\工程1"下面的所有文件夹和文件备份到"d:\备份2012-9-1-52647",然后再把"d:\工程1"发布到"c:\网站"。 用法: ba...
先将"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
用法:
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