0

I need a batch file to delete folders old of 1 day.

The folders contains inside files that will be removed together with the folders

I tryed 3 different code but do not remove the old folders.

Path where are located folders to delete (path have space):

D:\Programmi Installati\

Example folder names (start with log_)

log_1
log_10-12-2019
log_2008-10000
log_222222211111

Days old: 1

:: Code 1

@echo off
setlocal

set target="D:\Programmi Installati\"
set days=1

for /f "usebackq delims=" %%G in (
'forfiles /p "%target%" /c "cmd /c if /i @isdir == true echo @path" /d -%days% 2^>nul'
) do rd /s /q "%%~G"

pause
endlocal & exit /b


:: Code 2

forfiles /p "D:\Programmi Installati\" /d -1 /c "cmd /c if @isdir==true rd /s /q @path"



:: Code 3

FORFILES /P "D:\Programmi Installati\" /S /C "cmd /c IF @isdir == TRUE rmdir /S @path /Q" -D -1

I'm interested a fix of code already indicate or a new code that works.

placidomaio
  • 111
  • 1
  • 13
  • You need to better explain your task. Are you wishing to keep only directories which have existed for one day or less? Or are you looking to remove any directory which does not hold a file which has been created in the previous 24 hours? Or something else?...Also, you should explain the environment in which each of the 3 codes was run and exactly what happened in each case. – Compo Jan 13 '19 at 20:53
  • I'm interested to delete the folders created for more than 24 hours and I need to delete the old folders with the files inside (need to check the date creation of folder only). I need to use this code to remove the folder with old log. I tryed the code indicated but do not remove any folders, i have folder old of 3-4 days ago and they have not been deleted from the execution of the bat. – placidomaio Jan 13 '19 at 21:02

1 Answers1

1

It seems that you want to check all log directories starting with log_ only and delete them if they are older than a day, or at least have a date of yesterday, if so, I believe that this is what you want to to:

forfiles /P "D:\Programmi Installati" /M "log_*" /D -1 /C "cmd /c if @isdir==TRUE echo rd /s /q @path"

Note! This will only echo the command rd /s /q path in order for you to verify it does what you intended it to. Once you are satisfied, remove echo from the end of the line in echo rd /s /q @path

Gerhard
  • 22,678
  • 7
  • 27
  • 43