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:
path_3=D:\Scripts\DeleteLogsScripts\test,3
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!
)
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.