Check that file exists: Difference between revisions
Content added Content deleted
No edit summary |
(Added Toka) |
||
Line 89: | Line 89: | ||
if { [file isdirectory docs] } { puts "docs exists and is a directory" } |
if { [file isdirectory docs] } { puts "docs exists and is a directory" } |
||
if { [file isdirectory [file nativename /docs]] } { puts "/docs exists and is a directory" } |
if { [file isdirectory [file nativename /docs]] } { puts "/docs exists and is a directory" } |
||
==[[Toka]]== |
|||
[[Category:Toka]] |
|||
[ "R" file.open dup 0 <> [ dup file.close ] ifTrue 0 <> ] is exists? |
|||
" input.txt" exists? . |
|||
" /input.txt" exists? . |
|||
" docs" exists? . |
|||
" /docs" exists? . |
Revision as of 01:41, 14 May 2007
Check that file exists
You are encouraged to solve this task according to the task description, using any language you may know.
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.
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
#!/usr/bin/perl sub isFileExists($) { my ($filename) = @_; return -e $filename; } sub test($$) { my ($type, $filename) = @_; print "The following " . $type . " called " . $filename . (isFileExists($filename) ? " exists." : " not exists."); } my $FileSeperator = ($^O eq "MSWin32") ? "\\" : "/"; test("file", "input.txt"); test("file", $FileSeperator . "input.txt"); test("directory", "docs"); test("directory", $FileSeperator ."docs". $FileSeperator); exit;
# Short version my $FileSeperator = ($^O eq "MSWin32") ? "\\" : "/"; print -e 'input.txt'; print -e $FileSeperator.'input.txt'; print -e 'docs'; print -e $FileSeperator.'docs'.$FileSeperator;
Pop11
;;; Define an auxilary function, returns boolean define file_exists(fname); lvars f = sysopen(fname, 0, true, `A`); if f then sysclose(f); return (true); else return (false); endif; enddefine;
;;; Use it (prints true or false) file_exists('input.txt') => file_exists('/input.txt') => file_exists('docs') => file_exists('/docs') =>
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.
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? .