Check that file exists: Difference between revisions

+Stata
(+Stata)
Line 1,991:
OS.FileSys.access ("/input.txt", []);
OS.FileSys.access ("/docs", []);</lang>
 
=={{header|Stata}}==
Mata has functions to check the existence of files and directories:
<lang stata>mata
fileexists("input.txt")
direxists("docs")
end</lang>
 
It's not as straightforward in Stata's macro language. For files, use [http://www.stata.com/help.cgi?confirm confirm]. Since it throws an error when the file does not exist, use [http://www.stata.com/help.cgi?capture capture] and check [http://www.stata.com/help.cgi?_variables _rc] afterwards.
 
<lang stata>capture confirm file input.txt
if !rc {
* do something if the file exists
}</lang>
 
It's not possible to check existence of a directory with confirm. One may use the [https://ideas.repec.org/c/boc/bocode/s435507.html confirmdir] package from SSC. The confirmdir command saves the current directory, then tries to chdir to the directory to test (with capture to prevent an error). Then the value of _rc is put in a [http://www.stata.com/help.cgi?return stored result]. Example of use:
 
<lang stata>confirmdir docs
if !r(confirmdir) {
* do something if the directory exists
}</lang>
 
=={{header|Tcl}}==
1,336

edits