Check that file exists: Difference between revisions
Content added Content deleted
m (→{{header|PHP}}: s/ther/there/) |
m (Fixed lang tags.) |
||
Line 179: | Line 179: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
{{libheader|boost}} |
{{libheader|boost}} |
||
<lang cpp> |
<lang cpp>#include "boost/filesystem.hpp" |
||
#include "boost/filesystem.hpp" |
|||
#include <string> |
#include <string> |
||
#include <iostream> |
#include <iostream> |
||
Line 204: | Line 203: | ||
testfile("/input.txt"); |
testfile("/input.txt"); |
||
testfile("/docs"); |
testfile("/docs"); |
||
⚫ | |||
} |
|||
⚫ | |||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
Line 217: | Line 215: | ||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
⚫ | |||
<lang clojure> |
|||
⚫ | |||
(defn kind [filename] |
(defn kind [filename] |
||
Line 234: | Line 231: | ||
(look-for "/input.txt") |
(look-for "/input.txt") |
||
(look-for "docs") |
(look-for "docs") |
||
(look-for "/docs") |
(look-for "/docs")</lang> |
||
⚫ | |||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
Line 260: | Line 256: | ||
=={{header|D}}== |
=={{header|D}}== |
||
<lang d> |
<lang d>import std.file, std.path: sep; |
||
import std.file, std.path: sep; |
|||
void verify(string name) { |
void verify(string name) { |
||
Line 273: | Line 268: | ||
// check in root |
// check in root |
||
verify(sep~"input.txt"); verify(sep~"docs"); |
verify(sep~"input.txt"); verify(sep~"docs"); |
||
⚫ | |||
} |
|||
⚫ | |||
=={{header|DOS Batch File}}== |
=={{header|DOS Batch File}}== |
||
Line 283: | Line 277: | ||
=={{header|E}}== |
=={{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!` }) |
|||
⚫ | |||
} |
|||
=={{header|Forth}}== |
=={{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}}== |
=={{header|Fortran}}== |
||
Line 306: | Line 300: | ||
Cannot check for directories in Fortran |
Cannot check for directories in Fortran |
||
<lang 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: |
Actually, f90,f95 are able to deal with directory staff: |
||
<lang fortran> |
<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}}== |
=={{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}}== |
=={{header|J}}== |
||
<lang j> |
<lang j>require 'files' |
||
fexist 'input.txt' |
|||
fexist '/input.txt' |
|||
direxist=: 2 = ftype |
|||
direxist 'docs' |
|||
direxist '/docs'</lang> |
|||
=={{header|Java}}== |
=={{header|Java}}== |
||
Line 378: | Line 372: | ||
=={{header|Logo}}== |
=={{header|Logo}}== |
||
{{works with|UCB Logo}} |
{{works with|UCB Logo}} |
||
<lang logo> |
<lang logo>show file? "input.txt |
||
⚫ | |||
show file? "/input.txt |
show file? "/input.txt |
||
show file? "docs |
show file? "docs |
||
show file? "/docs |
show file? "/docs</lang> |
||
</lang> |
|||
Alternatively, one can set a file prefix used for subsequent file commands. |
Alternatively, one can set a file prefix used for subsequent file commands. |
||
<lang logo> |
<lang logo>setprefix "/ |
||
⚫ | |||
setprefix "/ |
|||
show file? "input.txt |
|||
</lang> |
|||
=={{header|MAXScript}}== |
=={{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}}== |
=={{header|Modula-3}}== |
||
Line 457: | Line 447: | ||
=={{header|Pop11}}== |
=={{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. |
Note that the above literally checks for existence. Namely sys_file_exists returns true if file exists but can not be read. |
||
Line 466: | Line 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: |
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): |
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: | Line 507: | ||
=={{header|Raven}}== |
=={{header|Raven}}== |
||
<lang 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}}== |
=={{header|Ruby}}== |
||
Line 533: | Line 523: | ||
=={{header|Slate}}== |
=={{header|Slate}}== |
||
<lang slate> |
<lang slate>(File newNamed: 'input.txt') exists |
||
(File newNamed: 'input.txt') exists |
|||
(File newNamed: '/input.txt') exists |
(File newNamed: '/input.txt') exists |
||
(Directory root / 'input.txt') exists |
(Directory root / 'input.txt') exists |
||
(Directory newNamed: 'docs') exists |
(Directory newNamed: 'docs') exists |
||
(Directory newNamed: '/docs') exists |
(Directory newNamed: '/docs') exists</lang> |
||
</lang> |
|||
=={{header|Smalltalk}}== |
=={{header|Smalltalk}}== |
||
Line 545: | Line 533: | ||
[[Squeak]] has no notion of 'current directory' because it isn't tied to the shell that created it. |
[[Squeak]] has no notion of 'current directory' because it isn't tied to the shell that created it. |
||
<lang smalltalk> |
<lang smalltalk>FileDirectory new fileExists: 'c:\serial'. |
||
(FileDirectory on: 'c:\') directoryExists: 'docs'.</lang> |
|||
In [[GNU Smalltalk]] instead you can do: |
In [[GNU Smalltalk]] instead you can do: |
||
<lang smalltalk> |
<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. |
Using ''exists'' in the third and fourth case will return true for directories too. |
||
Line 584: | Line 572: | ||
=={{header|Toka}}== |
=={{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}}== |
=={{header|UNIX Shell}}== |
||
<lang bash>test -f input.txt |
|||
test -f /input.txt |
|||
test -d docs |
|||
test -d /docs</lang> |
|||
=={{header|Vedit macro language}}== |
=={{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. |
Vedit allows using either '\' or '/' as directory separator character, it is automatically converted to the one used by the operating system. |
||
<lang vedit> |
<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("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") } |
if (File_Exist("docs/nul", NOERR)) { M("docs exists\n") } else { M("docs does not exist\n") } |
||
Line 605: | Line 592: | ||
// In the root directory |
// 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("/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") } |
if (File_Exist("/docs/nul", NOERR)) { M("/docs exists\n") } else { M("/docs does not exist\n") }</lang> |
||
</lang> |
|||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |
||
Line 612: | Line 598: | ||
{{works with|Visual Basic .NET|9.0+}} |
{{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. --> |
{{omit from|TI-83 BASIC}} {{omit from|TI-89 BASIC}} <!-- Does not have a filesystem, just namespaced variables. --> |