Batch BAT deletes a folder before the specified number of days date _dos/bat

Source: Internet
Author: User
Tags goto

"Scheme one" BAT + REG + VBS

Copy Code code as follows:

@echo off
:: Demo: Deletes a folder that precedes the specified number of days in the specified path, whichever is the last modified date of the folder.
:: If the demo results are correct, remove the echo from the front of Rd to achieve the real deletion.
:: This example calls a temporary VBS code for a date calculation
:: This example is compatible with different date formats, call the REG command (XP system from the band) to set the date format uniformly,
:: After processing, then the date format back to the original state.

REM Specifies the location of the folder to be deleted
Set Srcdir=c:/test
REM Specify the number of days
Set Daysago=1
for/f "skip=2 delims="%%a in (' reg query ' Hkey_current_user/control panel/international '/V sshortdate ') do (
Set "Regdateold=%%a"
)
Set regdateold=%regdateold:~-8%
REG ADD "Hkey_current_user/control panel/international"/V sshortdate/t reg_sz/d Yyyy-m-d/f>nul
> "%temp%/dstdate.vbs" Echo lastdate=date ()-%daysago%
>> "%temp%/dstdate.vbs" Echo fmtdate=right (Lastdate), 4 ^& Right ("0" ^& month (lastdate), 2) ^& Right ("0" ^& Day (Lastdate), 2)
>> "%temp%/dstdate.vbs" Echo WScript.Echo fmtdate
for/f%%a in (' Cscript/nologo '%temp%/dstdate.vbs "') Do (
Set "Dstdate=%%a"
)
Set dstdate=%dstdate:~0,4%-%dstdate:~4,2%-%dstdate:~6,2%
FOR/D%%a in ("%srcdir%/*.*") Do (
If "%%~ta" Leq "%dstdate%" (
If exist "%%a/"
echo rd/s/q "%%a"
)
)
)
REG ADD "Hkey_current_user/control panel/international"/V sshortdate/t reg_sz/d%regdateold%/f>nul
Pause

"Scheme two" BAT + REG + Ritchie Lawrence Date function

Copy Code code as follows:

@echo off
:: Demo: Deletes a folder that precedes the specified number of days in the specified path, whichever is the last modified date of the folder.
:: If the demo results are correct, remove the echo from the front of Rd to achieve the real deletion.
:: This example calls the date function of Ritchie Lawrence for date calculation
:: The core algorithm of date conversion please refer to http://bbs.bathome.cn/thread-3056-1-1.html
:: This example is compatible with different date formats, call the REG command (XP system from the band) to set the date format uniformly,
:: After processing, then the date format back to the original state.

REM Specifies the location of the folder to be deleted
Set Srcdir=c:/test
REM Specify the number of days
Set Daysago=1
for/f "skip=2 delims="%%a in (' reg query ' Hkey_current_user/control panel/international '/V sshortdate ') do (
Set "Regdateold=%%a"
)
Set regdateold=%regdateold:~-8%
REG ADD "Hkey_current_user/control panel/international"/V sshortdate/t reg_sz/d Yyyy-m-d/f>nul
Call:D atetodays%date:~0,4%%date:~5,2%%date:~8,2% passdays
REG ADD "Hkey_current_user/control panel/international"/V sshortdate/t reg_sz/d%regdateold%/f>nul
set/a passdays-=%daysago%
Call:D aystodate%passdays% dstyear dstmonth dstday
Set dstdate=%dstyear%%dstmonth%%dstday%
Set dstdate=%dstdate:~0,4%-%dstdate:~4,2%-%dstdate:~6,2%
FOR/D%%a in ("%srcdir%/*.*") Do (
If "%%~ta" Leq "%dstdate%" (
If exist "%%a/"
echo rd/s/q "%%a"
)
)
)
REG ADD "Hkey_current_user/control panel/international"/V sshortdate/t reg_sz/d%regdateold%/f>nul
Pause
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%)
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

"Scheme three" BAT + VBS

Copy Code code as follows:

@echo off
:: Demo: Deletes a folder that precedes the specified number of days in the specified path, whichever is the last modified date of the folder.
:: If the demo results are correct, remove the echo from the front of Rd to achieve the real deletion.
:: This example calls the temporary VBS code for the date calculation, and sets the system date format uniformly, processing is complete
:: The date format is then restored to its original state. Get rid of dependencies on the reg command (XP system itself).

REM Specifies the location of the folder to be deleted
Set Srcdir=c:/test
REM Specify the number of days
Set Daysago=1
> "%temp%/backupdate.vbs" echo Set WshShell = WScript.CreateObject ("Wscript.Shell")
>> "%temp%/backupdate.vbs" Echo WScript.Echo WshShell.RegRead ("Hkey_current_user/control panel/international/ Sshortdate ")
for/f%%a in (' Cscript/nologo '%temp%/backupdate.vbs "') Do (
Set "Regdateold=%%a"
)
> "%temp%/unifydate.vbs" echo Set WshShell = WScript.CreateObject ("Wscript.Shell")
>> "%temp%/unifydate.vbs" Echo WshShell.RegWrite "Hkey_current_user/control panel/international/sshortdate", " Yyyy-m-d "," REG_SZ "
Cscript/nologo "%temp%/unifydate.vbs"
> "%temp%/dstdate.vbs" Echo lastdate=date ()-%daysago%
>> "%temp%/dstdate.vbs" Echo fmtdate=right (Lastdate), 4 ^& Right ("0" ^& month (lastdate), 2) ^& Right ("0" ^& Day (Lastdate), 2)
>> "%temp%/dstdate.vbs" Echo WScript.Echo fmtdate
for/f%%a in (' Cscript/nologo '%temp%/dstdate.vbs "') Do (
Set "Dstdate=%%a"
)
Set dstdate=%dstdate:~0,4%-%dstdate:~4,2%-%dstdate:~6,2%
FOR/D%%a in ("%srcdir%/*.*") Do (
If "%%~ta" Leq "%dstdate%" (
If exist "%%a/"
echo rd/s/q "%%a"
)
)
)
> "%temp%/recoverdate.vbs" echo Set WshShell = WScript.CreateObject ("Wscript.Shell")
>> "%temp%/recoverdate.vbs" Echo WshShell.RegWrite "Hkey_current_user/control panel/international/sshortdate" , "%regdateold%", "REG_SZ"
Cscript/nologo "%temp%/recoverdate.vbs"
Pause

"Scheme four" string interception

Copy Code code as follows:

@echo off
:: Demo: Deletes a folder that precedes a specified number of days in a specified path, whichever is the date string contained in the folder name.
:: If the demo results are correct, remove the echo from the front of Rd to achieve the real deletion.
:: This example assumes a date string contained in a folder name (for example, archive-2009-12-25)

REM Specifies the location of the folder to be deleted
Set Srcdir=c:/test
REM Specify the number of days
Set Daysago=1

> "%temp%/dstdate.vbs" Echo lastdate=date ()-%daysago%
>> "%temp%/dstdate.vbs" Echo fmtdate=right (Lastdate), 4 ^& Right ("0" ^& month (lastdate), 2) ^& Right ("0" ^& Day (Lastdate), 2)
>> "%temp%/dstdate.vbs" Echo WScript.Echo fmtdate
for/f%%a in (' Cscript/nologo '%temp%/dstdate.vbs "') Do (
Set "Dstdate=%%a"
)
Set dstdate=%dstdate:~0,4%-%dstdate:~4,2%-%dstdate:~6,2%

Setlocal enabledelayedexpansion
FOR/D%%a in ("%srcdir%/*.*") Do (
If "%%~ta" Leq "%dstdate%" (
Set "Folderdate=%%~na"
Set "folderdate=! folderdate:~8,10! "
If "! folderdate! "Leq"%dstdate% "(
If exist "%%a/" (
echo rd/s/q "%%a"
)
)
)
)
Endlocal
Pause

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.