Windows Server bat deletes objects of a specified date

Source: Internet
Author: User
Tags current time echo date rar

In windows, the forfiles command can be used to operate files based on the timestamp of files,
Forfiles /? You can view detailed help information.


FORFILES [/P pathname] [/M searchmask] [/S] [/C command] [/D [+ |-] {yyyy/MM/dd | dd}]
/P "directory name" specifies the search path
/S recursive search
/C "specify the command to be executed"
/D query at the specified time
+ Yyyy/MM/dd specifies the absolute time to find the file that is later than the specified date.

-Dd files earlier than the current time and n days earlier than the current time

Displays the files in the specified directory that have not been modified for more than 6 days. By default, the file name is output.
Forfiles/p "% bak_dir %"/S/D-6
Forfiles/p "% bak_dir %"/S/D-6-C "cmd/c echo @ file"

Complete output path

Forfiles/p "% bak_dir %"/S/D-6-C "cmd/c echo @ path"

Delete objects in the specified directory that have been last modified for more than 6 days

Forfiles/p "% bak_dir %"/S/D-6/C "cmd/c del/q @ file"

Bytes

If exist "c: \ Program Files \ winrar" set Path =; "c: \ Program Files \ winrar"; % SystemRoot % \ system32
If exist "c: \ Program Files (x86) \ winrar" set Path =; "c: \ Program Files (x86) \ winrar"; % SystemRoot % \ system32
Forfiles/p "% bak_dir %"/S/D-6/C "cmd/c rar.exe a backp.rar @ path"


Bat obtains the previous month.

@ Echo off
Setlocal EnableDelayedExpansion
Pause
Rem ################# obtain the date of the previous month ################## ######
Rem get year
Set year = % date :~ 0, 4%
Rem obtains the month.
Echo current year: % year %
Set mounth = % date :~ 5, 2%
Echo Current Month: % mounth %
Echo #####################
If "% mounth %" = "01 "(
Set/A year = % year %-1
Set/A lastmounth = 01
Echo current year: % year % _ % mounth %
Echo **************
) Else (
Rem minus one for the current month
Echo % mounth %
Set/A lastmounth =! Mounth! -1
Echo previous month :! Lastmounth!
Rem if the value of 1 in the current month is less than 10, then add it with zero, for example, 2 = 02.
If! Lastmounth! Lss 10 (
Set lastmounth =! Lastmounth!
 )
Echo last month: % year % _! Lastmounth!
)
Pause
Method 2

?
@ Echo off
Rem setlocal
Echo date: % date %
REM separation Year Month Day
@ Set year = % date :~ 0, 4%
Set month = % date :~ 5, 2%
Set day = % date :~ 8, 2%
Echo this year: % year %
Echo this month: % month %
Echo ############# test #################
If "% month %" = "01 "(
Set/A lastyear = % year %-1
Echo last year: % lastyear %
Set lastmonth = 12
Echo last month: % lastmonth %
Echo last year_month: % lastyear % _ % lastmonth %
Echo ############ month = 01 #############
) Else (
If "% month %" = "02" set lastmonth = 01
If "% month %" = "03" set lastmonth = 02
If "% month %" = "04" set lastmonth = 03
If "% month %" = "05" set lastmonth = 04
If "% month %" = "06" set lastmonth = 05
If "% month %" = "07" set lastmonth = 06
If "% month %" = "08" set lastmonth = 07
If "% month %" = "09" set lastmonth = 08
If "% month %" = "10" set lastmonth = 09
If "% month %" = "11" set lastmonth = 10
If "% month %" = "12" set lastmonth = 11
Echo last year_month: % year % _ % lastmonth %
Echo ############## month! = 01 ##############
)
Pause

Related Article

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.