Check that file exists: Difference between revisions

m (Adjusted text formatting issues.)
 
(7 intermediate revisions by 6 users not shown)
Line 821:
ENTER FILENAME TO CHECK? NOFILE.DOC
'NOFILE.DOC' IS NOT HERE.</pre>
 
==={{header|SmallBASIC}}===
<syntaxhighlight lang="qbasic">
if(isdir("docs")) then print "directory docs exist"
if(isdir("/docs")) then print "directory /docs exist"
if(isfile("input.txt")) then print "file input.txt exist"
if(isfile("/input.txt")) then print "file /input.txt exist"
s" /docs" .exists</syntaxhighlight>
 
 
 
=={{header|BASIC256}}==
<syntaxhighlight lang="basic256">subroutine opener (filename$)
Line 967 ⟶ 978:
 
===Using C++ 17===
C++ 17 contains a Filesystem library which significantly improves manipulationsoperations with files.
<syntaxhighlight lang="c++">
 
Line 987 ⟶ 998:
 
int main() {
file_exists("C:/input.txt");
file_exists("C:/zero_length.txt");
file_exists("C:/docs/input2input.txt");
file_exists("C:/docs/zero_length2zero_length.txt");
}
</syntaxhighlight>
{{ out }}
</pre>
"C:/input.txt" exists with a file size of 11 bytes.
 
"C:/zero_length.txt" exists with a file size of 0 bytes.
 
"C:/docs/input2input.txt" exists with a file size of 11 bytes.
 
"C:/docs/zero_length2zero_length.txt" exists with a file size of 0 bytes.
</pre>
 
Line 1,377 ⟶ 1,388:
=={{header|Forth}}==
 
<syntaxhighlight lang="forth">: .exists ( str len -- ) 2dup file-status nip 0= if ." exists" else ." does not exist" then type ;
2dup file-status nip 0= if
s" input.txt" .exists
s" /input .txt" .exists: "
else
s" docs" .exists
." does not exist: "
s" /docs" .exists</syntaxhighlight>
then
type
;
 
s" input.txt" .exists cr
s" /input.txt" .exists cr
s" docs" .exists cr
s" /docs" .exists cr
</syntaxhighlight>
 
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
Line 1,923 ⟶ 1,944:
[[io]]::fp.existsFile($dir2)
[[io]]::fp.closeFile($dir2)
</syntaxhighlight>
 
=={{header|langur}}==
The prop() function returns a hash of file/directory properties.
<syntaxhighlight lang="langur">val .printresult = impure fn(.file) {
write .file, ": "
if val .p = prop(.file) {
if .p'isdir {
writeln "is directory"
} else {
writeln "is file"
}
} else {
writeln "nothing"
}
}
 
.printresult("input.txt")
.printresult("/input.txt")
.printresult("docs")
.printresult("/docs")
</syntaxhighlight>
 
Line 2,758 ⟶ 2,800:
0
</syntaxhighlight>
=={{header|RPL}}==
The 2 functions below take a word as an argument and return a boolean stating the presence or not of a 'file' (a 'variable' in RPL jargon) or a directory corresponding to the word.
« VARS SWAP POS
» '<span style="color:blue">ISHERE?</span>' STO
« PATH HOME
VARS ROT POS
SWAP EVAL <span style="color:grey">@ Back to initial directory</span>
» '<span style="color:blue">ISHOME?</span>' STO
 
=={{header|Ruby}}==
<code>File.exist?</code> only checks if a file exists; it can be a regular file, a directory, or something else. <code>File.file?</code> or <code>File.directory?</code> checks for a regular file or a directory. Ruby also allows <code>FileTest.file?</code> or <code>FileTest.directory?</code>.
Line 3,180 ⟶ 3,232:
 
Since in Linux an ''empty'' directory has a size of 4K bytes, we check the number of files it contains to confirm that it's empty.
<syntaxhighlight lang="ecmascriptwren">import "io" for Directory, File
 
for (name in ["input.txt", "`Abdu'l-Bahá.txt"]) {
Line 3,205 ⟶ 3,257:
docs directory exists and contains 0 files.
</pre>
 
=={{header|XPL0}}==
Attempting to open a non-existent file or directory will cause an error.
885

edits