Check that file exists: Difference between revisions

m
Fixed lang tags.
m (→‎{{header|PHP}}: s/ther/there/)
m (Fixed lang tags.)
Line 179:
=={{header|C++}}==
{{libheader|boost}}
<lang cpp>#include "boost/filesystem.hpp"
#include "boost/filesystem.hpp"
#include <string>
#include <iostream>
Line 204 ⟶ 203:
testfile("/input.txt");
testfile("/docs");
}</lang>
}
</lang>
 
=={{header|C sharp|C#}}==
Line 217 ⟶ 215:
 
=={{header|Clojure}}==
<lang lisp>(import '(java.io File))
<lang clojure>
(import '(java.io File))
 
(defn kind [filename]
Line 234 ⟶ 231:
(look-for "/input.txt")
(look-for "docs")
(look-for "/docs")</lang>
</lang>
 
=={{header|Common Lisp}}==
Line 260 ⟶ 256:
 
=={{header|D}}==
<lang d>import std.file, std.path: sep;
import std.file, std.path: sep;
 
void verify(string name) {
Line 273 ⟶ 268:
// check in root
verify(sep~"input.txt"); verify(sep~"docs");
}</lang>
}
</lang>
 
=={{header|DOS Batch File}}==
Line 283 ⟶ 277:
 
=={{header|E}}==
<lang e>for file in [<file:input.txt>,
<file:///input.txt>] {
require(file.exists(), fn { `$file is missing!` })
require(!file.isDirectory(), fn { `$file is a directory!` })
}
 
for file in [<file:docs>,
<file:///docs>] {
require(file.exists(), fn { `$file is missing!` })
require(file.isDirectory(), fn { `$file is not a directory!` })
}</lang>
}
=={{header|Forth}}==
 
<lang 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</lang>
 
=={{header|Fortran}}==
Line 306 ⟶ 300:
 
Cannot check for directories in Fortran
<lang fortran> LOGICAL :: file_exists
INQUIRE(FILE="input.txt", EXIST=file_exists) ! file_exists will be TRUE if the file
! exists and FALSE otherwise
INQUIRE(FILE="/input.txt", EXIST=file_exists)</lang>
 
Actually, f90,f95 are able to deal with directory staff:
 
<lang fortran> logical :: dir_e
! a trick to be sure docs is a dir
inquire( file="./docs/.", exist=dir_e )
if ( dir_e ) then
write(*,*), "dir exists!"
else
! workaround: it calls an extern program...
call system('mkdir docs')
end if</lang>
 
=={{header|Haskell}}==
 
<lang 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"</lang>
 
=={{header|J}}==
<lang j> require 'files'
fexist 'input.txt'
fexist '/input.txt'
direxist=: 2 = ftype
direxist 'docs'
direxist '/docs'</lang>
 
=={{header|Java}}==
Line 378 ⟶ 372:
=={{header|Logo}}==
{{works with|UCB Logo}}
<lang logo>show file? "input.txt
show file? "input.txt
show file? "/input.txt
show file? "docs
show file? "/docs</lang>
</lang>
Alternatively, one can set a file prefix used for subsequent file commands.
<lang logo>setprefix "/
show file? "input.txt</lang>
setprefix "/
show file? "input.txt
</lang>
 
=={{header|MAXScript}}==
<lang maxscript>-- Here
doesFileExist "input.txt"
(getDirectories "docs").count == 1
-- Root
doesFileExist "\input.txt"
(getDirectories "C:\docs").count == 1</lang>
 
=={{header|Modula-3}}==
Line 457 ⟶ 447:
=={{header|Pop11}}==
 
<lang pop11>sys_file_exists('input.txt') =>
sys_file_exists('/input.txt') =>
sys_file_exists('docs') =>
sys_file_exists('/docs') =></lang>
 
Note that the above literally checks for existence. Namely sys_file_exists returns true if file exists but can not be read.
Line 466 ⟶ 456:
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:
 
<lang pop11>;;; 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;</lang>
 
The above works but is not the only way or the best way to check status of a file in Pop11. There is a very general procedure sys_file_stat that allows interrogation of a file or directory. The full documentation can be seen in the online documentation (search for sys_file_stat):
Line 517 ⟶ 507:
=={{header|Raven}}==
 
<lang 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</lang>
 
=={{header|Ruby}}==
Line 533 ⟶ 523:
 
=={{header|Slate}}==
<lang slate>(File newNamed: 'input.txt') exists
(File newNamed: 'input.txt') exists
(File newNamed: '/input.txt') exists
(Directory root / 'input.txt') exists
(Directory newNamed: 'docs') exists
(Directory newNamed: '/docs') exists</lang>
</lang>
 
=={{header|Smalltalk}}==
Line 545 ⟶ 533:
[[Squeak]] has no notion of 'current directory' because it isn't tied to the shell that created it.
 
<lang smalltalk> FileDirectory new fileExists: 'c:\serial'.
(FileDirectory on: 'c:\') directoryExists: 'docs'.</lang>
 
In [[GNU Smalltalk]] instead you can do:
 
<lang smalltalk> (Directory name: 'docs') exists ifTrue: [ ... ]
(Directory name: 'c:\docs') exists ifTrue: [ ... ]
(File name: 'serial') isFile ifTrue: [ ... ]
(File name: 'c:\serial') isFile ifTrue: [ ... ]</lang>
 
Using ''exists'' in the third and fourth case will return true for directories too.
Line 584 ⟶ 572:
=={{header|Toka}}==
 
<lang toka>[ "R" file.open dup 0 <> [ dup file.close ] ifTrue 0 <> ] is exists?
" input.txt" exists? .
" /input.txt" exists? .
" docs" exists? .
" /docs" exists? .</lang>
 
=={{header|UNIX Shell}}==
<lang bash>test -f input.txt
test -f /input.txt
test -d docs
test -d /docs</lang>
 
=={{header|Vedit macro language}}==
Vedit allows using either '\' or '/' as directory separator character, it is automatically converted to the one used by the operating system.
<lang vedit>// In current directory
// In current directory
if (File_Exist("input.txt")) { M("input.txt exists\n") } else { M("input.txt does not exist\n") }
if (File_Exist("docs/nul", NOERR)) { M("docs exists\n") } else { M("docs does not exist\n") }
Line 605 ⟶ 592:
// In the root directory
if (File_Exist("/input.txt")) { M("/input.txt exists\n") } else { M("/input.txt does not exist\n") }
if (File_Exist("/docs/nul", NOERR)) { M("/docs exists\n") } else { M("/docs does not exist\n") }</lang>
</lang>
 
=={{header|Visual Basic .NET}}==
Line 612 ⟶ 598:
 
{{works with|Visual Basic .NET|9.0+}}
<lang vbnet>'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"))</lang>
 
{{omit from|TI-83 BASIC}} {{omit from|TI-89 BASIC}} <!-- Does not have a filesystem, just namespaced variables. -->
Anonymous user