delete 2 days older folders

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Vikky_Vik
Posts: 7
Joined: 27 Dec 2011 03:26

delete 2 days older folders

#1 Post by Vikky_Vik » 27 Dec 2011 05:00

My Requirement is-
I want to delete 2 days old folders and only those folder which are of format “month-date” i.e. “12-27” for today(27th Dec).
I want to also keep other folders which are not of date format. E.g. XYZ, ABC, etc..

Below is my script by which I can delete 2 days old folders, but it is not satisfying my other requirement. It is deleting other folders also.

------------------------------------------
C:
cd c:\backup

SET dates=%date:~7,2%
SET /A day =%dates%
SET /A month=%date:~4,2%
SET today=%month%-%day%

SET /A day =%dates% - 1
SET yesterday=%month%-%day%

for /d %%i in (*) do (

IF /I %%i equ %today% (
echo "Today"
) ELSE IF /I %%i equ %yesterday% (
echo "Yesterday"
) ELSE (
rmdir /q /s "%%i"
)
)
------------------------------------------

For example:-
If my folders are as below-
12-27
12-26
12-25
12-24
12-23
12-22
XYZ
ABC

So it should keep below folders-
12-27
12-26
XYZ
ABC

All other folders should be deleted.


Thanks a lot for giving your precious time.

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: delete 2 days older folders

#2 Post by Squashman » 27 Dec 2011 06:37

A batch file using the ForFiles command in combination with the findstr command may work better but ForFiles is not native to Windows XP.

Going to throw my idea out there but I am sure other suggestions may come along that will work better as my script may have some caveats because I don't know how you are naming your folders for single digit months and days. Examples January 1st. Do you name it 01-01 or do you name it 1-1?

Code: Select all

for /F "skip=2 tokens=*" %%I in ('dir /ad /b /O-D ^| findstr /R /B [0-9][0-9]-[0-9][0-9]') do del "%%I"

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: delete 2 days older folders

#3 Post by Squashman » 27 Dec 2011 06:48

I should note that technically my script does not delete folders older than two days, it just keeps the two newest folders that match your date pattern. If you create a new folder every day for some type of backup this will essentially keep the two newest folders.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: delete 2 days older folders

#4 Post by aGerman » 27 Dec 2011 06:50

Vikky_Vik wrote:Below is my script by which I can delete 2 days old folders

No, unfortunately this doesn't work. Imagine today is January 1st. In this case yesterday was December 31th. As you can see you can't simply subtract 1 to get yesterday.

Before I provide a possible solution there are 2 things I need to know:
- What format has the value in %date%? I need to know the order, the separator and the number of digits of day, month and year (1 or 2 digits in case of January, 2 or 4 digits for the year).
- (as Squashman asked before) How would the folder name look like in case of January 1st? 01-01 or 1-1?

Regards
aGerman

Vikky_Vik
Posts: 7
Joined: 27 Dec 2011 03:26

Re: delete 2 days older folders

#5 Post by Vikky_Vik » 27 Dec 2011 10:51

Thanks a lot for replying,

-2 digit in case of jan, and 4 digit for year. Date format is - Tue 12/27/2011
-For 1st January, folder name will be 01-01 .

regards,

Vikky_Vik
Posts: 7
Joined: 27 Dec 2011 03:26

Re: delete 2 days older folders

#6 Post by Vikky_Vik » 27 Dec 2011 10:55

Thanks a lot for replying..

@ squashman-
For 1st January, folder name will be 01-01 .

And squashman, that is what my script also doing, it is also keeping the two latest folders. But i want to keep other folders also which are not of month-date format.

Regards,

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: delete 2 days older folders

#7 Post by Squashman » 27 Dec 2011 11:23

Vikky_Vik wrote:And squashman, that is what my script also doing, it is also keeping the two latest folders. But i want to keep other folders also which are not of month-date format.

Regards,

My script does not touch your XYZ or ABC folders. I pipe the output of the DIR command to the FINDSTR command. The findstr command then looks for your DATE pattern. The folder name has to start with ##-##.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: delete 2 days older folders

#8 Post by aGerman » 27 Dec 2011 11:40

@Vikky_Vik

Your script doesn't work if the month/year is changing, it also wont work if the day is less than 12. Even worse your equation will totally fail if the day is 08 or 09. Please answer my first question if you want to get a working script.

Regards
aGerman

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: delete 2 days older folders

#9 Post by Squashman » 27 Dec 2011 11:57

Squashman wrote:
Vikky_Vik wrote:And squashman, that is what my script also doing, it is also keeping the two latest folders. But i want to keep other folders also which are not of month-date format.

Regards,

My script does not touch your XYZ or ABC folders. I pipe the output of the DIR command to the FINDSTR command. The findstr command then looks for your DATE pattern. The folder name has to start with ##-##.

I should also point out that my script assumes your directories were created the same day that you named them. My script does have its caveats but if your folder names are created the same day they are named then in theory it should work.

But if you really want something that does the math to figure it out you will have to post the output of your DATE variable from the cmd prompt.
So Please post the output of echo %date% from the cmd prompt so that other people here can give you a more solid script.

Vikky_Vik
Posts: 7
Joined: 27 Dec 2011 03:26

Re: delete 2 days older folders

#10 Post by Vikky_Vik » 27 Dec 2011 12:26

Hi aGerman,

Here is the output you are asking.

C:\Users\Vikky_Vik>echo %date%
Tue 12/27/2011

C:\Users\Vikky_Vik>

