[GIS] Keeping the original file name when batch processing GDAL translate

convertgdalgeotiff-tiffqgisqgis-processing

I would like to convert .tif files to Idrisi or ENVI format. I would like to do a batch process, keeping the original filename. When I choose: "run as a batch process", as I can see, I still have to set the output name and directory for each file?

Is there a faster way to do this?

Best Answer

You can set the output to take the file name as an additional string which will appear in front of a string specified by you. So what you could do is in the first row, click the ellipses button (shown in red circle), define a path (e.g. to an empty folder) and use a single character like an underscore (shown in green circle). Then click Save.

Translate output

You will receive another pop up menu for Autofill settings.

Select the following options:

  • Autofill mode - Fill with parameter values
  • Parameter to use - Input layer

Autofill settings

Then click OK. I would also suggest setting the Load in QGIS parameter to No for all your layers. Then run your process.

When it is finished, open the Python Console from the menubar Plugins > Python Console and run the following code to remove the underscore from the output files, leaving you with the original file name using os.rename:

import os
os.chdir("path/to/folder/")
for fileName in os.listdir("."):
    os.rename(fileName, fileName.replace("_", ""))