PDA

View Full Version : File.DoesExist() False Positive



Jason Pate
04-12-2007, 12:23 PM
Some people might pick this one apart but here is the deal, if you pass a variable into the File.DoesExist() and it is equal to a valid folder (NO FILE) it returns "true". I would like to summit this is wrong since there is “no file it must be false”, if I wanted to test for a folder I would use Folder.DoesExist.

Maybe an option in the File.DoesExist() function for "must be a file" = true/false or something. I have a pick box but if user types in a folder with no file its testing good because the folder is valid, but I crashing later in the script because there did not input a file. I think I can code around this for now suggests are always welcome, but I would summit this to IR as a bug.

Eagle
04-13-2007, 09:26 PM
try this 'one way to do' it test workaround:
(I believe the 'issue' is NTFS file system related)

place function in Globals if ya want


function Is_aValidFile(sIn)
--[[attempt to verify target is 'valid' and is of type: 'file'
notes:
- 'folders' on 'ntfs' formatted 'storage devices\sources' are in reality 'place holder' files under the hood
- ( which is why several 'file' related actions can work on 'ntfs' 'folders' - and do not work on 'fat32' and ealier)
- a valid 'folder' should return a 'file size' of: 0 bytes ]]

if sIn then
if (not Folder.DoesExist(sIn)) then
local nsize = File.GetSize(sIn); -- 'file' must be greater 0 bytes
local nerror = Application.GetLastError();
if ((nsize > 0) and (nerror == 0)) then
return true; --is valid and of type: 'file'
end
end
end

return false; --is of type 'folder' or an error occurred

end


to call the function test calls - place below where suitable



--Example diagnostic test call(s)

--(manually create in: yourlocaltempfolder\folder\file structure as below - then run a few test calls

local sTarget_fl = _TempFolder.."\\test.txt\\test.txt";--try this one(make sure the 'real file' is > 0 bytes)
--local sTarget_fl = _TempFolder.."\\test.txt"; --now try this one(make sure is a 'real folder')

if (not Is_aValidFile(sTarget_fl)) then
--an error occurred or in theory is of type: 'folder'
--do some stuff here - test eg:
Dialog.Message("File validation: Failed", sTarget_fl);
else
Dialog.Message("File validation: Passed", sTarget_fl);
end


tweak to suit once you are satisfied is reliable enough for your purposes

Lorne
04-16-2007, 09:35 AM
Note that checking the size will give false positives on actual 0-byte files, which are fairly common.

Jason Pate
04-16-2007, 01:04 PM
Eagle,

Thanks for the reply I see what your doing, I like, and a good idea, I reworked my installer to reduce the change of this happening, I might add your code as well really prevent it, Thanks for the sample.

Lorne,

Thanks for the added info. File should never be 0 k (famous last words).