$if inserts a space if the metadata Field is blank while renaming folders based on song data. How to avoid or remove during renaming?

I have an action that automatically renames the parent folder based on the metadata of the songs, using the $if function depending on if certain fields are empty. I tried using $trim but it didn't seem to work.

$if(%catalog%,'['%catalog%']',) %album% (%year%) $if(%event%,'['%event%']',) (%_EXTENSION%)

CATALOG and EVENT are custom metadata fields for the songs. It results in a neat folder name such as:

[ACCD2] Acclaims Raving X-Mas (1997) (flac)

However, my problem is that the second half of $if (the "no" else latter half, ",)") is inserting blank spaces in the filename if the metadata field is empty. This is inserting a space at the beginning of the folder name, and inserting a space after the year.

How can I avoid or automatically trim this while running this action? Ideally I don't want to run a separate "remove leading spaces" action after this, I want it all-in-one.

Thanks for any help.

Could you please enclose your complete $if formula in code brackets image (the 6. symbol in the mini editor toolbar of this discourse forum - or select your formula and press CTRL + E). This ensures that we can better see the backslashes and spaces and no character get lost.

You could save a number of complicated $IF()s if :wink: you used the square-bracket syntax for optional parts:
['['%catalog%']' ]%album% (%year%)[ '['%event%']'] (%_EXTENSION%)

1 Like

That seems to work! Thanks. I'm new to all this, so I guess now I should figure out why your version doesn't seem to need $if lol.

If you had included the spaces in the statements instead of as separators between the statements so that the space would have been part of the output only if the condition returned a true result, then you would not have had the extra spaces in either case. This was a logic problem, no syntax problem.

I only find the square bracket syntax a little easier to read.

I see. Thanks for the explanation.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.