Delete privious month backup folder

Hewlett-packard / Compaq nc8230
August 12, 2009 at 10:43:40
Specs: Windows XP

I have backup folder
Backup_2009_15_07 ,Backup_2009_16_07
and Backup_2009_10_08. I want to know batch file commands which will deletes all previous month back up folders like Backup_2009_15_07 ,Backup_2009_16_07 except Backup_2009_10_08 from folder C:\Batch\ARCHIVE\FINAL

Please let me know the code

See More: Delete privious month backup folder

Report •

August 12, 2009 at 12:25:58

In order to help you we need to know your date format in order to idenify the current month.
Type this at the Dos prompt and post it back.

echo %Date%

Report •

August 12, 2009 at 13:23:01
The current date is: Wed 08/12/2009
Enter the new date: (mm-dd-yy)

I am creating backup folder by folloing code
set Day=%Date:~7,2%
set Mth=%Date:~4,2%
set Yr=%Date:~10,4%


Report •

August 12, 2009 at 13:25:02
Not to throw water on your fire but if you only have a few why bother? Just manually delete. Do you verify your backups?

Report •

Related Solutions

August 12, 2009 at 13:28:43
The above is example with few folders. I am going to have lots of folders . Just i want code which deletes previous month folder and keep this month under C:\Batch\ARCHIVE\FINAL.

Report •

August 12, 2009 at 14:34:19
Hi Hrshelke

Delete the /s if you don't want confirmation, but it's best to leave it for testing.
I take it that there only last months backup files in the %Dir% directory.

@echo off
set Mth=%Date:~4,2%
for /f "tokens=1-4 delims=_" %%a in ('dir /b /ad %Dir%') do (
if %%d LSS %Mth% (rd /s %Dir%\%%a_%%b_%%c_%%d)

Report •

August 12, 2009 at 14:48:55
Thanks dtech10.

The code is working really fine.

Report •

August 13, 2009 at 07:57:03
Hi Hrshelke

Forgot to cater for year end.

@echo off
set Mth=%Date:~3,2%
for /f "tokens=1-4 delims=_" %%a in ('dir /b /ad %Dir%') do (
if %Mth% EQU 01 (if %%d EQU 12 (rd /s %Dir%\%%a_%%b_%%c_12))
if %%d LSS %Mth% (rd /s %Dir%\%%a_%%b_%%c_%%d)

Report •

Ask Question