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

**URL:** https://community.mp3tag.de/t/if-inserts-a-space-if-the-metadata-field-is-blank-while-renaming-folders-based-on-song-data-how-to-avoid-or-remove-during-renaming/54097
**Category:** Support
**Created:** [August 17, 2021, 8:43pm UTC](https://community.mp3tag.de/t/if-inserts-a-space-if-the-metadata-field-is-blank-while-renaming-folders-based-on-song-data-how-to-avoid-or-remove-during-renaming/54097 "2021-08-17T20:43:20Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Habanero](https://community.mp3tag.de/user_avatar/community.mp3tag.de/habanero/32/8241_2.png) [@Habanero](https://community.mp3tag.de/u/Habanero)
#### Post date: [August 17, 2021, 8:43pm UTC](https://community.mp3tag.de/t/if-inserts-a-space-if-the-metadata-field-is-blank-while-renaming-folders-based-on-song-data-how-to-avoid-or-remove-during-renaming/54097/1 "2021-08-17T20:43:20Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![LyricsLover](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/l/c6cbf5/32.png) [@LyricsLover](https://community.mp3tag.de/u/LyricsLover)
#### Post date: [August 17, 2021, 9:55pm UTC](https://community.mp3tag.de/t/if-inserts-a-space-if-the-metadata-field-is-blank-while-renaming-folders-based-on-song-data-how-to-avoid-or-remove-during-renaming/54097/2 "2021-08-17T21:55:53Z")

</div>

Could you please enclose your complete $if formula in code brackets ![image](https://community.mp3tag.de/uploads/default/original/2X/8/84cca8b108d371be8ab5bea7015e54924c11f9c8.png) (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.

---

<div class="post-metadata">

### Author: ![ohrenkino](https://community.mp3tag.de/user_avatar/community.mp3tag.de/ohrenkino/32/4843_2.png) [@ohrenkino](https://community.mp3tag.de/u/ohrenkino)
#### Post date: [August 18, 2021, 6:19am UTC](https://community.mp3tag.de/t/if-inserts-a-space-if-the-metadata-field-is-blank-while-renaming-folders-based-on-song-data-how-to-avoid-or-remove-during-renaming/54097/3 "2021-08-18T06:19:08Z")

</div>

> [@Habanero](#):
>
> $if(%catalog%,'['%catalog%']',) %album% (%year%) $if(%event%,'['%event%']',) (%\_EXTENSION%)

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

---

<div class="post-metadata">

### Author: ![Habanero](https://community.mp3tag.de/user_avatar/community.mp3tag.de/habanero/32/8241_2.png) [@Habanero](https://community.mp3tag.de/u/Habanero)
#### Post date: [August 18, 2021, 4:26pm UTC](https://community.mp3tag.de/t/if-inserts-a-space-if-the-metadata-field-is-blank-while-renaming-folders-based-on-song-data-how-to-avoid-or-remove-during-renaming/54097/4 "2021-08-18T16:26:52Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![ohrenkino](https://community.mp3tag.de/user_avatar/community.mp3tag.de/ohrenkino/32/4843_2.png) [@ohrenkino](https://community.mp3tag.de/u/ohrenkino)
#### Post date: [August 18, 2021, 4:46pm UTC](https://community.mp3tag.de/t/if-inserts-a-space-if-the-metadata-field-is-blank-while-renaming-folders-based-on-song-data-how-to-avoid-or-remove-during-renaming/54097/5 "2021-08-18T16:46:07Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Habanero](https://community.mp3tag.de/user_avatar/community.mp3tag.de/habanero/32/8241_2.png) [@Habanero](https://community.mp3tag.de/u/Habanero)
#### Post date: [August 18, 2021, 6:21pm UTC](https://community.mp3tag.de/t/if-inserts-a-space-if-the-metadata-field-is-blank-while-renaming-folders-based-on-song-data-how-to-avoid-or-remove-during-renaming/54097/6 "2021-08-18T18:21:35Z")

</div>

I see. Thanks for the explanation.

---

<div class="post-metadata">

### Author: ![system](https://community.mp3tag.de/uploads/default/original/2X/c/ce7035d426cb755a7916793326d23b465222a407.png) [@system](https://community.mp3tag.de/u/system)
#### Post date: [September 17, 2021, 6:21pm UTC](https://community.mp3tag.de/t/if-inserts-a-space-if-the-metadata-field-is-blank-while-renaming-folders-based-on-song-data-how-to-avoid-or-remove-during-renaming/54097/7 "2021-09-17T18:21:40Z")

</div>

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