Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use batch backup Directory in windows

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to use batch backup directory in windows. It is very detailed and has certain reference value. If you are interested, you must finish reading it.

The following is a bat batch script that needs to back up files, backing up the files under yesterday's C:\ Users\ Administrator\ Desktop\\ 111\ 2020\ 03\ 05 directory

If you need to write a batch file, first consider the need to get yesterday's date, and create a date directory to store the backup, and check the number of files to back up and the size of the file.

The number of files backed up is compared before and after. The code is as follows:

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: "get yesterday's time"

@ 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% an 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%

The date of echo yesterday was:% strLstDt%

Echo.

Rem: "dirPath defines the path to the pre-backup file (% LY%/%LM%/%LD% fixed format) tarPath defines the path to the backup file logpath defines the log path generated during the backup"

Set dirPath=C:\ Users\ Administrator\ Desktop\ New folder\ 111\% LY%/%LM%/%LD%

Set tarPath=C:\ Users\ Administrator\ Desktop\ New folder

Set logpath=C:\ Users\ Administrator\ Desktop\ New folder\ logs

Rem: "View the number and size of pre-backed up files"

Cd dirPath%

Setlocal enabledelayedexpansion

Set / a countbegin = 0

Set / a file1sizebegin = 0

For / f% I in ('dir / b / s / a Murd`) do (

Set / a countbegin=countbegin+1

Set / a file1sizebegin=file1sizebegin+%%~zi

)

Echo = > >% logpath%\ "% strLstDt%_%suiji%" .log

Echo = > >% logpath%\ "% strLstDt%_%suiji%" .log

Echo pre-backup directory% cd% files:! countbegin! File size and:! file1sizebegin! Bytes > >% logpath%\ "% strLstDt%_%suiji%" .log

Echo = > >% logpath%\ "% strLstDt%_%suiji%" .log

)

Popd

Rem: "create backup directories and backup pre-backed files"

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 creates a directory to back up:% targetpath% successful! > >% logpath%\ "% strLstDt%_%suiji%" .log

Echo = > >% logpath%\ "% strLstDt%_%suiji%" .log

Echo = > >% logpath%\ "% strLstDt%_%suiji%" .log

Set aversion% time%

Echo starts the backup at% copytimebegin% > >% logpath%\ "% strLstDt%_%suiji%" .log

Echo D | xcopy% dirPath%\ *. *% targetpath% / s / Q

Echo = > >% logpath%\ "% strLstDt%_%suiji%" .log

Echo = > >% logpath%\ "% strLstDt%_%suiji%" .log

Echo backup files to% targetpath% successful! > >% 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 baked% time%

Echo ends backup with% copytimeend% > >% logpath%\ "% strLstDt%_%suiji%" .log

Echo = > >% logpath%\ "% strLstDt%_%suiji%" .log

Rem: "View the number and size of backup files"

Cd "targetpath%"

Setlocal enabledelayedexpansion

Set / a count = 0

Set / a file1size = 0

For / f% I in ('dir / b / s / a Murd`) do (

Set / a count=count+1

Set / a file1size=file1size+%%~zi

)

Echo = > >% logpath%\ "% strLstDt%_%suiji%" .log

Echo backup directory% number of cd% files:! count! File size and:! file1size! Bytes > >% logpath%\ "% strLstDt%_%suiji%" .log

Echo = > >% logpath%\ "% strLstDt%_%suiji%" .log

)

Popd

Echo = > >% logpath%\ "% strLstDt%_%suiji%" .log

Rem: "calculate the total time taken from the start of the backup to the end of the backup"

@ echo off

Set / a h _ 2 percent a _

Set / a m 1m 1m 1% av v v 3 m 2% m 100

Set / a s1mm 1% apura 6pas 2% Rue 100

Set / a h3pm% borel 0pl 2%

Set / a m2mm / 1% bvl / m2mm / a / m2pm / a m2pm / a m2mm / a m2mm / a m2pm / a m2mm / a m2pm / a m2mm / a m2mm / a m2pm / a m2mm / a / m2mm / a

Set / a s2o1% bvuanju 6jin2% mur100

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 this backup! count! The file was successful! Total backup time is h hours m minutes s seconds > 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: check pre-backup files and backup files

If% countbegin% =% count% if% file1sizebegin% = =% file1size% (the number and size of echo pre-backup files are equal to the number and size of backup files. This backup check is successful! > >% logpath%\ "% strLstDt%_%suiji%" .log)

Else (

Echo this backup failed!

)

Echo + > >% logpath%\ "% strLstDt%_%suiji%" .log

Echo + > >% logpath%\ "% strLstDt%_%suiji%" .log

Echo = > >% logpath%\ "% strLstDt%_%suiji%" .log

Echo = > >% logpath%\ "% strLstDt%_%suiji%" .log

Exit

The above is all the contents of the article "how to use batch backup catalogs in windows". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report