I have to rewrite the next URL:

http://example.com/folder/subfolder/filename

to

http://example.com/folder/subfolder/filename.txt

I have attempted the next:

RewriteRule ^/folder/subfolder/(.*)(|/)$  /folder/subfolder/$1.txt      [R=302,L]

However this will make to rewrites get into a loop and so i can get:

/folder/subfolder/filename.txt.txt.txt.txt (etc)

Any idea steps to make my rewrite rule work?

You'll need a rewrite condition:

RewriteCond %{REQUEST_URI} !\.txt$

prior to the rule. I am unable to test this only at that moment, but I am sure, this is exactly what you'll need.