Community discussions

MikroTik App
 
User avatar
mozerd
Forum Veteran
Forum Veteran
Topic Author
Posts: 891
Joined: Thu Oct 05, 2017 3:39 pm
Location: Canada
Contact:

Bug in LEN

Mon Jul 02, 2018 6:43 pm

[brook@brookcity] > /file print
 # NAME                                             TYPE                                                  SIZE CREATION-TIME       
 0 skins                                            directory                                                  dec/31/1969 20:00:03
 1 disk1                                            disk                                                       jan/01/1970 20:00:15
 2 auto-before-reset.backup                         backup                                             11.7KiB jan/01/1970 20:07:05
 3 pub                                              directory                                                  feb/09/2018 17:31:42
 4 disk2                                            disk                                                       may/13/2018 21:56:51
 5 disk2/wsipdiff.txt                               .txt file                                                0 jun/30/2018 09:00:55
 6 disk2/wsiptik.rsc                                script                                            139.7KiB jun/30/2018 09:00:55
 7 disk2/fileiptik.rsc                              script                                           1292.1KiB jun/30/2018 09:00:56
 8 disk2/fileipdiff.txt                             .txt file                                         120.7KiB jun/30/2018 09:00:56
 9 disk1/log.0.txt                                  .txt file                                           9.6KiB jun/30/2018 09:01:54
10 disk1/log                                        directory                                                  dec/31/2017 10:43:10
[brook@brookcity] > :if ([:len [/file find name=disk2/wsipdiff.txt]] =0) do={ :put "value is True" } else={ :put "value is False"} 
value is False
[brook@brookcity] > :if ([:len [/file find name=disk2/wsipdiff.txt]] >0) do={ :put "value is True" } else={ :put "value is False"} 
value is True
[brook@brookcity] >
From the above you can see that the file wsipdiff.txt whose size is 0 the script testing for =0 and >0 both of which evaluate incorrectly

I have submitted a bug report to support but they have not as yet responded.
I would not trust using ‘len’
 
User avatar
dasiu
Trainer
Trainer
Posts: 231
Joined: Fri Jan 30, 2009 11:41 am
Location: Reading, UK
Contact:

Re: Bug in LEN  [SOLVED]

Tue Jul 03, 2018 12:43 am

Your report is incorrect - it's NOT a BUG :). It's good, normal behaviour.
1. :len - returns not a "file size", but a "size of the variable".
2. /file find... - returns an ARRAY of POINTERS to files that match the "find" criteria. In your case /file find name=""... returns one pointer - so the length of the array is 1 (one file found). That's what :len is showing.
3. To get the size you need to use "/file get ... size" - but only if you're sure that the file exists.
So:
:if ([:len [/file find name=disk2/wsipdiff.txt]] >0) do={
  :if ([/file get disk2/wsipdiff.txt size] > 0) do={
    :put "The file is not empty."
  } else={
    :put "The file is empty."
  }
} else={
  :put "The file does not exist!"
}
 
User avatar
mozerd
Forum Veteran
Forum Veteran
Topic Author
Posts: 891
Joined: Thu Oct 05, 2017 3:39 pm
Location: Canada
Contact:

Re: Bug in LEN

Tue Jul 03, 2018 12:48 pm

Your report is incorrect - it's NOT a BUG :). It's good, normal behaviour.
1. :len - returns not a "file size", but a "size of the variable".
2. /file find... - returns an ARRAY of POINTERS to files that match the "find" criteria. In your case /file find name=""... returns one pointer - so the length of the array is 1 (one file found). That's what :len is showing.
3. To get the size you need to use "/file get ... size" - but only if you're sure that the file exists.
@dasiu, THANK YOU very much for correcting me and providing the correct methodology.

Who is online

Users browsing this forum: Jonty, saied and 22 guests