I am trying to use SAGA_cmd to write a script that does the following: The united 1) Reads a series TIFF files I have in the work folder; 2) Runs a segmentation on the TIFF files on each respective file, and; 3) Outputs the corresponding shapefiles to a folder.

I have developed the following code based on examples from tutorials and searches online.

set PATH=%PATH%;C:\Users\PC\saga-4.1.0_x64


FOR %%i IN ('dir /b %WORK%\*.tif') DO (

saga_cmd io_grid_image  "Import Image (bmp, jpg, png, tif, gif, pnm, xpm)" ^ 

-OUT_GRID = %WORK%\%%~.sgrd ^
-OUT_RED="Band_1" ^
-OUT_GREEN="Band_2" ^
-OUT_BLUE="Band_3" ^
-FILE= %WORK%\%%i ^

REM Tool: Object Based Image Segmentation

saga_cmd imagery obia ^
-FEATURES= %WORK%\%%~ni_seg.sgrd ^
-OBJECTS=%WORK%\%%~ni.sgrd ^

However, I get the following error:

-OUT_GRID is not recognized as an internal or external command, operable program or batch file."

I would like to find the source of this error or figure out how to rectify it.

Also, is it possible to use the tool chains in SAGA to automatically input multiple files, i.e. batch process.

1 Answer 1


This error is a common one for begginners in scripting. You an unescaped the line-break before your command is finished. The carats (^) at the end of the lines escape the line-break to prevent the multi-line command from executing before it is fully entered. See another example of line breaks in use here.

The command shell is attempting to run the lines separately as

  1. saga_cmd io_grid_image "Import Image (bmp, jpg, png, tif, gif, pnm, xpm)" ^
  2. -OUT_GRID = %WORK%\%%~.sgrd ^

The second fails because, as the error says, -OUT_GRID is not a command. This might be because of the empty line between the two. I suggest you research basic batch scripting in windows to move forward with this. Batch scripting will allow you to iterate over multiple files and process them, in that is what the FOR %%i IN ('dir /b %WORK%\*.tif') DO ( block is setting up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.