Search code examples
batch-filegdal

How to use gdal_calc in a batch file


I'm trying to create a mask file from several input images using gdal with a batch windows file. However, the system is sending me a error when I use the "!" on the comparison calc, and after the first round, all the variables had read as a string. My code is the following:

@ECHO OFF
SETLOCAL EnableDelayedExpansion
SET mypath=F:\my_in_path\
SET path_salida=F:\my_out_path\
FOR /F %%i IN ('DIR /B %mypath%*.tif') DO (
    SET infile=%%i
    SET outfile=!infile!
    echo %mypath%!infile!
    echo %path_salida%!outfile!
    gdal_calc  -A %mypath%!infile! --outfile %path_salida%!outfile! --calc="2*(A==0)+1*(A==0)" --NoDataValue=0 --quiet 
)

Solution

  • As you've provided no feedback over half a day since my comment, here is an example to test and provide feedback on:

    @Echo Off
    Set "mypath=F:\my_in_path"
    Set "path_salida=F:\my_out_path"
    Set "calc_params=-A "%%A" --outfile "%path_salida%\%%~nxA" --calc="2*(A==0)+1*(A==0)" --NoDataValue=0 --quiet"
    
    For %%A In ("%mypath%\*.tif") Do gdal_calc %calc_params%