user10053673
user10053673

Reputation: 294

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?

Upvotes: 0

Views: 493

Answers (2)

aschipfl
aschipfl

Reputation: 34899

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.

Upvotes: 1

Ben Personick
Ben Personick

Reputation: 3264

Your biggest hurdle is escaping the double quotes in the FindStr and the Carrot needing doubling too (or it will stop the following quote from being escaped.)

Hmm strange I thought you asked to delete these directories, since you haven't I'll amend it as such.

ForFiles /P "c:\FileStore" /d -3 /C "CMD /C if @isdir==TRUE ( DIR /B @Path | FindStr \"^^\" >NUL || ECHO Empty Folder: @Path )"

Also since you are only looking for a list of those it does make sense to kill the output from the FindStr so I added the >Nul back in.

Again not sure how I ot it into my head that you wanted to remove the empty folders older than 3 days old, since there isn't such a requirement, the portion about needing to re-run the command is moot and I've remove dit for now.

Upvotes: 1

Related Questions