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> |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
Open (The_File, In_File, Name); |
|||
⚫ | |||
Close (The_File); |
|||
return True; |
|||
⚫ | |||
⚫ | |||
when Name_Error => |
|||
⚫ | |||
return False; |
|||
⚫ | |||
return False; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
Put_Line (Boolean'Image (Does_File_Exist ("\input.txt"))); |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
This example should work with any Ada 2005 compiler. |
This example should work with any Ada 2005 compiler. |
||
⚫ | |||
<ada> |
|||
with Ada.Directories; use Ada.Directories; |
|||
⚫ | |||
⚫ | |||
procedure Print_File_Exist (Name : String) is |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
Boolean'Image (Exists (Name))); |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
procedure Print_Dir_Exist (Name : String) is |
|||
Put_Line ("Does directory " & Name & " exist? " & |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
Boolean'Image (Exists (Name) and then Kind (Name) = Directory)); |
|||
⚫ | |||
⚫ | |||
Print_File_Exist ("input.txt" ); |
|||
⚫ | |||
Print_File_Exist ("/input.txt"); |
|||
Print_Dir_Exist ("docs"); |
|||
Print_Dir_Exist ("/docs"); |
|||
end File_Exists;</ada> |
|||
Print_Dir_Exist ("/docs"); |
|||
⚫ | |||
</ada> |
|||
=={{header|C}}== |
=={{header|C}}== |
||
{{works with|POSIX}} |
|||
⚫ | |||
<c>#include <sys/types.h> |
|||
#include <sys/stat.h> |
|||
⚫ | |||
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"); |
||
⚫ | |||
} |
|||
⚫ | |||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
Line 122: | Line 120: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
<java>import java.io.File; |
|||
public class FileExistsTest { |
|||
public static boolean isFileExists(String filename) { |
|||
boolean exists = new File(filename).exists(); |
|||
return exists; |
|||
} |
|||
public static void test(String type, String filename) { |
|||
System.out.println("The following " + type + " called " + filename + |
|||
(isFileExists(filename) ? " exists." : " not exists.") |
|||
); |
|||
} |
|||
public static void main(String args[]) { |
|||
test("file", "input.txt"); |
|||
test("file", File.separator + "input.txt"); |
|||
test("directory", "docs"); |
|||
test("directory", File.separator + "docs" + File.separator); |
|||
} |
|||
}</java> |
|||
} |
|||
=={{header|MAXScript}}== |
=={{header|MAXScript}}== |
||
Line 150: | Line 148: | ||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
<ocaml>Sys.file_exists "input.txt";; |
|||
Sys.file_exists "docs";; |
|||
Sys.file_exists "/input.txt";; |
|||
Sys.file_exists "/docs";;</ocaml> |
|||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
<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('/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';</php> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
<perl>use File::Spec::Functions qw(catfile rootdir); |
|||
# here |
|||
print -e 'input.txt'; |
|||
print -d 'docs'; |
|||
# root dir |
|||
print -e catfile rootdir, 'input.txt'; |
|||
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("docs") |
|||
os.path.exists("/docs")</python> |
|||
⚫ | |||
</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}}== |