0

In my hard disk I have the following structure:

ROOTFOLDER   
├───FOLDER1
│   └───TMPFOLDER
│       ├───FOLDERTODELETE1
│       ├───FOLDERTODELETE2
│       └───FOLDERTODELETE3
├───FOLDER2
│   └───TMPFOLDER
│       ├───FOLDERTODELETE4
│       └───FOLDERTODELETE5
└───FOLDER3
    └───TMPFOLDER
        ├───FOLDERTODELETE6
        ├───FOLDERTODELETE7
        └───FOLDERTODELETE8

I need to create a kind of script (I was thinking about a batch file, but any other solution will be appreciated) to delete all folders within each folders with a specific name (in this case TMPFOLDER) and created before today.

Andre Kampling
  • 5,476
  • 2
  • 20
  • 47
Fab
  • 4,526
  • 2
  • 21
  • 45

2 Answers2

1

Following to your needs the batch script below will do what you need. Remove the echo in front of the rmdir command if it's okay for you. Take care that the script uses the current working directory. Remove /q if you want to be asked for each directory that should be deleted:

@echo off

set "tmpfolder=TMPFOLDER"

for /f "tokens=*" %%D in ('dir /b /a:d "*"') do (
   for /f "tokens=*" %%E in ('dir /b /a:d "%%D\*" ^| findstr /l /x %tmpfolder%') do (
      echo Found temp folder: "%%D\%%E"
      for /f "tokens=*" %%F in ('dir /b /a:d "%%D\%%E\*"') do (
         echo rmdir /s /q "%%D\%%E\%%F"
      )
   )
)

Edit #1:

The above script does not take care of the creation date of the folder that should be removed. You want only folders to be deleted which have a creation date before today. The following script takes care of that. Look here how to get dates of folder and here how to compare dates:

@echo off
setlocal EnableDelayedExpansion

set "tmpfolder=TMPFOLDER"

for /f "tokens=*" %%D in ('dir /b /a:d "*"') do (
   for /f "tokens=*" %%E in ('dir /b /a:d "%%D\*" ^| findstr /x /c:"%tmpfolder%"') do (
      rem echo Found temp folder: "%%D\%%E"
      for /f "tokens=*" %%F in ('dir /b /a:d "%%D\%%E\*"') do (
         rem echo    Subfolder: "%%F"
         set "curdate=!date!"
         set "dirdate="
         echo GETDATE
         call :getdate dirdate "%%D\%%E\%%F"
         set "dirdate=!dirdate:~-4!!dirdate:~3,2!!dirdate:~0,2!"
         set "curdate=!curdate:~-4!!curdate:~3,2!!curdate:~0,2!"

         rem echo    dirdate: "!dirdate!"
         rem echo    curdate: "!curdate!"
         rem echo.

         if [!dirdate!] LSS [!curdate!] (
            echo rmdir /s /q "%%D\%%E\%%F"
         )
      )
   )
)
goto :EOF

:getdate
rem call: getdate date(dstParam) folder(srcParam)
for /f "skip=5 tokens=1 delims= " %%A in ('dir /a:d /o:d /t:c "%~2"') do (
   set "%~1=%%A"
   goto :EOF
)

Command reference links from ss64.com:

Andre Kampling
  • 5,476
  • 2
  • 20
  • 47
  • I extended my answer with a second variant of the script posted before. That version will take care of the creation date. Test it and report. ;) – Andre Kampling Sep 06 '17 at 09:36
  • Thanks a lot, It works great! Only one question: this batch works only if TMPFOLDER is on the 2nd level of the structure? – Fab Sep 06 '17 at 09:49
  • 1
    Yes because `findstr /x /c:"%tmpfolder%"` is involved in the 2nd `for /f` loop. Adapt the script as you need it. – Andre Kampling Sep 06 '17 at 09:50
  • I've a problem with `dirdate` value: it always return a blank value from `getdate` function. Within the function the value is correct (I checked it using a log). Could be the problem on the `set` command within the function? – Fab Sep 07 '17 at 14:19
  • @Fabio: I corrected the batch script and edited my answer. I was missing `EnableDelayedExpansion`. Now it will work without any problems: You can output the dates (if you want to see them) by removing the `rem` in front of the `echo dirdate: ...` etc. I added the `EnableDelayedExpansion` to the command reference. – Andre Kampling Sep 08 '17 at 06:55
1

Although an answer has already been accepted, I have decided to post this untested idea as an alternative:

@Echo Off
SetLocal EnableDelayedExpansion

For /F "Delims=" %%A In ('WMIC OS Get LocalDateTime') Do For %%B In (%%~nA
) Do Set "TD=%%B" & Set "TD=!TD:~,8!"

For /D /R "ROOTFOLDER" %%A In ("TMPFOLD?R") Do If /I "%%~nxA"=="TMPFOLDER" (
    Set "FP=%%~pA" & Set "FP=!FP:\=\\!"
    WMIC FSDir Where "Path='!FP!' And FileName='TMPFOLDER'" Get CreationDate^
     |FindStr/B "%TD%">Nul||Echo=RD/S/Q "%%A")
Timeout -1

You may need to change ROOTFOLDER and all instances of TMPFOLDER as necessary, remembering to use a ? to replace one of the characters in the first instance of TMPFOLDER.

If the output appears to be correct, remove the last line and Echo= from the line above it.

Compo
  • 36,585
  • 5
  • 27
  • 39