How to get the system date the day before

Source: Internet
Author: User
Tags command line goto backup

Demand:

Because has been doing data backup, daily tedious backup and delete, really very troublesome. And sometimes when you forget to delete the backed-up data and the disk is full, the data for the day cannot be fully backed up. And if what day data just did not back up well, and the server again out of the problem, but this happened, the Cup.

So now want to write a number of batch processing commands, so that the system daily backup and delete one weeks before the data, so that is easy, and will not forget, after all, the machine as long as the set up, is exactly according to our wishes to do things. Here to share the results of the next few days, shortcoming.

First look at the system date format:

Note that the system's short date format is: YY/MM/DD, the following code can be used to get the date of any day before the current date, and you can create a system in the name of the date of the folder to do a backup. The code is as follows: (This code I did under the Windows7 test)

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/OS/skills/

@echo off

REM calculates the date before the specified number of days

Set daysago=7

REM assumes that the format of the system date is YY/MM/DD, be sure to pay attention to the system format, or you may be wrong

Call:D atetodays%date:~0,4%%date:~4,2%%date:~6,2% passdays

set/a passdays-=%daysago%

Call:D aystodate%passdays% dstyear dstmonth dstday

Set dstdate=%dstyear%%dstmonth%%dstday%

echo%daysago% days before the date is%dstdate%

Set Targetdriver=c:\

REM creates a folder in C disk that takes the name of a prior day, such as 20120627

MD%targetdriver%%dstdate%

Goto:eof

:D atetodays%yy%%mm%%dd% days

Setlocal enableextensions

Set Yy=%1&set Mm=%2&set dd=%3

If 1%yy% LSS if 1%yy% LSS 170 (set yy=20%yy%) Else (set yy=19%yy%)

REM set/a dd=100%dd%%%100,mm=100%mm%%%100

set/a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2

set/a j=j/5+dd+y*365+y/4-y/100+y/400-2472633

Endlocal&set%4=%j%&goto:eof

:D aystodate%days% yy mm DD

Setlocal enableextensions

set/a A=%1+2472632,b=4*a+3,b/=146097,c=-b*146097,c/=4,c+=a

set/a d=4*c+3,d/=1461,e=-1461*d,e/=4,e+=c,m=5*e+2,m/=153,dd=153*m+2,dd/=5

set/a DD=-DD+E+1,MM=-M/10,MM*=12,MM+=M+3,YY=B*100+D-4800+M/10

(If%mm% LSS set mm=0%mm%) & (if%dd% LSS set dd=0%dd%)

Endlocal&set%2=%yy%&set%3=%mm%&set%4=%dd%&goto:eof

You can use the above code copy to save it as a. bat file.

Other than that:

If the date format is: YYYY-MM-DD, the 5th line of code above should read as follows:

Call:D atetodays%date:~0,4%%date:~5,2%%date:~8,2% passday

Tested in XP also changed, in the server will be very different, so want to do extrapolate, still have to read the above code in the meaning of a sentence:

Call:D atetodays%date:~0,4%%date:~4,2%%date:~6,2% passdays

In the cmd command line, enter: date, which displays the format above:

So the general meaning of the above sentence is: to get 1-4 bits for the year, 5-6 for the month, 7-8 for the day, but some of the date format in the middle of/or-will be counted, so you need to try more.

This article comes from "Robin's Home" blog, please be sure to keep this source http://winteragain.blog.51cto.com/1436066/911183

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.