Is there a way to find out the length of a string in NSIS?
I am trying to test if a file is empty(has no contents). One way is to read the file and store the contents in a string(called contentStr) then see how long that contentStr string is. If its > 0 then its not empty.
The other method is to check if contentStr == "" but as you can see below it doesn't work. Any empty file never returns 1 when it should:
!macro IsFileEmpty fName res
!insertmacro ReadFile "${fName}" ${res}
StrCmp ${res} "" +1 +2
IntOp ${res} 1 + 0
IntOp ${res} 0 + 0
!macroend
It is a little bit weird to do it this way and it only partially works:
...
StrCmp ${res} "" 0 +2 ; +1 and 0 is the same, jumps to next instruction
StrCpy ${res} 1 ; No need to do math here
IntOp ${res} ${res} + 0 ; NaN + 0 = 0 but what if you read a number from the file?
If the file might start with a number you need to jump like zbynour suggested:
...
StrCmp ${res} "" 0 +3
StrCpy ${res} 1
Goto +2
StrCpy ${res} 0
If you flip the test you can get what you want with the same number of instructions:
!macro IsFileNOTEmpty fName res
!insertmacro ReadFile "${fName}" ${res}
StrCmp ${res} "" +2 0
StrCpy ${res} 1 ; Or IntOp ${res} ${res} | 1 if you really wanted to do extra math ;)
IntOp ${res} ${res} + 0
!macroend
or even better:
!macro IsFileEmpty fName res
!insertmacro ReadFile "${fName}" ${res}
StrLen ${res} ${res} ; If this is enough depends on how you want to deal with newlines at the start of the file (\r or \n)
!macroend
...all of this code assumes you want to test if the file starts with some text, if the file starts with a 0 byte it will always say the file is empty. So if the file has binary content you need to use Francisco R's code that tests the actual size in bytes.