Check that file exists: Difference between revisions

Content added Content deleted
(added ruby)
Line 5: Line 5:
=={{header|Ada}}==
=={{header|Ada}}==
This example should work with any Ada 95 compiler.
This example should work with any Ada 95 compiler.
<ada>with Ada.Text_IO; use Ada.Text_IO;
<ada>

with Ada.Text_IO; use Ada.Text_IO;
procedure File_Exists is
function Does_File_Exist (Name : String) return Boolean is
procedure File_Exists is
The_File : Ada.Text_IO.File_Type;
function Does_File_Exist (Name : String) return Boolean is
begin
The_File : Ada.Text_IO.File_Type;
Open (The_File, In_File, Name);
begin
Open (The_File, In_File, Name);
Close (The_File);
Close (The_File);
return True;
exception
return True;
when Name_Error =>
exception
when Name_Error =>
return False;
end Does_File_Exist;
return False;
begin
end Does_File_Exist;
Put_Line (Boolean'Image (Does_File_Exist ("input.txt" )));
begin
Put_Line (Boolean'Image (Does_File_Exist ("input.txt" )));
Put_Line (Boolean'Image (Does_File_Exist ("\input.txt")));
end File_Exists;</ada>
Put_Line (Boolean'Image (Does_File_Exist ("\input.txt")));
end File_Exists;
</ada>
This example should work with any Ada 2005 compiler.
This example should work with any Ada 2005 compiler.
<ada>with Ada.Text_IO; use Ada.Text_IO;
<ada>
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Directories; use Ada.Directories;

with Ada.Directories; use Ada.Directories;
procedure File_Exists is
procedure File_Exists is
procedure Print_File_Exist (Name : String) is
begin
procedure Print_File_Exist (Name : String) is
Put_Line ("Does " & Name & " exist? " &
begin
Put_Line ("Does " & Name & " exist? " &
Boolean'Image (Exists (Name)));
end Print_File_Exist;
Boolean'Image (Exists (Name)));
procedure Print_Dir_Exist (Name : String) is
end Print_File_Exist;
begin
procedure Print_Dir_Exist (Name : String) is
Put_Line ("Does directory " & Name & " exist? " &
begin
Boolean'Image (Exists (Name) and then Kind (Name) = Directory));
Put_Line ("Does directory " & Name & " exist? " &
end Print_Dir_Exist;
Boolean'Image (Exists (Name) and then Kind (Name) = Directory));
begin
end Print_Dir_Exist;
Print_File_Exist ("input.txt" );
begin
Print_File_Exist ("input.txt" );
Print_File_Exist ("/input.txt");
Print_File_Exist ("/input.txt");
Print_Dir_Exist ("docs");
Print_Dir_Exist ("docs");
Print_Dir_Exist ("/docs");
end File_Exists;</ada>
Print_Dir_Exist ("/docs");
end File_Exists;
</ada>


=={{header|C}}==
=={{header|C}}==
{{works with|POSIX}}
#include <sys/types.h>
#include <sys/stat.h>
<c>#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <unistd.h>
int main(){
int main(){
struct stat file_stat;
struct stat file_stat;
if(stat("/tmp/test",&file_stat) ==0)
if(stat("/tmp/test",&file_stat) ==0)
Line 58: Line 55:
else
else
printf("no file lol");
printf("no file lol");
return 0;
}
}</c>


=={{header|Clojure}}==
=={{header|Clojure}}==
Line 122: Line 120:


=={{header|Java}}==
=={{header|Java}}==
import java.io.File;
<java>import java.io.File;
public class FileExistsTest {
public class FileExistsTest {
public static boolean isFileExists(String filename) {
public static boolean isFileExists(String filename) {
boolean exists = new File(filename).exists();
boolean exists = new File(filename).exists();
return exists;
return exists;
}
}
public static void test(String type, String filename) {
public static void test(String type, String filename) {
System.out.println("The following " + type + " called " + filename +
System.out.println("The following " + type + " called " + filename +
(isFileExists(filename) ? " exists." : " not exists.")
(isFileExists(filename) ? " exists." : " not exists.")
);
);
}
}
public static void main(String args[]) {
public static void main(String args[]) {
test("file", "input.txt");
test("file", "input.txt");
test("file", File.separator + "input.txt");
test("file", File.separator + "input.txt");
test("directory", "docs");
test("directory", "docs");
test("directory", File.separator + "docs" + File.separator);
test("directory", File.separator + "docs" + File.separator);
}
}
}</java>
}


=={{header|MAXScript}}==
=={{header|MAXScript}}==
Line 150: Line 148:


=={{header|OCaml}}==
=={{header|OCaml}}==
Sys.file_exists "input.txt";;
<ocaml>Sys.file_exists "input.txt";;
Sys.file_exists "docs";;
Sys.file_exists "docs";;
Sys.file_exists "/input.txt";;
Sys.file_exists "/input.txt";;
Sys.file_exists "/docs";;
Sys.file_exists "/docs";;</ocaml>


=={{header|PHP}}==
=={{header|PHP}}==
if (file_exists('input.txt')) echo 'input.txt is here right by my side';
<php>if (file_exists('input.txt')) echo 'input.txt is here right by my side';
if (file_exists('docs' )) echo 'docs is here with me';
if (file_exists('docs' )) echo 'docs is here with me';
if (file_exists('/input.txt)) echo 'input.txt is over there in the root dir';
if (file_exists('/input.txt)) echo 'input.txt is over there in the root dir';
if (file_exists('/docs' )) echo 'docs is over ther in the root dir';
if (file_exists('/docs' )) echo 'docs is over ther in the root dir';</php>


=={{header|Perl}}==
=={{header|Perl}}==
use File::Spec::Functions qw(catfile rootdir);
<perl>use File::Spec::Functions qw(catfile rootdir);
# here
# here
print -e 'input.txt';
print -e 'input.txt';
print -d 'docs';
print -d 'docs';
# root dir
# root dir
print -e catfile rootdir, 'input.txt';
print -e catfile rootdir, 'input.txt';
print -d catfile rootdir, 'docs';
print -d catfile rootdir, 'docs';</perl>


'''Without a Perl Module'''
'''Without a Perl Module'''
Line 220: Line 218:
The ''os.path.exists'' method will return True if a path exists False if it does not.
The ''os.path.exists'' method will return True if a path exists False if it does not.


<python>
<python>import os

import os
os.path.exists("input.txt")
os.path.exists("input.txt")
os.path.exists("/input.txt")
os.path.exists("/input.txt")
os.path.exists("docs")
os.path.exists("docs")
os.path.exists("/docs")</python>
os.path.exists("/docs")
</python>


=={{header|Raven}}==
=={{header|Raven}}==
Line 235: Line 231:
'docs' isdir if 'docs exists and is a directory' print
'docs' isdir if 'docs exists and is a directory' print
'/docs' isdir if '/docs exists and is a directory' print
'/docs' isdir if '/docs exists and is a directory' print

=={{header|Ruby}}==
<ruby>File.exist?("input.txt")
File.exist?("/input.txt")
File.exist?("docs")
File.exist?("/docs")</ruby>


=={{header|Smalltalk}}==
=={{header|Smalltalk}}==