Automatically deleting oldest directories
Moderator: DosItHelp
Automatically deleting oldest directories
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!
@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!
-
- Expert
- Posts: 1160
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
Re: Automatically deleting oldest directories
instead of del @path you can use rd @path
Re: Automatically deleting oldest directories
Oh yeah, I didn't think of that. I'll try it, thanks.
Re: Automatically deleting oldest directories
try this (make a backup before):
Also look at "forfiles /?" at the command prompt.
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.
-
- Expert
- Posts: 1160
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
Re: Automatically deleting oldest directories
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
Re: Automatically deleting oldest directories
ShadowThief wrote:the "if @isdir==TRUE" is unnecessary because RD only deletes directories
You'll get an error for every file though.
-
- Expert
- Posts: 1160
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
Re: Automatically deleting oldest directories
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
Re: Automatically deleting oldest directories
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.
Re: Automatically deleting oldest directories
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!
Re: Automatically deleting oldest directories
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.
Re: Automatically deleting oldest directories
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" ?
Re: Automatically deleting oldest directories
yes, just put 'forfiles' in a 'for /f' loop and send the output through 'findstr'.
-
- Expert
- Posts: 1160
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
Re: Automatically deleting oldest directories
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
Re: Automatically deleting oldest directories
'if not' works only for very few folders-to-ignore.
If you have a lot of it better take 'findstr'.
If you have a lot of it better take 'findstr'.