Search code examples
batch-filecmdforfiles

Get list of empty folders older then x days


I need to get a list of empty folders in a given location, older then x days. Using "forfiles" I'm able to get all older folders, but not the empty ones. Using "for" I'm able to get all empty folders, but I can't seem to set the older ones.

Get empty folders:

@for /r "c:\FileStore" /d %F in (.) do @(dir /b "%F" | findstr "^" >nul || echo %~fF)

Get older folders:

 ForFiles /p "C:\FileStore" /s /d -3 /c "cmd /c if @isdir==TRUE echo @path"

How do I combine these 2 commands?


Solution

  • You need to escape the quotation marks of the part findstr "^" for forfiles. There is the way \", but I do not recommend this, because the " are still recognised by the command interpreter cmd (user Ben Personick shows how to do this in his answer though). Anyway, I would use 0x22 instead in order to hide the quotes from cmd, like this:

    forfiles /S /P "C:\FileStore" /D -3 /C "cmd /C if @ISDIR == TRUE (dir /B /A @PATH | findstr 0x22^0x22 > nul || echo @PATH)"
    

    Instead of findstr "^" you could also use find /V "":

    forfiles /S /P "C:\FileStore" /D -3 /C "cmd /C if @ISDIR == TRUE (dir /B /A @PATH | find /V 0x220x22 > nul || echo @PATH)"
    

    But the easiest way is to use set /P:

    forfiles /S /P "C:\FileStore" /D -3 /C "cmd /C if @ISDIR == TRUE (dir /B /A @PATH | set /P _= || echo @PATH)"
    

    N. B.:
    forfiles only regards the date (not the time) of the last modification, but not the creation date/time.