[GIS] How to batch “gdal_merge” on Win7 in one command line command

gdal

I want to create a mosaic of all tif files in a directory on windows 7.

How could I create a one-line command in the windows command line to loop through all tifs and merge them all together using gdal_merge.py?

I tried this;

for %i in (*.tif) do gdal_merge.py -of GTiff %i mosaic.tif

but it gives me an error about mosaic not being a supported dataset name??

Best Answer

Your command line will try to merge mosaic.tif (as part of *.tif) with itself. Creating the mosaic in another folder would prevent that.

The correct syntax for batch processing is:

for %%N in (D:\Karten\gdal\gdal2tiles\NL50\*.tif) DO gdal_translate -of vrt -expand rgba %%N D:\Karten\gdal\gdal2tiles\NL50\%%~nN.vrt
gdalbuildvrt -allow_projection_difference index.vrt NL50\*.vrt
pause

This should also work for gdal_merge.py, but I prefer to work with the vrt solution.