Automatically deleting oldest directories

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ahwinnen
Posts: 4
Joined: 07 Oct 2013 07:17

Automatically deleting oldest directories

#1 Post by ahwinnen » 07 Oct 2013 07:25

For quite a while I've been using a batch file to delete old backup files. I've normally used forfiles to find all files older than 5 days. The code usually looks something like this:

@echo off
forfiles /p "G:\VERITAS\B2D" /m "*.bkf" /d -5 /c "cmd /c del @path"

Now, I have a client that has all of his backup files stored in multiple folders all in the same directory. Is there a way to delete directories older than 5 days the same way I was deleting files. Basically is there an equivalent command that does the same thing as "forfiles" but for directories?

Thank you in advance!

ShadowThief
Expert
Posts: 1160
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Automatically deleting oldest directories

#2 Post by ShadowThief » 07 Oct 2013 10:10

instead of del @path you can use rd @path

ahwinnen
Posts: 4
Joined: 07 Oct 2013 07:17

Re: Automatically deleting oldest directories

#3 Post by ahwinnen » 07 Oct 2013 12:08

Oh yeah, I didn't think of that. I'll try it, thanks.

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Automatically deleting oldest directories

#4 Post by Endoro » 07 Oct 2013 12:36

try this (make a backup before):

Code: Select all

@echo off
forfiles /p "G:\VERITAS\B2D"  /d -5 /c "cmd /c if @isdir==TRUE rd /s /q @path"

Also look at "forfiles /?" at the command prompt.

ShadowThief
Expert
Posts: 1160
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Automatically deleting oldest directories

#5 Post by ShadowThief » 07 Oct 2013 12:50

Endoro wrote:try this (make a backup before):

Code: Select all

@echo off
forfiles /p "G:\VERITAS\B2D"  /d -5 /c "cmd /c if @isdir==TRUE rd /s /q @path"

Also look at "forfiles /?" at the command prompt.

the "if @isdir==TRUE" is unnecessary because RD only deletes directories

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Automatically deleting oldest directories

#6 Post by foxidrive » 07 Oct 2013 16:52

ShadowThief wrote:the "if @isdir==TRUE" is unnecessary because RD only deletes directories


You'll get an error for every file though.

ShadowThief
Expert
Posts: 1160
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Automatically deleting oldest directories

#7 Post by ShadowThief » 07 Oct 2013 17:04

foxidrive wrote:
ShadowThief wrote:the "if @isdir==TRUE" is unnecessary because RD only deletes directories


You'll get an error for every file though.

But it won't affect anything. If it really bothers you, you can throw a 2>nul at the end

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Automatically deleting oldest directories

#8 Post by foxidrive » 07 Oct 2013 17:44

ShadowThief wrote:
foxidrive wrote:
ShadowThief wrote:the "if @isdir==TRUE" is unnecessary because RD only deletes directories


You'll get an error for every file though.

But it won't affect anything. If it really bothers you, you can throw a 2>nul at the end


You could, but I consider that 2>nul in this particular case is sloppy scripting.

In the future when you read the code "if @isdir==TRUE" it will immediately tell you that you are processing folders.

ahwinnen
Posts: 4
Joined: 07 Oct 2013 07:17

Re: Automatically deleting oldest directories

#9 Post by ahwinnen » 08 Oct 2013 08:55

Code: Select all

@echo off
forfiles /p "G:\VERITAS\B2D"  /d -5 /c "cmd /c if @isdir==TRUE rd /s /q @path"


^^ This will delete folders inside the B2D directory, not the directory itself, correct? I'll do a backup and test it when I get to work, just wanted to make sure.

And I don't mind throwing a 2>nul in at the end if that's the only way to get rid of those errors. This is something I'm hoping to automate for a lot of clients eventually so I would really like it to run without any extra user input.

Thanks guys!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Automatically deleting oldest directories

#10 Post by foxidrive » 08 Oct 2013 18:06

ahwinnen wrote:

Code: Select all

@echo off
forfiles /p "G:\VERITAS\B2D"  /d -5 /c "cmd /c if @isdir==TRUE rd /s /q @path"


^^ This will delete folders inside the B2D directory, not the directory itself, correct?


Yup.

ahwinnen
Posts: 4
Joined: 07 Oct 2013 07:17

Re: Automatically deleting oldest directories

#11 Post by ahwinnen » 09 Oct 2013 12:28

What if there is one directory that I want it to skip. Is there a way to exclude a directory before the "rd /s /q @path" ?

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Automatically deleting oldest directories

#12 Post by Endoro » 09 Oct 2013 18:55

yes, just put 'forfiles' in a 'for /f' loop and send the output through 'findstr'.

ShadowThief
Expert
Posts: 1160
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Automatically deleting oldest directories

#13 Post by ShadowThief » 09 Oct 2013 20:57

An easier way would be that if it's only one folder, you can say

Code: Select all

cmd /c if not @path==C:\path\you\want\to\ignore rd /s /q @path

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Automatically deleting oldest directories

#14 Post by Endoro » 10 Oct 2013 20:55

'if not' works only for very few folders-to-ignore.
If you have a lot of it better take 'findstr'.

Post Reply