We help IT Professionals succeed at work.

Delete Archive's Batch

markterry
markterry asked
on
Hello,

I am currently backing up my Windows Sharepoint Services environment with the stsadmin and -directory flag which creates a backup folder for each backup inside my network drive \\host\backup\sharepoint. Using this method is good for archiving, but it takes up considerable space over time.

Is there a way in a batch file or powershell script to have it delete all but the latest 7 folders? I would like to be able to schedule this every day so once a new backup exists it chops off the oldest one.
Comment
Watch Question

Test your restores, not your backups...
Expert of the Year 2019
Top Expert 2016
Commented:
How about a little BAT script like this.  It should look at the subfolders in reverse date order (newest first) and when it finds more than the defined limit remove them.

Notice that I have an ECHO in front of the RD command right now.  This is for testing purposes.  When you run this script it won't actually delete any folders, just echo the command that it would have executed to the screen so you can make sure it's finding the right ones and number, etc.

Once you are satisfied it works properly remove that ECHO word and the RD will actually execute, deleting the extra folders.

~bp
@echo off
setlocal EnableDelayedExpansion
 
REM Define directory to clean, and number of folders to keep (newest)
set BaseDir=\\host\backup\sharepoint
set KeepLimit=7
 
REM Loop through subfolders, newest first, when more than limit remove them
set Count=0
for /F "tokens=*" %%A in ('dir /ad /b /o-d "%BaseDir%"') do (
  set /A Count+=1
  if !Count! GTR > %KeepLimit% (
    ECHO rd /Q /S "%BaseDir%\%%A"
  )
)

Open in new window

How about...

~~CJ
for /F "usebackq skip=7 delims=" %%P in (`dir /A:D /B /O:-D "\\host\backup\sharepoint"`) do @rd /Q /S "%%~P"

Open in new window

Correction...

The /B obtion of the DIR command removes the fully qualifed path name on the output even when specified on the command line. :-(  So you need to supply the path name on the RD command.

~~CJ

@echo off
setlocal ENABLEDELAYEDEXPANSION ENABLEEXTENSIONS
set _DIR=\\host\backup\sharepoint
for /F "usebackq skip=7 delims=" %%P in (`dir /A:D /B /O:-D "%_DIR%"`) do @rd /Q /S "%_DIR%\%%~P"
endlocal

Open in new window

Author

Commented:
Bill,

Thank you for your solution, it looks like it should work but unfortunately I do not know enough about advanced batch scripting to fix it, it gives me the following when ran from CMD:

Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\Administrator.PRA>@echo off
setlocal EnableDelayedExpansion

REM Define directory to clean, and number of folders to keep (newest)
set BaseDir=\\host\backup\sharepoint
set KeepLimit=7

REM Loop through subfolders, newest first, when more than limit remove them
set Count=0
for /F "tokens=*" %%A in ('dir /ad /b /o-d "%BaseDir%"') do (
%%A was unexpected at this time.
  set /A Count+=1
1  if !Count! GTR > %KeepLimit% (
> was unexpected at this time.
    ECHO rd /Q /S "%BaseDir%\%%A"
rd /Q /S "\\host\backup\sharepoint\%%A"
  )
)

Author

Commented:
Is that because I would need directories older than 7 days there to make it show anything?

Author

Commented:
Thank you very much for your help. This makes sense.
Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:
Oops,looks like an extra character snuck in to that line, must have fat fingered it. Here's a corrected version.

~bp
@echo off
setlocal EnableDelayedExpansion
 
REM Define directory to clean, and number of folders to keep (newest)
set BaseDir=\\host\backup\sharepoint
set KeepLimit=7
 
REM Loop through subfolders, newest first, when more than limit remove them
set Count=0
for /F "tokens=*" %%A in ('dir /ad /b /o-d "%BaseDir%"') do (
  set /A Count+=1
  if !Count! GTR %KeepLimit% (
    ECHO rd /Q /S "%BaseDir%\%%A"
  )
)

Open in new window