Folder tags disappearing

I have a little routine that renames some files in a folder selected by the user. Curiously, if the folder has a tag before the renaming, it’s gone after the renaming! The folder itself isn’t being changed or renamed at all - just the contents. It’s not a massive problem but I’m curious as to why this might be happening. Any ideas?

I’m running 10.9.5 btw.

Could it be that you’re also renaming the hidden .DS_Store in the folder? Maybe that’s storing the tags and by renaming it, you’ll take that information away.

No. Just renaming some audio files.

Tags are not saved in .DS_Store.
They are saved in an extendet attribute “com.apple.metadata:_kMDItemUserTags”, look via

xattr -l FILE.

How about a file system where no extended attributes are supported (e.g. FAT formatted USB stick)? In that case there may something in an “.NAME" file next to the “NAME” item.
Do you by accident rename these ".
” files, too?