# Solved: Help with batch file that removed folder 10 days old



## lookwow (Feb 19, 2009)

Currently I have a batch file that copys a folder which has several other folders and files in it. The batch file copys the file TRUDATA to C:\trubkup and renames the folder with the date in front(ex:2009_02_20_TRUDATA).

I need a batch file that will look at the files that are out there and remove anything that is over 10 days old. It can either be 10 days by date or just remove anything that is over the 10 newest ones. I would like to use the folder name rather then created/modify if possible.

Thanks for your help.


----------



## devil_himself (Apr 7, 2007)

you want do delete the files in "TRUDATA" or "C:\trbackup" which are more than 10 days old ?



> I would like to use the folder name rather then created/modify if possible.


i don't quite understand this ??


----------



## TheOutcaste (Aug 8, 2007)

Welcome to TSG!

See [post=6353772]this post[/post]. Change the following two lines:
* Set _NumtoKeep=6
Set _Path=C:\Test*
to
* Set _NumtoKeep=10
Set _Path=C:\trubkup*
This will keep the 10 newest folders under C:\trubkup and delete the rest. It will not delete any files that are in C:\trubkup, just folders and their contents. This does not check the folder name in any way, so it will delete folders besides ones named YYYY_MM_DD_TRUDATA
You can exclude all folders that don't have TRUDATA in the name by using this:
* Set _Path=C:\trubkup\*TRUDATA**

If you want to actually check the date in the folder name, this should work. It's based on the code in Post 4 of the same thread above. I've attached it as a zip file as well; if you copy and paste, the tab delimiter set in the 4th line never gets set:


```
@Echo Off
[B][COLOR=Red]:: Set the next variable equal to tab followed by a space
:: If you copy this code, you may have to manually edit this line[/COLOR][/B]
Set _TabSpace=     
Setlocal EnableDelayedExpansion
:: User Variables
:: 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:\test1\trubkup
:: Set this to the base name of the folders to find. If this string is not in the folder name, the folder will be skipped
:: Set this to nothing to find all folders
Set _BaseName=\*TRUDATA*
:: Get todays date
Call :GetDate
Set _yy=%_fDate:~,4%
Set _mm=%_fDate:~4,2%
Set _dd=%_fDate:~6,2%
:: Convert todays date to Julian
Call :JDate %_yy% %_mm% %_dd%
Set _JToday=%_JDate%
:: Set delete date
Set /a _DelDate=_JToday-%_DaysKept%-1
If Exist "%temp%\tf}1{" Del "%temp%\tf}1{"
PushD %_Path%
Set _s=s
If %_DaysKept%==1 set _s=
Echo Please wait, searching for folders more than %_DaysKept% day%_s% old
For /F "Tokens=1-4 Delims=_" %%I In ('dir "%_Path%%_BaseName%" /AD /B /ON') Do (
  Call :JDate %%I %%J %%K
  If !_JDate! LEQ %_DelDate% (
    If Exist "%temp%\tf}1{" (
      Echo %%I_%%J_%%K_%%L>>"%temp%\tf}1{"
    ) Else (
      Echo.>"%temp%\tf}1{"
      Echo Do you wish to delete the following folders?>>"%temp%\tf}1{"
      Echo      Name>>"%temp%\tf}1{"
      Echo %%I_%%J_%%K_%%L>>"%temp%\tf}1{"
    )) Else (
  Goto :_allFound
))
:_allFound
If Not Exist "%temp%\tf}1{" Echo No Folders Found to delete&Goto _Done
Type "%temp%\tf}1{" | More
Set _rdflag= /q
:_Prompt1
Set /P _resp=Delete All, None, or Prompt for each (A/N/P)?
If /I "%_resp:~0,1%"=="N" Goto _Done
If /I "%_resp:~0,1%"=="A" Goto _Removeold
If /I NOT "%_resp:~0,1%"=="P" (Echo (A/N/P only please)&Goto _Prompt1
Set _rdflag=
:_Removeold
For /F "skip=3 Delims=" %%I In ('type "%temp%\tf}1{"') Do (
 If "%_rdflag%"=="" Echo Deleting
 rd /s%_rdflag% "%%I")
:_Done
If Exist "%temp%\tf}1{" Del "%temp%\tf}1{"
Set _TabSpace=
PopD
Goto:EOF
::===================================::
::                                         ::
::   -   S u b r o u t i n e s   -   ::
::                                         ::
::===================================::
:JDate
:: Convert date to Julian
:: Arguments : YYYY MM DD
:: Returns   : Julian date in variable _JDate
:: Usage
::Call :JDate %__GYear% %_GMonth% %_GDay%
:: First strip leading zeroes; a logical error in this
:: routine was corrected with help from Alexander Shapiro
::Code taken from datediff.bat written by Rob van der Woude
::http://www.robvanderwoude.com
Set _JMM=%2
Set _JDD=%3
IF 1%_JMM% LSS 110 Set _JMM=%_JMM:~1%
IF 1%_JDD% LSS 110 Set _JDD=%_JDD:~1%
::
:: Algorithm based on Fliegel-Van Flandern
:: algorithm from the Astronomical Almanac,
:: provided by Doctor Fenton on the Math Forum
:: (http://mathforum.org/library/drmath/view/51907.html),
:: and converted to batch code by Ron Bakowski.
Set /A _JMonth1 = ( %_JMM% - 14 ) / 12
Set /A _JYear1  = %1 + 4800
Set /A _JDate  = 1461 * ( %_JYear1% + %_JMonth1% ) / 4 + 367 * ( %_JMM% - 2 -12 * %_JMonth1% ) / 12 - ( 3 * ( ( %_JYear1% + %_JMonth1% + 100 ) /100 ) ) / 4 + %_JDD% - 32075
For %%A In (_JMonth1 _JYear1) Do Set %%A=
Goto:EOF
:GetDate
:: This subroutine will always display the same results,
:: for the date independent of "International" settings.
:: This batch file uses REG.EXE from the NT Resource Kit
:: (already installed with WinXP and Vista)
:: to read the "International" settings from the registry.
:: Date is returned as yyyymmdd in variable _fdate
:: Modified from SortDate Written by Rob van der Woude
:: http://www.robvanderwoude.com
::
If NOT [%1]==[] Set Date=%1
If "%date%A" LSS "A" (Set _NumTok=1-3) Else (Set _NumTok=2-4)
:: Delims= is a TAB followed by a space in the next two lines
For /F "SKIP=3 TOKENS=2* DELIMS=%_TabSpace%" %%A In ('REG QUERY "HKCU\Control Panel\International" /v iDate') Do Set _iDate=%%B
For /F "SKIP=3 TOKENS=2* DELIMS=%_TabSpace%" %%A In ('REG QUERY "HKCU\Control Panel\International" /v sDate') Do Set _sDate=%%B
IF %_iDate%==0 For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%date%") Do Set _fdate=%%D%%B%%C
IF %_iDate%==1 For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%date%") Do Set _fdate=%%D%%C%%B
IF %_iDate%==2 For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%date%") Do Set _fdate=%%B%%C%%D
Goto:EOF
```
HTH

