Check that file exists: Difference between revisions
Line 193: | Line 193: | ||
In GNU Smalltalk instead you can do: |
In GNU Smalltalk instead you can do: |
||
(Directory name: 'docs') exists ifTrue: [ ... ] |
|||
(Directory name: 'c:\docs') exists ifTrue: [ ... ] |
(Directory name: 'c:\docs') exists ifTrue: [ ... ] |
||
(File name: 'serial') isFile ifTrue: [ ... ] |
|||
(File name: 'c:\serial') isFile ifTrue: [ ... ] |
(File name: 'c:\serial') isFile ifTrue: [ ... ] |
||
Using ''exists'' in the |
Using ''exists'' in the third and fourth case will return true for directories too. |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
Revision as of 16:20, 13 February 2008
You are encouraged to solve this task according to the task description, using any language you may know.
In this task, the job is to verify that a file called "input.txt" and the directory called "docs" exist. This should be done twice: once for the current working directory and once for a file and a directory in the filesystem root.
Ada
This example should work with any ada compiler. It has been tested with Gnat 3.15p
with ada.text_io; use ada.text_io; procedure fileExists is function file_exists( name : string ) return boolean is the_file:Ada.Text_IO.file_type; begin open(the_file, in_file, name); close( the_file); return true; exception when name_error => return false; end file_exists; procedure putBool(bool: boolean) is begin if (bool) then put_line ("true"); else put_line ("false"); end if; end; begin putBool( file_exists("input.txt" ) ); putBool( file_exists("\input.txt") ); end;
C
#include <sys/types.h> #include <sys/stat.h> #include <unistd.h> int main(){ struct stat file_stat; if(stat("/tmp/test",&file_stat) ==0) printf("file exists"); else printf("no file lol"); }
DOS Batch File
if exist input.txt echo The following file called input.txt exists. if exist \input.txt echo The following file called \input.txt exists. if exist docs echo The following directory called docs exists. if exist \docs\ echo The following directory called \docs\ exists.
Forth
: .exists ( str len -- ) 2dup file-status nip 0= if type ." exists" else type ." does not exist" then ; s" input.txt" .exists s" /input.txt" .exists s" docs" .exists s" /docs" .exists
Java
import java.util.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.seperator + "input.txt"); test("directory", "docs"); test("directory", File.seperator + "docs" + File.seperator); } }
Haskell
import System.IO import System.Directory check :: (FilePath -> IO Bool) -> FilePath -> IO () check p s = do result <- p s putStrLn $ s ++ if result then " does exist" else " does not exist" main = do check doesFileExist "input.txt" check doesDirectoryExist "docs" check doesFileExist "/input.txt" check doesDirectoryExist "/docs"
MAXScript
-- Here doesFileExist "input.txt" (getDirectories "docs").count == 1 -- Root doesFileExist "\input.txt" (getDirectories "C:\docs").count == 1
OCaml
Sys.file_exists "input.txt";; Sys.file_exists "docs";; Sys.file_exists "/input.txt";; Sys.file_exists "/docs";;
PHP
// List of files. $files = array('./input.txt','./docs','/input.txt','/docs'); // Process Each File foreach($files as $fileToCheck){ // List Findings print "The file "; if( file_exists($fileToCheck) ){ print $fileToCheck." exists.\n"; }else{ print $fileToCheck." does not exist.\n"; } }
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';
Without a Perl Module
A 1 is printed if the file or dir exists.
perl -e 'print -e "./input.txt", "\n";' perl -e 'print -d "./docs", "\n";' perl -e 'print -e "/input.txt", "\n";' perl -e 'print -d "/docs", "\n";'
Pop11
sys_file_exists('input.txt') => sys_file_exists('/input.txt') => sys_file_exists('docs') => sys_file_exists('/docs') =>
Note that the above literally checks for existence. Namely sys_file_exists returns true if file exists but can not be read.
The only sure method to check if file can be read is to try to open it. If one just wants to check if file is readable the following may be useful:
;;; Define an auxilary function, returns boolean define file_readable(fname); lvars f = sysopen(fname, 0, true, `A`); if f then sysclose(f); return (true); else return (false); endif; enddefine;
PowerShell
Test-Path input.txt
Python
Interpreter: Python 2.5
The os.path.exists method will return True if a path exists False if it does not.
import os os.path.exists("input.txt") os.path.exists("/input.txt") os.path.exists("docs") os.path.exists("/docs")
Raven
'input.txt' exists if 'input.txt exists' print '/input.txt' exists if '/input.txt exists' print 'docs' isdir if 'docs exists and is a directory' print '/docs' isdir if '/docs exists and is a directory' print
Smalltalk
Squeak has no notion of 'current directory' because it isn't tied to the shell that created it.
FileDirectory new fileExists: 'c:\serial'. (FileDirectory on: 'c:\') directoryExists: 'docs'.
In GNU Smalltalk instead you can do:
(Directory name: 'docs') exists ifTrue: [ ... ] (Directory name: 'c:\docs') exists ifTrue: [ ... ] (File name: 'serial') isFile ifTrue: [ ... ] (File name: 'c:\serial') isFile ifTrue: [ ... ]
Using exists in the third and fourth case will return true for directories too.
Tcl
Taking the meaning of the task from the DOS example:
if { [file exists input.txt] } { puts "input.txt exists" } if { [file exists [file nativename /input.txt]] } { puts "/input.txt exists" }
if { [file isdirectory docs] } { puts "docs exists and is a directory" } if { [file isdirectory [file nativename /docs]] } { puts "/docs exists and is a directory" }
Toka
[ "R" file.open dup 0 <> [ dup file.close ] ifTrue 0 <> ] is exists? " input.txt" exists? . " /input.txt" exists? . " docs" exists? . " /docs" exists? .
Visual Basic .NET
Platform: .NET
Language Version: 9.0+
'Current Directory Console.WriteLine(If(IO.Directory.Exists("docs"), "directory exists", "directory doesn't exists")) Console.WriteLine(If(IO.Directory.Exists("output.txt"), "file exists", "file doesn't exists")) 'Root Console.WriteLine(If(IO.Directory.Exists("\docs"), "directory exists", "directory doesn't exists")) Console.WriteLine(If(IO.Directory.Exists("\output.txt"), "file exists", "file doesn't exists")) 'Root, platform independent Console.WriteLine(If(IO.Directory.Exists(IO.Path.DirectorySeparatorChar & "docs"), _ "directory exists", "directory doesn't exists")) Console.WriteLine(If(IO.Directory.Exists(IO.Path.DirectorySeparatorChar & "output.txt"), _ "file exists", "file doesn't exists"))