0

I'm building batch files to (1) move directories older than 120 days to an archive location and (2) then eventually delete those folders from the archive location when they are 4 years old.

The names of the directories are in CCYYMM format (201601). The business rule is to move/delete based on the date in the directory name, not the modify or create date.

This is similar to Batch file logic to delete files based on date in filename, but since my scenario is for directories, not files, not certain if this would work, or how to modify for directories. The FOR command only appears to work for files(?)

Is there a method to loop through directories and perform a move or rmdir command in a batch file, comparing the month/year in the name to current month/year?

I did find that FORFILE works for directories based on modified date, but requirements are to use directory name.

Thanks for your consideration!

Edit 20160817

Since the directories are year/month labeled (201601 for January 2016), I'm taking the current year month (201608) and subtracting the directory value. For example 201608 - 201601 = 7. Then the batch would move any folder that has a difference of 4 or greater to meet the business requirements, using the FOR /D and only looking at directories beginning with 2:

    @echo off
    setlocal EnableDelayedExpansion

    SET tempDATE=%DATE:~10,4%%DATE:~4,2%

    for /d  %%a in ("2*") do (
    set /a numberOld=tempDATE - %%a

    if !numberOld! geq 4 move /Y %%a ArchiveFolder 
    )

Using the same type of logic, I'm using 400 as the test value for the remove/delete directory script.

Appreciate everyone's insight.

Community
  • 1
  • 1
lmohr
  • 1
  • 2
  • 1
    Possible duplicate of [Batch file put folders into other folders depending on name](http://stackoverflow.com/questions/38970982/batch-file-put-folders-into-other-folders-depending-on-name) – aschipfl Aug 16 '16 at 21:14
  • The biggest problem here is the date math. Some possible approaches are here: http://stackoverflow.com/questions/355425/date-arithmetic-in-cmd-scripting Powershell might be better than those options. The rest should be pretty easy. Check out `for /d` – shawnt00 Aug 16 '16 at 21:38
  • 1
    I _never_ have seen the CCYYMM notation to refer to dates. Although I understand that in the 201601 date the CC is 20 and the YY is 16 ("year 16 of 20th century") I never have seen this format before... – Aacini Aug 17 '16 at 00:21
  • Possible duplicate of [Batch process to move file having Date in YYYYMMDD format from one folder to another folder](http://stackoverflow.com/q/8937944) – aschipfl Aug 17 '16 at 08:43
  • Thanks for those of you looking at this - please see my edit above. @shawnt00 - you put me on the correct path! – lmohr Aug 17 '16 at 15:07
  • In the original question you refer to: _"CCYYMM format (201601)"_, but in the edit you said: _"year/month labeled (201601 for January 2016)"_. What happened with the CCYYMM format? Our answers are based on _your_ description of the problem, so if the description is confusing, you'll get confusing replies (like this one!). IMHO the "CCYYMM format" for dates is very unusual and still have no idea why you used it. Perhaps in this case this detail seems a foolishness, but in a complicated problem the wrong terminology may lead to big confusions! – Aacini Aug 20 '16 at 14:23
  • What about looking at the file modification date rather than their names? would that be an option for you? if so, check out the [`forfiles`](http://ss64.com/nt/forfiles.html) command and its `/D` option... – aschipfl Sep 02 '16 at 23:33

0 Answers0