Exporting IP addresses from an address-list to a file

Hi, I have this script that exports the IP addresses of this list to a file.


/ip firewall address-list
:foreach id in=[find list=servers] do={
    :local ipAddr [:toip [get $id address]] 
    :local filenameIP "servers100.txt"
    /file
    :if ([:len [find where name="flash" and type="disk"]] = 1) do={:set filenameIP "flash/$filenameIP"}
    :if ([:len [find where name=$filenameIP]] = 0) do={print file="$filenameIP"; :delay 2s; set $filenameIP contents=""}
    :local filecontent [get $filenameIP contents]
    :local newfilecontent "$ipAddr\r\n$filecontent"
    set $filenameIP contents=$newfilecontent; :delay 1s
}

But I have a problem and if I run the script again it adds ALL the IPs again, I would like please to indicate me an “IF” condition to compare the “address-list” with the content of the file and only add the new IPs that are not in the file.

Is this possible? Thank you.

Can I search for content within a file?

/ip firewall address-list
:foreach id in=[find list=servers] do={
    :local ipAddr [:toip [get $id address]] 
    :local filenameIP "servers100.txt"
    /file
    :if ([:len [find where name="flash" and type="disk"]] = 1) do={:set filenameIP "flash/$filenameIP"}
    :if ([:len [find where name=$filenameIP]] = 0) do={print file="$filenameIP"; :delay 2s; set $filenameIP contents=""}
    :local filecontent [get $filenameIP contents]
    
    :foreach id2 in=[$filecontent] do={
        :if ((($ipAddr in $id2) or ($ipAddr = $id2)) = 0) do={
        >>>>> NOT WORKING <<<<<<<    
        }  
    }
    <
    :local newfilecontent "$ipAddr\r\n$filecontent"
    set $filenameIP contents=$newfilecontent; :delay 1s
}