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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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\shar epoint
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"
)
)
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
setlocal EnableDelayedExpansion
REM Define directory to clean, and number of folders to keep (newest)
set BaseDir=\\host\backup\shar
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\
)
)
ASKER
Is that because I would need directories older than 7 days there to make it show anything?
ASKER
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
~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"
)
)
~~CJ
Open in new window