Script for CSV DHCP export

Hello, New to scripting and been trying to work it out on my own but need a hand.
I have put together the below script and tried other variants but I can only get 1 of the variables to pull at a time and export. doesnt matter if its address, mac-address, server etc. The min I add more variables it creates a file that shows the current file structure and not the output.
If I try multiple :local address :local mac etc it will not even create a file.
Open for my noob ideas to be fixed. 2 versions below I have played with.

:local output ""
:local filename "dhcp-leases.txt"
:local entry
/ip dhcp-server lease
:foreach i in=[find where server=fttx-public-dhcp] do={
    :set entry ([get $i address].",".[get $i mac-address].",".[get $i client-id].",".[get $i server])

    :set output ($output . "$entry\r\n")
}
/file print file=$filename
:delay 2s
/file set $filename contents=$output



:local filename "dhcp-leases.txt"
/file set $filename contents=""

/ip dhcp-server lease
:foreach i in=[find where server=fttx-public-dhcp] do={
    :local address [get $i address]
    :local mac [get $i mac-address]
    :local client_id [get $i client-id]
    :local server [get $i server]
    :local entry "$address,$mac,$client_id,$server"
    
    :put ("$entry")
    /file print file=$filename

file is limited to 4KB, if you go over that limit 1st script do not work.

do not use the var name like the named item in the context..

the second script is ALSO full of logical errors…

:execute script=“/ip dhcp-server lease\r
\n:foreach i in=[find where server="fttx-public-dhcp"] do={\r
\n :local add [get $i address]\r
\n :local mac [get $i mac-address]\r
\n :local cid [get $i client-id]\r
\n :local svr [get $i server]\r
\n :local line "\"$add\",\"$mac\",\"$cid\",\"$svr\""\r
\n :put $line\r
\n}\r
\n” file=“dhcp-leases.txt”