DOS编程--记对文件的全量备份与更新
rem自动备份,更新。
@echo off
mode con cols=100 lines=400
rem 切换到脚本所在目录
pushd %~dp0
rem 输入参数
set /p backupPath_programDirectory=请输入,程序目录 备份目录 iis站点名称:
::set backupPath_programDirectory=D:\程序目录 D:\备份目录
for /f "tokens=1,2,3 delims= " %%a in ("%backupPath_programDirectory%") do (
set programRootDir=%%a
set backupPath=%%b
set iisSiteName=%%c
)
rem 获取更新包里的文件名,保存到文件filesName.txt中。
set file=filesName.txt
mkdir %backupPath% >nul 2>nul
::dir /b/s *.dll *.aspx *.xml *.js *.html *.xls *.ascx *.cshtml *.config *.Master *.cs > %file%
dir /a-d/b/s | findstr /v "backup__update.bat" | findstr /v "filesName.txt" > %file%
rem 去掉文件前面的当前目录的路径名称
set replace=%cd%
set replaceWith=
for /f "tokens=* delims= " %%i in (%file%) do (
SETLOCAL enabledelayedexpansion
set str=%%i
set str=!str:%replace%=%replaceWith%!
if not "%%~nxi%" == "backup__update.bat" (
if not "%%~nxi%" == "filesName.txt" (
echo !str! >> %file%.temp
)
)
ENDLOCAL
)
move %file%.temp %file% >nul 2>nul
copy %file% %backupPath% >nul 2>nul
echo.复制程序目录里的原文件,到备份目录里。
echo *************************************
echo 》》》开始备份
for /r %%i in (*.*) do (
SETLOCAL enabledelayedexpansion
set fileFullName=%%~nxi
set fileExtensionName=%%~xi
set relativePath=
set fileRelativePath=
if not "!fileFullName!" == "backup__update.bat" (
if not "!fileFullName!" == "filesName.txt" (
for /f "tokens=* delims= " %%j in (%file%) do (
set relativePath=%%~pj
set fileRelativePath=!relativePath!"!fileFullName!"
rem 通过文件名,找到文件名所在行,得到文件名称前面的路径名
echo "%%j" | findstr /c:"!fileFullName!" >nul
if !errorlevel! == 0 (
echo f | xcopy /y %programRootDir%!fileRelativePath! %backupPath%!fileRelativePath!
)
)
)
)
ENDLOCAL
)
echo 》》》备份结束!《《《
echo *************************************
pause
rem 停止站点
call :IIS_Site_Stop %iisSiteName%
echo.
echo *************************************
echo 》》》开始替换
for /r %%i in (*.*) do (
SETLOCAL enabledelayedexpansion
set fileFullName=%%~nxi
set relativePath=
set fileRelativePath=
if not "!fileFullName!" == "backup__update.bat" (
if not "!fileFullName!" == "filesName.txt" (
for /f "tokens=* delims= " %%j in (%file%) do (
set relativePath=%%~pj
set fileRelativePath=!relativePath!"!fileFullName!"
echo "%%j" | findstr /c:"!fileFullName!" >nul
if !errorlevel! == 0 (
:continue_copy
echo f | xcopy /y %cd%!fileRelativePath! %programRootDir%!fileRelativePath!
if not !errorlevel! == 0 (
ping -n 3 127.0.0.1 >nul 2>nul
goto :continue_copy
)
)
)
)
)
ENDLOCAL
)
echo 》》》替换完成!《《《
echo *************************************
rem 启动站点
call :IIS_Site_Start %iisSiteName%
echo.
pause
exit /b
::IIS站点(应用程序)
:IIS_Site_Start
echo 启动IIS站点
set var1=%1
C:\Windows\System32\inetsrv\appcmd.exe start site %var1%
C:\Windows\System32\inetsrv\appcmd.exe start apppool /apppool.name:%var1%
C:\Windows\System32\inetsrv\appcmd.exe recycle apppool /apppool.name:%var1%
GOTO:EOF
::IIS站点(应用程序)
:IIS_Site_Stop
echo 关闭IIS站点
set var1=%1
C:\Windows\System32\inetsrv\appcmd.exe stop site %var1%
C:\Windows\System32\inetsrv\appcmd.exe stop apppool /apppool.name:%var1%
GOTO:EOF