[FWTools] gdal_merge.bat

Matt Wilkie matt.wilkie at gov.yk.ca
Thu Nov 17 15:14:42 EST 2005

> It isn't clear to me what you have tried, and how it failed.
> Perhaps you could explain.  You should be able to do something
> like:
> gdal_merge.bat -o mosaico -of GTiff A1.tif A2.tif A3.tif A4.tif
> Unfortunately, the DOS command shell does not do wildcard
> expanation so you need to list all the input files.

The lack of wildcard expansion is indeed a pain. There is a workaround 
but you have to create a batch file to use it. An example is attached 
(remove the .txt extension). The important bit is:

    setlocal enabledelayedexpansion
    set infiles=
    for %%a in (d:\src\*.tif) do set infiles=!infiles! %%a
    call gdal_merge -o outfile.tif %infiles%

Frank: an easier solution for end users would be to add a feature to use 
a text file as the input list. E.g:

    gdal_merge -o merged.tif @filelist.txt


matt wilkie
Geographic Information,
Information Management and Technology,
Yukon Department of Environment
10 Burns Road * Whitehorse, Yukon * Y1A 4Y9
867-667-8133 Tel * 867-393-7003 Fax
-------------- next part --------------
@echo off
:: initial version, 2005-Nov-17, matt wilkie
:: this script is public domain
if [%1]==[] (
	echo -={ gdal_wildmerge }=- Allow gdal_merge to accept wildcards for input files
	echo .
	echo . 	gdal_wildmerge.bat [input-file-wildcard] [output-file] {options}
	echo . 
	echo .	gdal_wildmerge d:\src\*.tif mosaick.tif -init -9999
	goto :EOF
	:: %1 is the input wildcard mask
	:: This is inverted from normal usage in order to allow other
	:: options be passed to gdal_merge

:: delayed expansion requires windows NT or later command shell 
:: (it won't work on windows 9x)
	verify other 2>nul 
	if errorlevel 1 goto :wrong_version

:: gdal_merge won't overwrite an existing file	
	if exist %2 goto :file_exists

:: Build the input file list
	set infiles=
	for %%a in (%1) do set infiles=!infiles! "%%a"

:: Here is were the actual work happens
	set gdalcmd=gdal_merge %infiles% -o "%2"
   echo %gdalcmd%
   :: IMPORTANT, if gdal_merge is not call'ed control doesn't return 
	:: to the batch file
	call %gdalcmd%
	goto :EOF

	echo *** the output file "%2" already exists.
	goto :EOF

	echo	Sorry, this batchfile requires DELAYED EXPANSION which is 
	echo	not available in this shell. See 
	echo  http://www.google.com/search?q=enabledelayedexpansion
	goto :EOF

More information about the FWTools mailing list