Link to home
Start Free TrialLog in
Avatar of markterry
markterry

asked on

Delete Archive's Batch

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.
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CatsAndJammer
CatsAndJammer

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

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of markterry

ASKER

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"
  )
)
Is that because I would need directories older than 7 days there to make it show anything?
Thank you very much for your help. This makes sense.
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