Check that file exists

Revision as of 04:06, 20 July 2007 by rosettacode>Linuxadmin (Perl - Without a perl module)

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.

Task
Check that file exists
You are encouraged to solve this task according to the task description, using any language you may know.


DOS

 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.

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);
   }
}

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 want to check if file is readable the following may be usefull:

;;; 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;

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")

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? .