QGIS Batch Processing – Parameter Autofill Output Name Doesn’t Match Input Layer Name

batchpythonqgisqgis-modeler

I'm using a model I've prepared on QGIS Graphical Modeler and I'm trying to batch processing this tool. I've noticed that compared to normal batch processing using any processing tool, the autofill function for output names using parameter values generates output names that don't match the input names, reporting a hexadecimal code as a suffix:

enter image description here

I've found out that this is due to the input layer ID.

As a workaround, I checked this thread on a similar issue, but I can't apply the proposed solution because any expression I use on the Expression String Builder, I only get NULL as a result:

enter image description here

Alternatively, I may let the autofill set the names as in figure 1 and then rename files using a Python script. I already use this script to substitute any set of characters in the filename, but unfortunately, these hexadecimal codes are all different.

import os
os.chdir("C:\folder")
for fileName in os.listdir("."):
    os.rename(fileName, fileName.replace("_G", "G"))

I've searched for this topic and a proposed solution is using a script like this:

import os
path1=....
for i in os.listdir(path1):
    os.rename(os.path.join(path1, i),os.path.join(path1, i[:8])+'.txt')

There are two problems:

  1. Treating with shapefiles I have 4 different file extensions;
  2. Many scripts like this work by considering a "basename" with a fixed number of characters (the i[:8] argument) to filter out the rest of the filename, but in my case, the basename has no fixed number of characters because the S45W16 part refers to DMS coordinates and can be from 4 characters to 7 characters long.

Is there any way I could solve this problem, either inside the batch processing or using Python scripts?

Best Answer

Variables in the Expression window are case-sensitive, unlike the functions in it.

In your case, use this:

base_file_name( layer_property( @Input, 'source' ) ) + '_SUFFIX'

enter image description here

When using INPUT instead of Input you get NULL.

enter image description here

Related Question