Check that file exists: Difference between revisions

m (→‎{{header|Ruby}}: File.exists is deprecated)
 
(17 intermediate revisions by 10 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"
</syntaxhighlight>
 
 
 
=={{header|BASIC256}}==
<syntaxhighlight lang="basic256">subroutine opener (filename$)
Line 904 ⟶ 915:
return 0;
}</syntaxhighlight>
 
{{libheader|Gadget}}
<syntaxhighlight lang="c">
#include <gadget/gadget.h>
 
LIB_GADGET_START
 
/* input.txt = check_file.c
docs = tests */
 
Main
Print "tests/check_file.c is a regular file? %s\n", Exist_file("tests/check_file.c") ? "yes" : "no";
Print "tests is a directory? %s\n", Exist_dir("tests") ? "yes" : "no";
Print "some.txt is a regular file? %s\n", Exist_file("some.txt") ? "yes" : "no";
Print "/tests is a directory? %s\n", Exist_dir("/tests") ? "yes" : "no";
End
</syntaxhighlight>
{{out}}
<pre>
tests/check_file.c is a regular file? yes
tests is a directory? yes
some.txt is a regular file? no
/tests is a directory? no
 
</pre>
 
=={{header|C sharp|C#}}==
 
Line 939 ⟶ 976:
testfile("/docs");
}</syntaxhighlight>
 
===Using C++ 17===
C++ 17 contains a Filesystem library which significantly improves operations with files.
<syntaxhighlight lang="c++">
 
#include <iostream>
#include <filesystem>
 
void file_exists(const std::filesystem::path& path) {
std::cout << path;
if ( std::filesystem::exists(path) ) {
if ( std::filesystem::is_directory(path) ) {
std::cout << " is a directory" << std::endl;
} else {
std::cout << " exists with a file size of " << std::filesystem::file_size(path) << " bytes." << std::endl;
}
} else {
std::cout << " does not exist" << std::endl;
}
}
 
int main() {
file_exists("input.txt");
file_exists("zero_length.txt");
file_exists("docs/input.txt");
file_exists("docs/zero_length.txt");
}
</syntaxhighlight>
{{ out }}
</pre>
"input.txt" exists with a file size of 11 bytes.
 
"zero_length.txt" exists with a file size of 0 bytes.
 
"docs/input.txt" exists with a file size of 11 bytes.
 
"docs/zero_length.txt" exists with a file size of 0 bytes.
</pre>
 
=={{header|Clojure}}==
<syntaxhighlight lang="clojure">
Line 1,312 ⟶ 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,633 ⟶ 1,719:
direxist '/docs'</syntaxhighlight>
=={{header|Java}}==
This can be done with a <code>File</code> object.<br />
<syntaxhighlight lang="java">
new File("docs/input.txt").exists();
new File("/docs/input.txt").exists();
</syntaxhighlight>
Zero-length files are not a problem, and return as existent.<br />
Java supports UTF-16, so the unusual file name is not a problem.
<syntaxhighlight lang="java">
new File("`Abdu'l-Bahá.txt").exists()
</syntaxhighlight>
<syntaxhighlight lang="java">
new File("`Abdu'l-Bah\u00E1.txt").exists();
</syntaxhighlight>
<br />
Alternately
<syntaxhighlight lang="java">import java.io.File;
public class FileExistsTest {
Line 1,672 ⟶ 1,773:
}
}</syntaxhighlight>
 
=={{header|JavaScript}}==
Javascript interpreters are now widely embedded in contexts which do have access to file systems, but the early context of browser scripting has precluded the inclusion of file system libraries in the definition of the language itself.
Line 1,818 ⟶ 1,920:
{{VI snippet}}<br/>
[[File:Ensure_that_a_file_exists.png]]
 
 
=={{header|Lang}}==
{{libheader|lang-io-module}}
<syntaxhighlight lang="lang">
# Load the IO module
# Replace "<pathToIO.lm>" with the location where the io.lm Lang module was installed to without "<" and ">"
ln.loadModule(<pathToIO.lm>)
 
$file1 = [[io]]::fp.openFile(input.txt)
[[io]]::fp.existsFile($file1)
[[io]]::fp.closeFile($file1)
 
$file2 = [[io]]::fp.openFile(/input.txt)
[[io]]::fp.existsFile($file2)
[[io]]::fp.closeFile($file2)
 
$dir1 = [[io]]::fp.openFile(docs)
[[io]]::fp.existsFile($dir1)
[[io]]::fp.closeFile($dir1)
 
$dir2 = [[io]]::fp.openFile(/docs)
[[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>
 
=={{header|Lasso}}==
<syntaxhighlight lang="lasso">// local file
Line 2,409 ⟶ 2,558:
Bdos Err on A: Select
</pre>
 
=={{header|Plain English}}==
{{libheader|Plain English-output}}
<syntaxhighlight lang="text">
To run:
Start up.
\ In the current working directory
Check that ".\input.txt" is in the file system.
Check that ".\docs\" is in the file system.
\ In the filesystem root
Check that "C:\input.txt" is in the file system.
Check that "C:\docs\" is in the file system.
Wait for the escape key.
Shut down.
 
To check that a path is in the file system:
If the path is in the file system, write the path then " exists" to the output; exit.
If the path is not in the file system, write the path then " does not exist" to the output; exit.
</syntaxhighlight>
 
=={{header|Pop11}}==
 
Line 2,631 ⟶ 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,053 ⟶ 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,078 ⟶ 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