Last active
November 23, 2023 17:47
Delete All Files In Downloads Folder After A week
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
setlocal | |
REM Set the path to the Downloads folder | |
set "download_folder=C:\Users\[UserName]\Downloads" | |
echo Target folder is: %download_folder% | |
REM Check if the downloads directory exists | |
if not exist "%download_folder%" ( | |
echo The specified folder does not exist please input your username: %download_folder% | |
goto end | |
) | |
REM Calculate the date one week ago from today | |
for /f "usebackq tokens=1-3 delims=/ " %%a in (`echo %date%`) do ( | |
set /a "year=%%c" | |
set /a "month=%%a" | |
set /a "day=%%b" | |
) | |
set /a "day-=7" | |
if %day% lss 1 ( | |
set /a "month-=1" | |
if %month% lss 1 ( | |
set /a "year-=1" | |
set "month=12" | |
) | |
set /a "day+=31" | |
) | |
set "week_ago_date=%year%-%month%-%day%" | |
REM List and delete files modified more than a week ago | |
echo Deleting files modified more than a week ago... | |
for /r "%download_folder%" %%i in (*) do ( | |
for /f "tokens=1,2,3 delims= " %%a in ('dir "%%i" ^| find "%%~nxi"') do ( | |
set "file_date=%%c" | |
set "file_time=%%b" | |
set "file_datetime=%%a %%b %%c" | |
REM Check if the file_datetime is older than a week ago | |
for /f %%d in ('wmic os get LocalDateTime ^| find "."') do set "current_datetime=%%d" | |
if "!file_datetime!" lss "%week_ago_date%" ( | |
echo Deleting: %%i (Last Modified Date: %%c %%b) | |
del "%%i" | |
) | |
) | |
) | |
REM Check if the folder is empty and delete it if so | |
for /r "%download_folder%" %%d in (.) do ( | |
dir /b "%%d" | findstr "^" >nul || ( | |
echo Deleting empty folder: "%%d" | |
rd "%%d" | |
) | |
) | |
:end | |
pause |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment