Community Forum

How do I check if a file exists?

Forum Overview >> Scripting

CategoryScripting
Created08.12.2021 22:09


Svyatchenko Yuri (Berrod) 08.12.2021 22:09
Please tell me how to check if a file exists in the specified directory?
"xmlFilename" - is the path to the file
If the file does not exist, then the function call
loadXMLFile ("TempConfig", xmlFilename)
the log will display "Error: Failed to open xml file ..."
I don't want this message to be logged, i.e. before calling, you need to check for the existence of the file.

The function below does not solve the problem:

-local function file_exists(xmlFilename)
----local f=io.open(xmlFilename)
----if f~=nil then
--------io.close(f)
--------return true
----else
--------return false
----end
-end

After using such a function, it writes to the log:
"Warning: io.open, only write mode ('w') is allowed"

Unfortunately, I didn't find anything in the documentation. Thanks.

Espen K. (estyx) 08.12.2021 23:29
---@param path string
---@return boolean
function fileExists(path) end

Svyatchenko Yuri (Berrod) 09.12.2021 06:41
Thank you very much, Espen K. Indeed, there is such a global function.


Note: Log in to post. Create a new account here.