# DOS Batch: How to process files in a series based on the date of the filename(Batch)



## _unknown_ (Oct 28, 2014)

How will merge/process the files in a series based on their date using the script below? Whenever the system is down and you weren't able to process the files let's say for one week, it will just check for the last date where it stop and process it and then process the next date and process the next and so on. But how can I do that? Any idea?

Let's say, the system was down last November 25 and resume on December 1. So it failed to process the files from 25-1. When the system resume, the batch should then process first the files from November 25, then 26, then 27, then 28, 29, 30 and 1, it should process in series not randomly or not from latest date to oldest date. Then on 2 it will run normally. It will process the files for 2 and so on.

File names:


T2010232034000.L2_LAC.Tera.tif
 T2010232035000.L2_LAC.Tera.tif
 T2010232036000.L2_LAC.Tera.tif
 T2010232037000.L2_LAC.Tera.tif


 T2010233045000.L2_LAC.Tera.tif
 T2010233046000.L2_LAC.Tera.tif
 T2010233047000.L2_LAC.Tera.tif
 T2010233048000.L2_LAC.Tera.tif
 T2010233049000.L2_LAC.Tera.tif
Note: T2010232034000 = Terra/Data Year/Julian Date/HH/MM/SS


```
@echo on
setlocal EnableDelayedExpansion

rem Modify following variables
set "in_path=E:\Proc\Pro\"
set "out_path=E:\Proc\Mer"
set "proc_path=E:\Proc\Proc_Pro"

rem DON'T modify the following variables
set "yearDay="
set "fileList="
set "numFiles=0"

md %out_path%
md %proc_path%

rem Process all *.tif files in input path
pushd "%in_path%"
for %%a in (*.tif) do (
   set "fileName=%%a"
   rem If the YearDay in this file is the same of previous one
   if "!fileName:~1,7!" equ "!yearDay!" (
      rem Join this filename to previous list
      set "fileList=!fileList!!fileName! "
      set /A numFiles+=1
   ) else (
      rem Merge the files in the list if there are more than 2, in the out_path leaving only TYYYYDDD.L2_LAC.Tera.tif
      if !numFiles! gtr 2 (
         gdal_merge.py -n 0 -a_nodata -32767 -of GTiff -o %out_path%\T!yearDay!.L2_LAC.Tera.tif !fileList!
         rem Move processed files to a different directory
         for %%a in (!fileList!) do move %%a "%proc_path%"  >nul
      )
      rem And start a new YearDay and fileList
      set "yearDay=!fileName:~1,7!"
      set "fileList=!fileName! "
      set numFiles=1
   )
)
rem Merge the files in the last list if there are more than 2
if !numFiles! gtr 2 (
   gdal_merge.py -n 0 -a_nodata -32767 -of GTiff -o %out_path%\T!yearDay!.L2_LAC.Tera.tif !fileList!
   for %%a in (!fileList!) do move %%a "%proc_path%"  >nul
)
popd
```


----------



## Squashman (Apr 4, 2003)

Oh jeez. You found your way over here too.


----------



## _unknown_ (Oct 28, 2014)

This issue is not yet solved. That's why I posted it anywhere again. I hope you'd understand.


----------



## _unknown_ (Oct 28, 2014)

Remove or delete this post.


----------