Please let me know if you need more information.

And aGerman , i did not understand your 1st point exactly. Please explain me if i need to run some query or something else.

Regards,

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: delete 2 days older folders

#11 Post by Squashman » 27 Dec 2011 12:31

Posting a link to a script written on another forum by an old buddy of mine (TheOutcaste).
http://forums.techguy.org/dos-other/976 ... lders.html

This script should work for you and it doesn't rely on what your DATE variable is. Credit is given throughout the script from whom wrote the original code.

I can show you what lines you need to change for your purposes.
Change all the USER VARIABLES to your needs

Code: Select all

:: User Variables
:: Set this next line to False to automatically delete all folders found
Set _Prompt=True
:: Set this to the number of days you want to keep
Set _DaysKept=10
:: Set this to the folder that contains the folders to check and delete
Set _Path=C:\Backup

You also need to change the findstr command to this.

Code: Select all

Findstr /B /R "[0-1][0-9]-[0-3][0-9]"

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: delete 2 days older folders

#12 Post by aGerman » 27 Dec 2011 13:09

I'm sorry, you already answered the question but for some reason I missed your first answer.
OK, here you will find the 2 functions :date2jdate and :jdate2date. You need it to calculate a number of days from a date and backward.

untested:

Code: Select all

@echo off &setlocal

for /f "tokens=1-3 delims=/" %%a in ("%date:* =%") do (
  set "mm=%%a"
  set "dd=%%b"
  set "yy=%%c"
)

set "today=%mm%-%dd%"

call :date2jdate jd %yy% %mm% %dd%
set /a jd-=1
call :jdate2date %jd% yy_y mm_y dd_y

set /a mm_y=10%mm_y%, dd_y=10%dd_y%
set mm_y=%mm_y:~-2%
set dd_y=%dd_y:~-2%

set "yesterday=%mm_y%-%dd_y%"

for /f "delims=" %%i in ('dir /ad /b^|findstr /rxc:"[01][0-9]-[0-3][0-9]"^|findstr /vx "%today% %yesterday%"') do rd /s /q "%%i"
goto :eof

:date2jdate JD YYYY MM DD -- converts a gregorian calender date to julian day format
::                        -- JD   [out] - julian days
::                        -- YYYY [in]  - gregorian year, i.e. 2006
::                        -- MM   [in]  - gregorian month, i.e. 12 for december
::                        -- DD   [in]  - gregorian day, i.e. 31
:$reference http://aa.usno.navy.mil/faq/docs/JD_Formula.html
:$created 20060101 :$changed 20080219 :$categories DateAndTime
:$source http://www.dostips.com
SETLOCAL
set "yy=%~2"&set "mm=%~3"&set "dd=%~4"
set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
if %yy% LSS 100 set /a yy+=2000 &rem Adds 2000 to two digit years
set /a JD=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
ENDLOCAL & IF "%~1" NEQ "" (SET %~1=%JD%) ELSE (echo.%JD%)
EXIT /b

:jdate2date JD YYYY MM DD -- converts julian days to gregorian date format
::                     -- JD   [in]  - julian days
::                     -- YYYY [out] - gregorian year, i.e. 2006
::                     -- MM   [out] - gregorian month, i.e. 12 for december
::                     -- DD   [out] - gregorian day, i.e. 31
:$reference http://aa.usno.navy.mil/faq/docs/JD_Formula.html
:$created 20060101 :$changed 20080219 :$categories DateAndTime
:$source http://www.dostips.com
SETLOCAL ENABLEDELAYEDEXPANSION
set /a L= %~1+68569,     N= 4*L/146097, L= L-(146097*N+3)/4, I= 4000*(L+1)/1461001
set /a L= L-1461*I/4+31, J= 80*L/2447,  K= L-2447*J/80,      L= J/11
set /a J= J+2-12*L,      I= 100*(N-49)+I+L
set /a YYYY= I,  MM=100+J,  DD=100+K
set MM=%MM:~-2%
set DD=%DD:~-2%
( ENDLOCAL & REM RETURN VALUES
    IF "%~2" NEQ "" (SET %~2=%YYYY%) ELSE echo.%YYYY%
    IF "%~3" NEQ "" (SET %~3=%MM%) ELSE echo.%MM%
    IF "%~4" NEQ "" (SET %~4=%DD%) ELSE echo.%DD%
)
EXIT /b

Vikky_Vik
Posts: 7
Joined: 27 Dec 2011 03:26

Re: delete 2 days older folders

#13 Post by Vikky_Vik » 27 Dec 2011 13:27

Hi aGerman,

Thanks a lot for your help. I will go through this script and let you know the feedback.

I want to confirm one thing , isn't there short script to do same? Because the one who assign me this task said that it is not a big task, script is of 10-15 rows only.
I am not sure if had already a script for same or he has some other thoughts.
Can you please confirm.

Regards,

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: delete 2 days older folders

#14 Post by aGerman » 27 Dec 2011 13:38

If you're familiar with batch you could insert the equations of the functions directly into the "main" code. The result would be a code of approx. 30 - 35 lines. But since batch does not provide the possibility to calculate date values directly you need these complicated algorithms.

Regards
aGerman

Vikky_Vik
Posts: 7
Joined: 27 Dec 2011 03:26

Re: delete 2 days older folders

#15 Post by Vikky_Vik » 27 Dec 2011 13:53

Thankyou squashman,

I will go through your script and let you knoe the feedback.

Regards,
Vikky_Vik

Post Reply