Jerry


----------



## lookwow (Feb 19, 2009)

First off you are the best TheOutcaste. Thanks a ton for the help on this. I have a background in programming, but batch files just go over my head.

Also thanks for all the comments. I actually think im starting to understand how it flows.


----------



## Squashman (Apr 4, 2003)

lookwow said:


> First off you are the best TheOutcaste. Thanks a ton for the help on this. I have a background in programming, but batch files just go over my head.
> 
> Also thanks for all the comments. I actually think im starting to understand how it flows.


Yes he is.
It is basically about syntax. I had the hardest time learning the syntax of the commands when it came to batch files. And you really never know what the commands can do until you learn the syntax or see some other batch file that does something and then you can adapt to something else you were working on. I have an ongoing batch file that Outcaste has been helping me with. It now has developed into an batch file with Vbscript as a subroutine.


----------



## lookwow (Feb 19, 2009)

Ive ran into one problem. Im using the one you zipped me. If finds the correct folders, but when i select A to delete them i get the message "The Directory is not empty"

Any ideas?


----------



## TheOutcaste (Aug 8, 2007)

lookwow said:


> Ive ran into one problem. Im using the one you zipped me. If finds the correct folders, but when i select A to delete them i get the message "The Directory is not empty"
> 
> Any ideas?


First one that pops to mind is don't try to delete Directories that aren't empty

Seems I let a little typo in. I've corrected the code in the above post, and uploaded a corrected version. I also removed a couple of lines (#24 and #25) that aren't needed as we are getting the date from the folder name, rather than a directory listing, and found another little bug that will only appear if there are no folders new enough to keep -- the POPD statement is in the wrong place.

So download the latest version. The problem with it not deleting is the extra space in Lines 35 and 40. The space between the *%%L* and *>>* in both lines adds a space to the folder name, and for some reason the RD command doesn't see this as a *not found* error, but a *not empty* error. So this:

```
Echo %%I_%%J_%%K_%%[COLOR=Red]L >[/COLOR]>"%temp%\tf}1{"
```
should be this:

```
Echo %%I_%%J_%%K_%%L>>"%temp%\tf}1{"
```
When I created my test folders I put subfolders and files into just a few -- and they were all ones with dates less than 10 days old.
This time I even tested with hidden and system files/folders

Amazing how real world data will always find a way to trip you up.

Jerry


----------



## lookwow (Feb 19, 2009)

That worked perfectly. Thanks a million.


----------



## TheOutcaste (Aug 8, 2007)

You're Welcome!

If your issue has been resolved you (and ONLY you) can mark this thread Solved by using the Mark Solved button at the Top Left of this thread (above the first post) 










Jerry


----------

