4

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?

2
  • Actually, I don't fully understand what you are trying to do. You tried to change the first letters of file, then you tried to change the last part of the path. What exactly do you want the output paths to be? Can you add an example of output names you desire? How did you get the names with id in the first image, did you use an expression? Feb 12, 2022 at 15:51
  • @KadirŞahbaz That's my fault, the "fileName.replace("_G", "G"))" is what I usually use for another purpose, but it was only indicative of the script I currently have.
    – NorthSon
    Feb 12, 2022 at 16:06

1 Answer 1

6

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

1
  • Thank you very much. Now the String Expression Builder returns me the results I was expecting. I've realised then I was missing the information about the complete path of the output, but I've managed to concatenate the file_path function with the rest of the expression.
    – NorthSon
    Feb 12, 2022 at 16:09

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.