简体   繁体   中英

Deleting files and folder older than 3 days via batch file

I am trying to write a batch script to delete files and folders that have data older than 3 days. But there are files inside the folder and once the files are deleted, the folder's last modified data changes to today which is causing the folder not to get deleted.

My script has 3 different files:

1. Property file that consists of paths and time

path_3=D:\Scripts\DeleteLogsScripts\test,3

2. Batch file to read the path and time and send them as variables to another batch file

setlocal enabledelayedexpansion
for /F "tokens=1,2,3 delims==," %%G IN (data.properties) DO (
@echo %%G %%H %%I
set local_path=%%H
set local_time=%%I
call backup_filecheck1.bat !local_path! !local_time!
)

3. Batch file to delete files

set local_path=%1
echo %local_path%
set local_time=%2
Forfiles /P "%local_path%" /S /D -%local_time% /C "Cmd /C If @isdir==FALSE Del @Path"
Forfiles /p "%local_path%" /S /C "Cmd /C If @isdir==TRUE RD /s /q @Path 2>Nul"

The folder it is deleting from:

原始文件夹结构

删除文件后

As you can see, the folder's last modified date changes to today.

I am not understanding how to tackle this.

I did finally figure out how to do it. I updated the script in such a way that after deleting the files, it'll sort files and delete empty folders

set local_path=%1 echo %local_path% set local_time=%2 Forfiles /P "%local_path%" /S /D -%local_time% /C "Cmd /C If @isdir==FALSE Del @Path" cd /D %local_path%\\%%a for /f "delims=" %%i in ('dir /s /b /ad ^| sort /r') do rd "%%i">NUL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM