Batch delete folders named with dates 90 days ago.bat

d:
cd D:\MsgStorageServer\Bulk
@echo off
:: Find the date N days ago
:: Ideas:
:: Simulate manual operations, subtract the number of days from the number of dates, and borrow 1 from the high bit when the difference is negative.
:: Features:
:: The code is concise and easy to understand, and the speed depends on the specified number of days

setlocal enabledelayedexpansion

:Main
cls
:: The date can have the week, but it must be arranged in the order of year, month and day
:: The year must be four digits, otherwise an error will occur
set day=%date%
set days=0
echo.&echo.
echo The specified date is :%day%
echo.
::set /p input= Please specify the number of days to go back:
set input=90

:: 提取日期
for /f "tokens=1-3 delims=-/. " %%i in ("%day%") do (
    set /a sy=%%i, sm=100%%j %% 100, sd=100%%k %% 100
)

set /a sd-=input

if %sd% leq 0 call :count

cls
echo.&echo.
echo The specified date is: %day%
echo.
set sm=0%sm%
set sd=0%sd%
echo %input% The date before the day is: %sy%-%sm:~-2 %-%sd:~-2%

echo delete folder %sy%%sm:~-2%%sd:~-2%
del /s /q %sy%%sm:~-2%%sd:~-2%
rd /s /q %sy %%sm:~-2%%sd:~-2%
::pause>nul

:count
set /a sm-=1
if !sm! equ 0 set /a sm=12, sy-=1
call :days
set /a sd+=days
if %sd% leq 0 goto count
goto :eof

:days
:: Get the total number of days in the specified month
set /a leap="^!(sy %% 4) & ^!(^!(sy %% 100)) | ^!(sy %% 400)"
set /a max=28+leap
set num=0
set str=31 %max% 31 30 31 30 31 31 30 31 30 31
for %%i in (%str%) do (
    set /a num+=1
    if %sm% equ ! num!set days=%%i&goto :eof
)
goto :eof

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326558667&siteId=291194637