共计 6306 个字符,预计需要花费 16 分钟才能阅读完成。
这篇文章主要介绍 windows 中如何使用批处理备份目录,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
以下是需要备份文件的一个 bat 批处理脚本,备份昨天 C:\Users\Administrator\Desktop\\111\2020\03\05 目录下面的文件,
需要写一个批处理文件,首先考虑需要获取昨天的日期,以及创建一个日期的目录存放备份,查看要备份的文件个数文件的大小,
备份完的文件大小个数的多少,在进行前后的比较一下,代码如下:
rem: author is czb
rem: create scripts time 2020-03-05
@echo off
set date_time=%date:~0,4%%date:~5,2%%date:~8,2%_0%time:~1,1%%time:~3,2%%time:~6,2%
set suiji=%random%
echo initing………..
rem: 获取昨天的时间
@echo off
set YE=%date:~0,4%
set MO=%date:~5,2%
set DA=%date:~8,2%
set DG=1
set/a vY1=%YE% %% 400
set/a vY2=%YE% %% 4
set/a vY3=%YE% %% 100
if %vY1%==0 (set var=true) else (if %vY2%==0 (if %vY3%==0 (set var=false) else (set var=true)) else (set var=false))
set LY=%YE%
set LM=%MO%
if %MO:~0,1%==0 (set MO=%MO:~1,1%)
if %DA:~0,1%==0 (set DA=%DA:~1,1%)
if %DA% GTR %DG% (set/a LD=%DA%-%DG%) else (
if %MO%==1 (set/a LY=%YE%-1) (set/a LM=12) (set/a LD=31+%DA%-%DG%) else (
set/a LM=%MO%-1
if %MO%==3 (if %var%==false (set/a LD=28+%DA%-%DG%) else (set/a LD=29+%DA%-%DG%))
for %%a in (2 4 6 8 9 11) do (if %MO% == %%a (set/a LD=31+%DA%-%DG%))
for %%b in (5 7 10 12) do (if %MO% == %%b (set/a LD=30+%DA%-%DG%))))
if %LM% LSS 10 set LM=0%LM:~-1%
if %LD% LSS 10 set LD=0%LD:~-1%
set strLstDt=%LY%%LM%%LD%
echo 昨天的日期为:%strLstDt%
echo.
rem: dirPath 定义预备份文件的路径(%LY%/%LM%/%LD% 固定格式)tarPath 定义备份文件的路径 logpath 定义备份时产生的日志路径
set dirPath=C:\Users\Administrator\Desktop\ 新建文件夹 \111\%LY%/%LM%/%LD%
set tarPath=C:\Users\Administrator\Desktop\ 新建文件夹
set logpath=C:\Users\Administrator\Desktop\ 新建文件夹 \logs
rem: 查看预备份文件的个数以及大小
cd %dirPath%
setlocal enabledelayedexpansion
set /a countbegin = 0
set /a file1sizebegin = 0
for /f %%i in (dir /b /s /a-d) do (
set /a countbegin=countbegin+1
set /a file1sizebegin=file1sizebegin+%%~zi
)
echo =============================================================================================== %logpath%\ %strLstDt%_%suiji% .log
echo =============================================================================================== %logpath%\ %strLstDt%_%suiji% .log
echo 预备份目录 %cd% 文件数:!countbegin! 个 文件大小和:!file1sizebegin! 个字节 %logpath%\ %strLstDt%_%suiji% .log
echo =============================================================================================== %logpath%\ %strLstDt%_%suiji% .log
)
popd
rem: 创建备份目录以及备份预备份的文件
set str_time_first_bit= %time:~0,1%
if %str_time_first_bit%== (
set str_date_time=%date:~0,4%%date:~5,2%%date:~8,2%_0%time:~1,1%%time:~3,2%%time:~6,2%
)else (
set str_date_time=%date:~0,4%%date:~5,2%%date:~8,2%_%time:~0,2%%time:~3,2%%time:~6,2%
)
echo The folder is about to be created: %strLstDt%_%suiji%_backups
set targetpath= %tarPath% \%strLstDt%_%suiji%_backups
mkdir %targetpath%
set copytimebegin=%date:~0,4%-%date:~5,2%-%date:~8,2% %time:~0,2%:%time:~3,2%:%time:~6,2%
echo =============================================================================================== %logpath%\ %strLstDt%_%suiji% .log
echo 创建要备份的目录: %targetpath% 成功!!!!%logpath%\ %strLstDt%_%suiji% .log
echo =============================================================================================== %logpath%\ %strLstDt%_%suiji% .log
echo =============================================================================================== %logpath%\ %strLstDt%_%suiji% .log
set a=%time%
echo 开始执行备份,开始备份的时间为 %copytimebegin% %logpath%\ %strLstDt%_%suiji% .log
echo D|xcopy %dirPath%\*.* %targetpath% /s /q
echo =============================================================================================== %logpath%\ %strLstDt%_%suiji% .log
echo =============================================================================================== %logpath%\ %strLstDt%_%suiji% .log
echo 备份文件到 %targetpath% 成功!!!!%logpath%\%strLstDt%_%suiji%.log
echo =============================================================================================== %logpath%\ %strLstDt%_%suiji% .log
echo =============================================================================================== %logpath%\ %strLstDt%_%suiji% .log
set copytimeend=%date:~0,4%-%date:~5,2%-%date:~8,2% %time:~0,2%:%time:~3,2%:%time:~6,2%
set b=%time%
echo 结束执行备份,结束备份的时间为 %copytimeend% %logpath%\ %strLstDt%_%suiji% .log
echo =============================================================================================== %logpath%\ %strLstDt%_%suiji% .log
rem: 查看备份文件的个数以及大小
cd %targetpath%
setlocal enabledelayedexpansion
set /a count = 0
set /a file1size = 0
for /f %%i in (dir /b /s /a-d) do (
set /a count=count+1
set /a file1size=file1size+%%~zi
)
echo =============================================================================================== %logpath%\ %strLstDt%_%suiji% .log
echo 备份目录 %cd% 文件数:!count! 个 文件大小和:!file1size! 个字节 %logpath%\ %strLstDt%_%suiji% .log
echo =============================================================================================== %logpath%\ %strLstDt%_%suiji% .log
)
popd
echo =============================================================================================== %logpath%\ %strLstDt%_%suiji% .log
rem : 计算开始备份到结束备份总共花费的时间
@echo off
set /a h2=%a:~0,2%
set /a m1=1%a:~3,2%-100
set /a s1=1%a:~6,2%-100
set /a h3=%b:~0,2%
set /a m2=1%b:~3,2%-100
set /a s2=1%b:~6,2%-100
if %h3% LSS %h2% set /a h3=%h3%+24
set /a ts1=%h2%*3600+%m1%*60+%s1%
set /a ts2=%h3%*3600+%m2%*60+%s2%
set /a ts=%ts2%-%ts1%
set /a h=%ts%/3600
set /a m=(%ts%-%h%*3600)/60
set /a s=%ts%%%60
echo 本次备份!count! 个文件成功!!!备份总共耗时的时间为 %h% 小时 %m% 分 %s% 秒 %logpath%\ %strLstDt%_%suiji% .log
echo =============================================================================================== %logpath%\ %strLstDt%_%suiji% .log
echo =============================================================================================== %logpath%\ %strLstDt%_%suiji% .log
echo ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %logpath%\ %strLstDt%_%suiji% .log
echo ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %logpath%\ %strLstDt%_%suiji% .log
rem : 校验预备份文件与备份文件
if %countbegin% == %count% if %file1sizebegin% == %file1size% (echo 预备份文件的个数、文件的大小 与 备份文件的个数、大小相等,此备份校验成功!!!!%logpath%\ %strLstDt%_%suiji% .log)
else (
echo 此备份备份失败!!!!
)
echo ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %logpath%\ %strLstDt%_%suiji% .log
echo ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %logpath%\ %strLstDt%_%suiji% .log
echo =============================================================================================== %logpath%\ %strLstDt%_%suiji% .log
echo =============================================================================================== %logpath%\ %strLstDt%_%suiji% .log
exit
以上是“windows 中如何使用批处理备份目录”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注丸趣 TV 行业资讯频道!