Search code examples
cmdwindows-10gdal

How to convert a folder of files with GDAL TRANSFER?


I have about 2000 images that need to be converted from TIF to JPG with some parameters using GDAL. I need to achieve this by using a CMD script. Here is my script so far:

set "SourceFolder=C:\Tif\newimages\"
set "DestinationFolder=C:\Tif\temp"
set "CreatedFolder="

if not exist "%DestinationFolder%\" (
    md "%DestinationFolder%" 2>nul
    if not exist "%DestinationFolder%\" (
        echo Error: Failed to create folder "%DestinationFolder%"
        goto EndBatch
    )
    set "CreatedFolder=1"
)
for %%N in (%SourceFolder%*.tif) DO gdal_translate -of jpeg -a_nodata 0 -b 1 -b 2 -b 3 %%i %DestinationFolder%\%%i*jpg

:EndBatch
endlocal
pause

This produces the following error message on CMD:

ERROR 4: %i: No such file or directory

Background:

Windows environment (obviously)
OSGeo4W cmd to run the script.

What I am looking for is an explanation of what I am doing wrong with the script and suggestions to make it functional.

Edit:

I realize that the commands at the end of the gdal line should be source and destinations of converted files. Is there any way around this as I would need to only define the destination folder at the end. I got it to work as an endless loop which was not optimal of course as the number of images is more than 2000.


Solution

  • Well after closer inspection, it turns out that I set a value for %%N and later tried to use %%i. Also, the gdal command should be this:

    DO gdal_translate -of jpeg -a_nodata 0 -b 1 -b 2 -b 3 %%N %DestinationFolder%\%%~nN.jpg
    

    Instead of:

    DO gdal_translate -of jpeg -a_nodata 0 -b 1 -b 2 -b 3 %%i %DestinationFolder%\%%i*jpg