Check that file exists: Difference between revisions

m (→‎[[Java]]: Use Java header instead)
 
(360 intermediate revisions by more than 100 users not shown)
Line 1:
[[Category:Simple]]
{{task}}
[[Category:{{task|File System Operations]]}}
;Task:
Verify that a file called     '''input.txt'''     and   a directory called     '''docs'''     exist.
 
This should be done twice:  
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.
:::*   once for the current working directory,   and
:::*   once for a file and a directory in the filesystem root.
 
<br>
==[[C]]==
Optional criteria (May 2015): &nbsp; verify it works with:
[[Category:C]]
:::* &nbsp; zero-length files
#include <sys/types.h>
:::* &nbsp; an unusual filename: &nbsp; <big> ''' `Abdu'l-Bahá.txt ''' </big>
#include <sys/stat.h>
<br><br>
#include <unistd.h>
=={{header|11l}}==
int main(){
{{Trans|Python}}
struct stat file_stat;
<syntaxhighlight lang="11l">fs:is_file(‘input.txt’)
if(stat("/tmp/test",&file_stat) ==0)
fs:is_file(‘/input.txt’)
printf("file exists");
fs:is_dir(‘docs’)
else
fs:is_dir(‘/docs’)</syntaxhighlight>
printf("no file lol");
=={{header|AArch64 Assembly}}==
}
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program verifFic64.s */
 
/*******************************************/
==[[DOS Batch File]]==
/* Constantes file */
[[Category:DOS Batch File]]
/*******************************************/
if exist input.txt echo The following file called input.txt exists.
/* for this file see task include a file in language AArch64 assembly*/
if exist \input.txt echo The following file called \input.txt exists.
.include "../includeConstantesARM64.inc"
if exist docs echo The following directory called docs exists.
.equ CHDIR, 49
if exist \docs\ echo The following directory called \docs\ exists.
.equ AT_FDCWD, -100
 
/*******************************************/
==[[Forth]]==
/* Initialized data */
[[Category:Forth]]
/*******************************************/
.data
szMessFound1: .asciz "File 1 found.\n"
szMessFound2: .asciz "File 2 found.\n"
szMessNotFound1: .asciz "File 1 not found.\n"
szMessNotFound2: .asciz "File 2 not found.\n"
szMessDir2: .asciz "File 2 is a directory.\n"
szMessNotAuth2: .asciz "File 2 permission denied.\n"
szCarriageReturn: .asciz "\n"
/* areas strings */
szPath2: .asciz "/"
szFicName1: .asciz "test1.txt"
szFicName2: .asciz "root"
/*******************************************/
/* UnInitialized data */
/*******************************************/
.bss
/*******************************************/
/* code section */
/*******************************************/
.text
.global main
main: // entry of program
 
/*************************************
: .exists ( str len -- ) 2dup file-status nip 0= if type ." exists" else type ." does not exist" then ;
open file 1
s" input.txt" .exists
************************************/
s" /input.txt" .exists
mov x0,AT_FDCWD // current directory
s" docs" .exists
ldr x1,qAdrszFicName1 // file name
s" /docs" .exists
mov x2,#O_RDWR // flags
mov x3,#0 // mode
mov x8, #OPEN // call system OPEN
svc 0
cmp x0,#0 // error ?
ble 1f
mov x1,x0 // FD
ldr x0,qAdrszMessFound1
bl affichageMess
// close file
mov x0,x1 // Fd
mov x8, #CLOSE
svc 0
b 2f
1:
ldr x0,qAdrszMessNotFound1
bl affichageMess
2:
/*************************************
open file 2
************************************/
ldr x0,qAdrszPath2
mov x8,CHDIR // call system change directory
svc 0
mov x0,AT_FDCWD // current directory
ldr x1,qAdrszFicName2 // file name
mov x2,O_RDWR // flags
mov x3,0 // mode
mov x8,OPEN // call system OPEN
svc 0
cmp x0,-21 // is a directory ?
beq 4f
cmp x0,0 // error ?
ble 3f
mov x1,x0 // FD
ldr x0,qAdrszMessFound2
bl affichageMess
// close file
mov x0,x1 // Fd
mov x8, #CLOSE
svc 0
b 100f
3:
ldr x0,qAdrszMessNotFound2
bl affichageMess
b 100f
4:
ldr x0,qAdrszMessDir2
bl affichageMess
100: // standard end of the program
mov x0,0 // return code
mov x8,EXIT // request to exit program
svc 0 // perform the system call
qAdrszFicName1: .quad szFicName1
qAdrszFicName2: .quad szFicName2
qAdrszMessFound1: .quad szMessFound1
qAdrszMessFound2: .quad szMessFound2
qAdrszMessNotFound1: .quad szMessNotFound1
qAdrszMessNotFound2: .quad szMessNotFound2
qAdrszMessNotAuth2: .quad szMessNotAuth2
qAdrszPath2: .quad szPath2
qAdrszMessDir2: .quad szMessDir2
/********************************************************/
/* File Include fonctions */
/********************************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
=={{header|Action!}}==
<syntaxhighlight lang="action!">BYTE lastError
 
PROC MyError(BYTE errCode)
=={{header|Java}}==
lastError=errCode
import java.util.File;
RETURN
public class FileExistsTest {
 
public static boolean isFileExists(String filename) {
BYTE FUNC FileExists(CHAR ARRAY fname)
boolean exists = new File(filename).exists();
DEFINE PTR="CARD"
return exists;
PTR old
BYTE dev=[1]
 
lastError=0
old=Error
Error=MyError ;replace error handling to capture I/O error
 
Close(dev)
Open(dev,fname,4)
Close(dev)
 
Error=old ;restore the original error handling
 
IF lastError=0 THEN
RETURN (1)
FI
RETURN (0)
 
PROC Test(CHAR ARRAY fname)
BYTE exists
 
exists=FileExists(fname)
IF exists THEN
PrintF("File ""%S"" exists.%E",fname)
ELSE
PrintF("File ""%S"" does not exist.%E",fname)
FI
RETURN
 
PROC Main()
Test("D:INPUT.TXT")
Test("D:DOS.SYS")
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Check_that_file_exists.png Screenshot from Atari 8-bit computer]
<pre>
File "D:INPUT.TXT" does not exist.
File "D:DOS.SYS" exists.
</pre>
=={{header|Ada}}==
This example should work with any Ada 95 compiler.
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO;
 
procedure File_Exists is
function Does_File_Exist (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 Does_File_Exist;
begin
Put_Line (Boolean'Image (Does_File_Exist ("input.txt" )));
Put_Line (Boolean'Image (Does_File_Exist ("\input.txt")));
end File_Exists;</syntaxhighlight>
This example should work with any Ada 2005 compiler.
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO;
with Ada.Directories; use Ada.Directories;
 
procedure File_Exists is
procedure Print_File_Exist (Name : String) is
begin
Put_Line ("Does " & Name & " exist? " &
Boolean'Image (Exists (Name)));
end Print_File_Exist;
procedure Print_Dir_Exist (Name : String) is
begin
Put_Line ("Does directory " & Name & " exist? " &
Boolean'Image (Exists (Name) and then Kind (Name) = Directory));
end Print_Dir_Exist;
begin
Print_File_Exist ("input.txt" );
Print_File_Exist ("/input.txt");
Print_Dir_Exist ("docs");
Print_Dir_Exist ("/docs");
end File_Exists;</syntaxhighlight>
=={{header|Aikido}}==
The <code>stat</code> function returns a <code>System.Stat</code> object for an existing file or directory, or <code>null</code> if it can't be found.
<syntaxhighlight lang="aikido">
function exists (filename) {
return stat (filename) != null
}
 
exists ("input.txt")
exists ("/input.txt")
exists ("docs")
exists ("/docs")
 
</syntaxhighlight>
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
Uses the Algol 68G specific "file is directory" procedure to test for the existence of directories.
<syntaxhighlight lang="algol68"># Check files and directories exist #
 
# check a file exists by attempting to open it for input #
# returns TRUE if the file exists, FALSE otherwise #
PROC file exists = ( STRING file name )BOOL:
IF FILE f;
open( f, file name, stand in channel ) = 0
THEN
# file opened OK so must exist #
close( f );
TRUE
ELSE
# file cannot be opened - assume it does not exist #
FALSE
FI # file exists # ;
 
# print a suitable messages if the specified file exists #
PROC test file exists = ( STRING name )VOID:
print( ( "file: "
, name
, IF file exists( name ) THEN " does" ELSE " does not" FI
, " exist"
, newline
)
);
# print a suitable messages if the specified directory exists #
PROC test directory exists = ( STRING name )VOID:
print( ( "dir: "
, name
, IF file is directory( name ) THEN " does" ELSE " does not" FI
, " exist"
, newline
)
);
 
# test the flies and directories mentioned in the task exist or not #
test file exists( "input.txt" );
test file exists( "\input.txt");
test directory exists( "docs" );
test directory exists( "\docs" )</syntaxhighlight>
=={{header|Amazing Hopper}}==
Version: hopper-FLOW!:
<syntaxhighlight lang="amazing hopper">
#include <flow.h>
 
DEF-MAIN(argv, argc)
WHEN( IS-FILE?("hopper") ){
MEM("File \"hopper\" exist!\n")
}
WHEN( IS-DIR?("fl") ){
MEM("Directory \"fl\" exist!\n")
}
IF( IS-DIR?("noExisDir"), "Directory \"noExistDir\" exist!\n", \
"Directory \"noExistDir\" does NOT exist!\n" )
//"arch mañoso bacán.txt" text-file created
 
STR-TO-UTF8("File \"arch mañoso bacán.txt\" ")
IF( IS-FILE?( STR-TO-UTF8("arch mañoso bacán.txt") ), "exist!\n", "NOT exist!\n")
 
PRNL
END
</syntaxhighlight>
{{out}}
<pre>
$ hopper existFile.flw
File "hopper" exist!
Directory "fl" exist!
Directory "noExistDir" does NOT exist!
File "arch mañoso bacán.txt" exist!
$
</pre>
=={{header|APL}}==
<syntaxhighlight lang="apl">
h ← ⎕fio['fopen'] 'input.txt'
h
7
⎕fio['fstat'] h
66311 803134 33188 1 1000 1000 0 11634 4096 24 1642047105 1642047105 1642047105
⎕fio['fclose'] h
0
h ← ⎕fio['fopen'] 'docs/'
h
7
⎕fio['fstat'] h
66311 3296858 16877 2 1000 1000 0 4096 4096 8 1642047108 1642047108 1642047108
⎕fio['fclose'] h
0
h ← ⎕fio['fopen'] 'does_not_exist.txt'
h
¯1
</syntaxhighlight>
=={{header|AppleScript}}==
{{Trans|JavaScript}}
(macOS JavaScript for Automation)
<syntaxhighlight lang="applescript">use framework "Foundation" -- YOSEMITE OS X onwards
use scripting additions
 
on run
setCurrentDirectory("~/Desktop")
ap({doesFileExist, doesDirectoryExist}, ¬
{"input.txt", "/input.txt", "docs", "/docs"})
--> {true, true, true, true, false, false, true, true}
-- The first four booleans are returned by `doesFileExist`.
-- The last four are returned by `doesDirectoryExist`,
-- which yields false for simple files, and true for directories.
end run
 
-- GENERIC SYSTEM DIRECTORY FUNCTIONS -----------------------------------------
 
-- doesDirectoryExist :: String -> Bool
on doesDirectoryExist(strPath)
set ca to current application
set oPath to (ca's NSString's stringWithString:strPath)'s ¬
stringByStandardizingPath
set {bln, int} to (ca's NSFileManager's defaultManager()'s ¬
fileExistsAtPath:oPath isDirectory:(reference))
bln and (int = 1)
end doesDirectoryExist
 
-- doesFileExist :: String -> Bool
on doesFileExist(strPath)
set ca to current application
set oPath to (ca's NSString's stringWithString:strPath)'s ¬
stringByStandardizingPath
ca's NSFileManager's defaultManager()'s fileExistsAtPath:oPath
end doesFileExist
 
-- getCurrentDirectory :: String
on getCurrentDirectory()
set ca to current application
ca's NSFileManager's defaultManager()'s currentDirectoryPath as string
end getCurrentDirectory
 
-- getFinderDirectory :: String
on getFinderDirectory()
tell application "Finder" to POSIX path of (insertion location as alias)
end getFinderDirectory
 
-- getHomeDirectory :: String
on getHomeDirectory()
(current application's NSHomeDirectory() as string)
end getHomeDirectory
 
-- setCurrentDirectory :: String -> IO ()
on setCurrentDirectory(strPath)
if doesDirectoryExist(strPath) then
set ca to current application
set oPath to (ca's NSString's stringWithString:strPath)'s ¬
stringByStandardizingPath
ca's NSFileManager's defaultManager()'s ¬
changeCurrentDirectoryPath:oPath
end if
end setCurrentDirectory
 
 
-- GENERIC HIGHER ORDER FUNCTIONS FOR THE TEST --------------------------------
 
-- A list of functions applied to a list of arguments
-- (<*> | ap) :: [(a -> b)] -> [a] -> [b]
on ap(fs, xs)
set {intFs, intXs} to {length of fs, length of xs}
set lst to {}
repeat with i from 1 to intFs
tell mReturn(item i of fs)
repeat with j from 1 to intXs
set end of lst to |λ|(contents of (item j of xs))
end repeat
end tell
end repeat
return lst
end ap
 
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: Handler -> Script
on mReturn(f)
if class of f is script then
f
else
script
property |λ| : f
end script
end if
end mReturn</syntaxhighlight>
{{Out}}
The first four booleans are returned by `doesFileExist`.
The last four are returned by `doesDirectoryExist`,
which yields false for simple files, and true for directories.
<syntaxhighlight lang="applescript">{true, true, true, true, false, false, true, true}</syntaxhighlight>
=={{header|Applesoft BASIC}}==
The error code for FILE NOT FOUND is 6.
<syntaxhighlight lang="applesoftbasic">100 F$ = "THAT FILE"
110 T$(0) = "DOES NOT EXIST."
120 T$(1) = "EXISTS."
130 GOSUB 200"FILE EXISTS?
140 PRINT F$; " "; T$(E)
150 END
 
200 REM FILE EXISTS?
210 REM TRY
220 ON ERR GOTO 300"CATCH
230 PRINT CHR$(4); "VERIFY "; F$
240 POKE 216, 0 : REM ONERR OFF
250 E = 1
260 GOTO 350"END TRY
300 REM CATCH
310 E = PEEK(222) <> 6
320 POKE 216, 0 : REM ONERR OFF
330 IF E THEN RESUME : REM THROW
340 CALL - 3288 : REM RECOVER
350 REM END TRY
360 RETURN
</syntaxhighlight>
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
/* ARM assembly Raspberry PI */
/* program verifFic.s */
 
/* Constantes */
.equ STDOUT, 1 @ Linux output console
.equ EXIT, 1 @ Linux syscall
.equ WRITE, 4 @ Linux syscall
.equ OPEN, 5 @ Linux syscall
.equ CLOSE, 6 @ Linux syscall
 
.equ O_RDWR, 0x0002 /* open for reading and writing */
 
/*******************************************/
/* Fichier des macros */
/********************************************/
.include "../../ficmacros.s"
 
/* Initialized data */
.data
szMessFound1: .asciz "File 1 found.\n"
szMessFound2: .asciz "File 2 found.\n"
szMessNotFound1: .asciz "File 1 not found.\n"
szMessNotFound2: .asciz "File 2 not found.\n"
szMessNotAuth2: .asciz "File 2 permission denied.\n"
szCarriageReturn: .asciz "\n"
 
/* areas strings */
szFicName1: .asciz "test1.txt"
szFicName2: .asciz "/debian-binary"
 
 
/* UnInitialized data */
.bss
 
/* code section */
.text
.global main
main: /* entry of program */
push {fp,lr} /* saves 2 registers */
 
/*************************************
open file 1
************************************/
ldr r0,iAdrszFicName1 @ file name
mov r1,#O_RDWR @ flags
mov r2,#0 @ mode
mov r7, #OPEN @ call system OPEN
swi 0
cmp r0,#0 @ error ?
ble 1f
mov r1,r0 @ FD
ldr r0,iAdrszMessFound1
bl affichageMess
@ close file
mov r0,r1 @ Fd
mov r7, #CLOSE
swi 0
b 2f
1:
ldr r0,iAdrszMessNotFound1
bl affichageMess
2:
/*************************************
open file 2
************************************/
ldr r0,iAdrszFicName2 @ file name
mov r1,#O_RDWR @ flags
mov r2,#0 @ mode
mov r7, #OPEN @ call system OPEN
swi 0
vidregtit verif
cmp r0,#-13 @ permission denied
beq 4f
cmp r0,#0 @ error ?
ble 3f
mov r1,r0 @ FD
ldr r0,iAdrszMessFound2
bl affichageMess
@ close file
mov r0,r1 @ Fd
mov r7, #CLOSE
swi 0
b 100f
3:
ldr r0,iAdrszMessNotFound2
bl affichageMess
b 100f
4:
ldr r0,iAdrszMessNotAuth2
bl affichageMess
100: /* standard end of the program */
mov r0, #0 @ return code
pop {fp,lr} @restaur 2 registers
mov r7, #EXIT @ request to exit program
swi 0 @ perform the system call
iAdrszFicName1: .int szFicName1
iAdrszFicName2: .int szFicName2
iAdrszMessFound1: .int szMessFound1
iAdrszMessFound2: .int szMessFound2
iAdrszMessNotFound1: .int szMessNotFound1
iAdrszMessNotFound2: .int szMessNotFound2
iAdrszMessNotAuth2: .int szMessNotAuth2
/******************************************************************/
/* display text with size calculation */
/******************************************************************/
/* r0 contains the address of the message */
affichageMess:
push {fp,lr} /* save registres */
push {r0,r1,r2,r7} /* save others registers */
mov r2,#0 /* counter length */
1: /* loop length calculation */
ldrb r1,[r0,r2] /* read octet start position + index */
cmp r1,#0 /* if 0 its over */
addne r2,r2,#1 /* else add 1 in the length */
bne 1b /* and loop */
/* so here r2 contains the length of the message */
mov r1,r0 /* address message in r1 */
mov r0,#STDOUT /* code to write to the standard output Linux */
mov r7, #WRITE /* code call system "write" */
swi #0 /* call systeme */
pop {r0,r1,r2,r7} /* restaur others registers */
pop {fp,lr} /* restaur des 2 registres */
bx lr /* return */
</syntaxhighlight>
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">checkIfExists: function [fpath][
(or? exists? fpath
exists? .directory fpath)? -> print [fpath "exists"]
-> print [fpath "doesn't exist"]
]
checkIfExists "input.txt"
checkIfExists "docs"
checkIfExists "/input.txt"
checkIfExists "/docs"</syntaxhighlight>
=={{header|AutoHotkey}}==
AutoHotkey’s FileExist() function returns an attribute string (a subset of "RASHNDOCT") if a matching file or directory is found. The attribute string must be parsed for the letter D to determine whether the match is a directory or file.
<br><br>
Another option is AutoHotkey's IfExist/IfNotExist command
 
<syntaxhighlight lang="autohotkey">; FileExist() function examples
ShowFileExist("input.txt")
ShowFileExist("\input.txt")
ShowFolderExist("docs")
ShowFolderExist("\docs")
 
; IfExist/IfNotExist command examples (from documentation)
IfExist, D:\
MsgBox, The drive exists.
IfExist, D:\Docs\*.txt
MsgBox, At least one .txt file exists.
IfNotExist, C:\Temp\FlagFile.txt
MsgBox, The target file does not exist.
 
Return
 
ShowFileExist(file)
{
If (FileExist(file) && !InStr(FileExist(file), "D"))
MsgBox, file: %file% exists.
Else
MsgBox, file: %file% does NOT exist.
Return
}
 
ShowFolderExist(folder)
{
If InStr(FileExist(folder), "D")
MsgBox, folder: %folder% exists.
Else
MsgBox, folder: %folder% does NOT exist.
Return
}</syntaxhighlight>
=={{header|AWK}}==
{{works with|gawk}}
<syntaxhighlight lang="awk">@load "filefuncs"
 
function exists(name ,fd) {
if ( stat(name, fd) == -1)
print name " doesn't exist"
else
print name " exists"
}
BEGIN {
exists("input.txt")
exists("/input.txt")
exists("docs")
exists("/docs")
}</syntaxhighlight>
 
Portable getline method. Also works in a Windows environment.
<syntaxhighlight lang="awk">#
# Check if file or directory exists, even 0-length file.
# Return 0 if not exist, 1 if exist
#
function exists(file ,line, msg)
{
if ( (getline line < file) == -1 )
{
# "Permission denied" is for MS-Windows
msg = (ERRNO ~ /Permission denied/ || ERRNO ~ /a directory/) ? 1 : 0
close(file)
return msg
}
else {
close(file)
return 1
}
}
BEGIN {
exists("input.txt")
exists("\\input.txt")
exists("docs")
exists("\\docs")
exit(0)
}</syntaxhighlight>
 
{{works with|gawk}}
Check file(s) existence
<syntaxhighlight lang="text">gawk 'BEGINFILE{if (ERRNO) {print "Not exist."; nextfile} else {print "Exist."; nextfile}}' input.txt input-missing.txt</syntaxhighlight>
=={{header|Axe}}==
<syntaxhighlight lang="axe">If GetCalc("appvINPUT")
Disp "EXISTS",i
Else
Disp "DOES NOT EXIST",i
End</syntaxhighlight>
=={{header|BASIC}}==
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">
ON ERROR GOTO ohNo
f$ = "input.txt"
GOSUB opener
f$ = "\input.txt"
GOSUB opener
 
'can't directly check for directories,
'but can check for the NUL device in the desired dir
f$ = "docs\nul"
GOSUB opener
f$ = "\docs\nul"
GOSUB opener
END
 
opener:
e$ = " found"
OPEN f$ FOR INPUT AS 1
PRINT f$; e$
CLOSE
RETURN
 
ohNo:
IF (53 = ERR) OR (76 = ERR) THEN
e$ = " not" + e$
ELSE
e$ = "Unknown error"
END IF
RESUME NEXT
</syntaxhighlight>
 
You can also check for a directory by trying to enter it.
 
<syntaxhighlight lang="qbasic">
ON ERROR GOTO ohNo
d$ = "docs"
CHDIR d$
d$ = "\docs"
CHDIR d$
END
 
ohNo:
IF 76 = ERR THEN
PRINT d$; " not found"
ELSE
PRINT "Unknown error"
END IF
RESUME NEXT
</syntaxhighlight>
 
{{works with|QuickBasic|7.1}} {{works with|PowerBASIC for DOS}}
 
Later versions of MS-compatible BASICs include the <CODE>DIR$</CODE> keyword, which makes this pretty trivial.
 
<syntaxhighlight lang="qbasic">
f$ = "input.txt"
GOSUB opener
f$ = "\input.txt"
GOSUB opener
 
'can't directly check for directories,
'but can check for the NUL device in the desired dir
f$ = "docs\nul"
GOSUB opener
f$ = "\docs\nul"
GOSUB opener
END
 
opener:
d$ = DIR$(f$)
IF LEN(d$) THEN
PRINT f$; " found"
ELSE
PRINT f$; " not found"
END IF
RETURN
</syntaxhighlight>
 
==={{header|BaCon}}===
<syntaxhighlight lang="freebasic">' File exists
f$ = "input.txt"
d$ = "docs"
IF FILEEXISTS(f$) THEN PRINT f$, " exists"
IF FILEEXISTS(d$) AND FILETYPE(d$) = 2 THEN PRINT d$, " directory exists"
 
f$ = "/" & f$
d$ = "/" & d$
PRINT f$, IIF$(FILEEXISTS(f$), " exists", " does not exist")
PRINT d$, IIF$(FILEEXISTS(d$) AND FILETYPE(d$) = 2, " is", " is not"), " a directory"
 
f$ = "empty.bac"
PRINT f$, IIF$(FILEEXISTS(f$), " exists", " does not exist")
 
f$ = "`Abdu'l-Bahá.txt"
PRINT f$, IIF$(FILEEXISTS(f$), " exists", " does not exist")</syntaxhighlight>
 
{{out}}
<pre>prompt$ ./fileexits
/input.txt does not exist
/docs is not a directory
empty.bac exists
`Abdu'l-Bahá.txt does not exist</pre>
 
 
==={{header|Commodore BASIC}}===
Try a file, then check the error status of the disk drive. Error code 62 is the "File not found" error. The trick is to open the file without specifying the file type (PRG, SEQ, REL, etc.) and the Read/Write mode in the OPEN statement, otherwise you may end up with error code 64 "File Type Mismatch".
<syntaxhighlight lang="gwbasic">10 REM CHECK FILE EXISTS
15 ER=0:EM$="":MSG$="FILE EXISTS."
20 PRINT CHR$(147);:REM CLEAR SCREEN
30 FI$="":INPUT "ENTER FILENAME TO CHECK";FI$:PRINT
35 IF FI$="" THEN PRINT "ABORTED.":END
40 OPEN 8,8,8,FI$
50 GOSUB 1000:REM FETCH ERROR STATUS FROM DRIVE: 0=OK, 62=FILE NOT FOUND
55 REM COMPARE ERROR NUMBER
60 IF ER<>0 THEN MSG$="I/O ERROR:"+STR$(ER)+" "+EM$
70 IF ER=62 THEN MSG$="'"+FI$+"' IS NOT HERE."
80 REM DO THINGS WITH FILE...
100 CLOSE 8
110 PRINT MSG$
120 PRINT:GOTO 30:REM REPEAT UNTIL EMPTY FILENAME IS ENTERED
1000 REM CHECK ERROR CHANNEL FOR STATUS OF LAST DISK OPERATION
1010 OPEN 15,8,15
1015 REM GET ERROR CODE, ERROR MESSAGE, TRACK, SECTOR
1020 INPUT#15,ER,EM$,T,S
1030 CLOSE 15
1040 RETURN</syntaxhighlight>
 
{{Out}}<pre>ENTER FILENAME TO CHECK? INDEX.TXT
FILE EXISTS.
 
ENTER FILENAME TO CHECK? NOFILE.DOC
'NOFILE.DOC' IS NOT HERE.</pre>
 
==={{header|SmallBASIC}}===
<syntaxhighlight lang="qbasic">
if(isdir("docs")) then print "directory docs exist"
if(isdir("/docs")) then print "directory /docs exist"
if(isfile("input.txt")) then print "file input.txt exist"
if(isfile("/input.txt")) then print "file /input.txt exist"
</syntaxhighlight>
 
 
 
=={{header|BASIC256}}==
<syntaxhighlight lang="basic256">subroutine opener (filename$)
if exists(filename$) then
print filename$; " exists"
else
print filename$; " does not exists"
end if
end subroutine
 
call opener ("input.txt")
call opener ("\input.txt")
call opener ("docs\nul")
call opener ("\docs\nul")
call opener ("empty.kbs")
call opener ("`Abdu'l-Bahá.txt"))
end</syntaxhighlight>
=={{header|Batch File}}==
<syntaxhighlight lang="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.</syntaxhighlight>
=={{header|BBC BASIC}}==
<syntaxhighlight lang="bbcbasic"> test% = OPENIN("input.txt")
IF test% THEN
CLOSE #test%
PRINT "File input.txt exists"
ENDIF
test% = OPENIN("\input.txt")
IF test% THEN
CLOSE #test%
PRINT "File \input.txt exists"
ENDIF
test% = OPENIN("docs\NUL")
IF test% THEN
CLOSE #test%
PRINT "Directory docs exists"
ENDIF
test% = OPENIN("\docs\NUL")
IF test% THEN
CLOSE #test%
PRINT "Directory \docs exists"
ENDIF</syntaxhighlight>
=={{header|BQN}}==
 
'''Works In:''' [[CBQN]]
 
Takes filename as a command line argument, tells whether it exists.
 
<syntaxhighlight lang="bqn">fname ← ⊑args
•Out fname∾" Does not exist"‿" Exists"⊑˜•File.exists fname</syntaxhighlight>
=={{header|C}}==
{{libheader|POSIX}}
<syntaxhighlight lang="c">#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
 
/* Check for regular file. */
int check_reg(const char *path) {
struct stat sb;
return stat(path, &sb) == 0 && S_ISREG(sb.st_mode);
}
 
/* Check for directory. */
int check_dir(const char *path) {
struct stat sb;
return stat(path, &sb) == 0 && S_ISDIR(sb.st_mode);
}
 
int main() {
printf("input.txt is a regular file? %s\n",
check_reg("input.txt") ? "yes" : "no");
printf("docs is a directory? %s\n",
check_dir("docs") ? "yes" : "no");
printf("/input.txt is a regular file? %s\n",
check_reg("/input.txt") ? "yes" : "no");
printf("/docs is a directory? %s\n",
check_dir("/docs") ? "yes" : "no");
return 0;
}</syntaxhighlight>
 
{{libheader|Gadget}}
<syntaxhighlight lang="c">
#include <gadget/gadget.h>
 
LIB_GADGET_START
 
/* input.txt = check_file.c
docs = tests */
 
Main
Print "tests/check_file.c is a regular file? %s\n", Exist_file("tests/check_file.c") ? "yes" : "no";
Print "tests is a directory? %s\n", Exist_dir("tests") ? "yes" : "no";
Print "some.txt is a regular file? %s\n", Exist_file("some.txt") ? "yes" : "no";
Print "/tests is a directory? %s\n", Exist_dir("/tests") ? "yes" : "no";
End
</syntaxhighlight>
{{out}}
<pre>
tests/check_file.c is a regular file? yes
tests is a directory? yes
some.txt is a regular file? no
/tests is a directory? no
 
</pre>
 
=={{header|C sharp|C#}}==
 
<syntaxhighlight lang="csharp">using System.IO;
 
Console.WriteLine(File.Exists("input.txt"));
Console.WriteLine(File.Exists("/input.txt"));
Console.WriteLine(Directory.Exists("docs"));
Console.WriteLine(Directory.Exists("/docs"));</syntaxhighlight>
=={{header|C++}}==
{{libheader|boost}}
<syntaxhighlight lang="cpp">#include "boost/filesystem.hpp"
#include <string>
#include <iostream>
 
void testfile(std::string name)
{
boost::filesystem::path file(name);
if (exists(file))
{
if (is_directory(file))
std::cout << name << " is a directory.\n";
else
std::cout << name << " is a non-directory file.\n";
}
else
std::cout << name << " does not exist.\n";
}
 
int main()
{
testfile("input.txt");
testfile("docs");
testfile("/input.txt");
testfile("/docs");
}</syntaxhighlight>
 
===Using C++ 17===
C++ 17 contains a Filesystem library which significantly improves operations with files.
<syntaxhighlight lang="c++">
 
#include <iostream>
#include <filesystem>
 
void file_exists(const std::filesystem::path& path) {
std::cout << path;
if ( std::filesystem::exists(path) ) {
if ( std::filesystem::is_directory(path) ) {
std::cout << " is a directory" << std::endl;
} else {
std::cout << " exists with a file size of " << std::filesystem::file_size(path) << " bytes." << std::endl;
}
} else {
std::cout << " does not exist" << std::endl;
}
}
public static void test(String type, String filename) {
 
System.out.println("The following " + type + " called " + filename +
int main() {
(isFileExists(filename) ? " exists." : " not exists.")
file_exists("input.txt");
file_exists("zero_length.txt");
file_exists("docs/input.txt");
file_exists("docs/zero_length.txt");
}
</syntaxhighlight>
{{ out }}
</pre>
"input.txt" exists with a file size of 11 bytes.
 
"zero_length.txt" exists with a file size of 0 bytes.
 
"docs/input.txt" exists with a file size of 11 bytes.
 
"docs/zero_length.txt" exists with a file size of 0 bytes.
</pre>
 
=={{header|Clojure}}==
<syntaxhighlight lang="clojure">
 
(dorun (map #(.exists (clojure.java.io/as-file %)) '("/input.txt" "/docs" "./input.txt" "./docs")))
 
</syntaxhighlight>
=={{header|COBOL}}==
{{works with|GnuCOBOL}} and other compilers with this system call extension
<syntaxhighlight lang="cobol"> identification division.
program-id. check-file-exist.
 
environment division.
configuration section.
repository.
function all intrinsic.
 
data division.
working-storage section.
01 skip pic 9 value 2.
01 file-name.
05 value "/output.txt".
01 dir-name.
05 value "/docs/".
01 unusual-name.
05 value "Abdu'l-Bahá.txt".
 
01 test-name pic x(256).
 
01 file-handle usage binary-long.
01 file-info.
05 file-size pic x(8) comp-x.
05 file-date.
10 file-day pic x comp-x.
10 file-month pic x comp-x.
10 file-year pic xx comp-x.
05 file-time.
10 file-hours pic x comp-x.
10 file-minutes pic x comp-x.
10 file-seconds pic x comp-x.
10 file-hundredths pic x comp-x.
 
procedure division.
files-main.
 
*> check in current working dir
move file-name(skip:) to test-name
perform check-file
 
move dir-name(skip:) to test-name
perform check-file
 
move unusual-name to test-name
perform check-file
 
*> check in root dir
move 1 to skip
move file-name(skip:) to test-name
perform check-file
 
move dir-name(skip:) to test-name
perform check-file
 
goback.
 
check-file.
call "CBL_CHECK_FILE_EXIST" using test-name file-info
if return-code equal zero then
display test-name(1:32) ": size " file-size ", "
file-year "-" file-month "-" file-day space
file-hours ":" file-minutes ":" file-seconds "."
file-hundredths
else
display "error: CBL_CHECK_FILE_EXIST " return-code space
trim(test-name)
end-if
.
 
end program check-file-exist.
</syntaxhighlight>
{{out}}
<pre>
prompt$ cobc -xj check-file-exists.cob
output.txt : size 000000000000000000, 2016-06-01 09:27:14.00
docs/ : size 000000000000004096, 2016-06-01 09:27:14.00
error: CBL_CHECK_FILE_EXIST +000000035 Abdu'l-Bahá.txt
error: CBL_CHECK_FILE_EXIST +000000035 /output.txt
error: CBL_CHECK_FILE_EXIST +000000035 /docs/
prompt$ echo -n >Abdu\'l-Bahá.txt
prompt$ cobc -xj check-file-exists.cob
output.txt : size 000000000000000000, 2016-06-01 09:27:14.00
docs/ : size 000000000000004096, 2016-06-01 09:27:14.00
Abdu'l-Bahá.txt : size 000000000000000000, 2016-06-01 09:33:35.00
error: CBL_CHECK_FILE_EXIST +000000035 /output.txt
error: CBL_CHECK_FILE_EXIST +000000035 /docs/</pre>
Errors due to file and dir not existing in root directory for this test pass
=={{header|CoffeeScript}}==
{{works with|Node.js}}
<syntaxhighlight lang="coffeescript">
fs = require 'fs'
path = require 'path'
 
root = path.resolve '/'
current_dir = __dirname
filename = 'input.txt'
dirname = 'docs'
 
local_file = path.join current_dir, filename
local_dir = path.join current_dir, dirname
 
root_file = path.join root, filename
root_dir = path.join root, dirname
 
for p in [ local_file, local_dir, root_file, root_dir ] then do ( p ) ->
fs.exists p, ( p_exists ) ->
unless p_exists
console.log "#{ p } does not exist."
else then fs.stat p, ( error, stat ) ->
console.log "#{ p } exists and is a #{ if stat.isFile() then 'file' else then 'directory' }."
 
</syntaxhighlight>
=={{header|Common Lisp}}==
 
''probe-file'' returns ''nil'' if a file does not exist. ''directory'' returns ''nil'' if there are no files in a specified directory.
<syntaxhighlight lang="lisp">(if (probe-file (make-pathname :name "input.txt"))
(print "rel file exists"))
(if (probe-file (make-pathname :directory '(:absolute "") :name "input.txt"))
(print "abs file exists"))
 
(if (directory (make-pathname :directory '(:relative "docs")))
(print "rel directory exists")
(print "rel directory is not known to exist"))
(if (directory (make-pathname :directory '(:absolute "docs")))
(print "abs directory exists")
(print "abs directory is not known to exist"))</syntaxhighlight>
 
There is no standardized way to determine if an empty directory exists, as Common Lisp dates from before the notion of directories as a type of file was near-universal. [http://www.weitz.de/cl-fad/ CL-FAD] provides many of the therefore-missing capabilities in a cross-implementation library.
 
{{libheader|CL-FAD}}
<syntaxhighlight lang="lisp">(if (cl-fad:directory-exists-p (make-pathname :directory '(:relative "docs")))
(print "rel directory exists")
(print "rel directory does not exist"))</syntaxhighlight>
=={{header|Crystal}}==
<syntaxhighlight lang="ruby">def check_file(filename : String)
if File.directory?(filename)
puts "#{filename} is a directory"
elsif File.exists?(filename)
puts "#{filename} is a file"
else
puts "#{filename} does not exist"
end
end
 
check_file("input.txt")
check_file("docs")
check_file("/input.txt")
check_file("/docs")</syntaxhighlight>
=={{header|D}}==
<syntaxhighlight lang="d">import std.stdio, std.file, std.path;
 
void verify(in string name) {
if (name.exists())
writeln("'", name, "' exists");
else
writeln("'", name, "' doesn't exist");
}
 
void main() {
// check in current working dir
verify("input.txt");
verify("docs");
 
// check in root
verify(dirSeparator ~ "input.txt");
verify(dirSeparator ~ "docs");
}</syntaxhighlight>
{{out}}
<pre>'input.txt' doesn't exist
'docs' doesn't exist
'\input.txt' doesn't exist
'\docs' doesn't exist</pre>
=={{header|DBL}}==
<syntaxhighlight lang="dbl">;
; Check file and directory exists for DBL version 4 by Dario B.
;
PROC
;------------------------------------------------------------------
XCALL FLAGS (0007000000,1) ;Suppress STOP message
 
CLOSE 1
OPEN (1,O,'TT:')
 
;The file path can be written as:
; "input.txt" (current directory)
; "/directory/input.txt" (complete path)
; "DEV:input.txt" (device DEV defined in shell)
; "$DEV/input.txt" (device DEV defined in shell)
CLOSE 2
OPEN (2,I,"input.txt") [ERR=NOFIL]
CLOSE 2
 
;Check directory (unix/linux systems)
CLOSE 2
OPEN (2,O,"/docs/.") [ERR=NODIR]
 
GOTO CLOS
 
;--------------------------------------------------------
NOFIL, DISPLAY (1,"File input.txt not found!",10)
GOTO CLOS
 
NODIR, DISPLAY (1,"Directory /docs not found!",10)
GOTO CLOS
 
CLOS, CLOSE 1
CLOSE 2
STOP</syntaxhighlight>
=={{header|DCL}}==
<syntaxhighlight lang="dcl">$ if f$search( "input.txt" ) .eqs. ""
$ then
$ write sys$output "input.txt not found"
$ else
$ write sys$output "input.txt found"
$ endif
$ if f$search( "docs.dir" ) .eqs. ""
$ then
$ write sys$output "directory docs not found"
$ else
$ write sys$output "directory docs found"
$ endif
$ if f$search( "[000000]input.txt" ) .eqs. ""
$ then
$ write sys$output "[000000]input.txt not found"
$ else
$ write sys$output "[000000]input.txt found"
$ endif
$ if f$search( "[000000]docs.dir" ) .eqs. ""
$ then
$ write sys$output "directory [000000]docs not found"
$ else
$ write sys$output "directory [000000]docs found"
$ endif</syntaxhighlight>
{{out}}
<pre>
$ @check_that_file_exists
input.txt found
directory docs not found
[000000]input.txt not found
directory [000000]docs not found</pre>
=={{header|Delphi}}==
<syntaxhighlight lang="delphi">program EnsureFileExists;
 
{$APPTYPE CONSOLE}
 
uses
SysUtils;
 
begin
if FileExists('input.txt') then
Writeln('File "input.txt" exists.')
else
Writeln('File "input.txt" does not exist.');
 
if FileExists('\input.txt') then
Writeln('File "\input.txt" exists.')
else
Writeln('File "\input.txt" does not exist.');
 
if DirectoryExists('docs') then
Writeln('Directory "docs" exists.')
else
Writeln('Directory "docs" does not exists.');
 
if DirectoryExists('\docs') then
Writeln('Directory "\docs" exists.')
else
Writeln('Directory "\docs" does not exists.');
end.</syntaxhighlight>
=={{header|E}}==
<syntaxhighlight 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!` })
}</syntaxhighlight>
=={{header|Elena}}==
ELENA 4.x :
<syntaxhighlight lang="elena">import system'io;
import extensions;
 
extension op
{
validatePath()
= self.Available.iif("exists","not found");
}
public program()
{
console.printLine("input.txt file ",File.assign("input.txt").validatePath());
console.printLine("\input.txt file ",File.assign("\input.txt").validatePath());
 
console.printLine("docs directory ",Directory.assign("docs").validatePath());
console.printLine("\docs directory ",Directory.assign("\docs").validatePath())
}</syntaxhighlight>
=={{header|Elixir}}==
<syntaxhighlight lang="elixir">File.regular?("input.txt")
File.dir?("docs")
File.regular?("/input.txt")
File.dir?("/docs")</syntaxhighlight>
=={{header|Emacs Lisp}}==
<syntaxhighlight lang="lisp">(file-exists-p "input.txt")
(file-directory-p "docs")
(file-exists-p "/input.txt")
(file-directory-p "/docs")</syntaxhighlight>
 
<code>file-exists-p</code> is true on both files and directories. <code>file-directory-p</code> is true only on directories. Both go through the <code>file-name-handler-alist</code> "magic filenames" mechanism so can act on remote files. On MS-DOS generally both <code>/</code> and <code>\</code> work to specify the root directory.
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">#!/usr/bin/escript
existence( true ) ->"exists";
existence( false ) ->"does not exist".
 
print_result(Type, Name, Flag) -> io:fwrite( "~s ~s ~s~n", [Type, Name, existence(Flag)] ).
 
 
main(_) ->
print_result( "File", "input.txt", filelib:is_regular("input.txt") ),
print_result( "Directory", "docs", filelib:is_dir("docs") ),
print_result( "File", "/input.txt", filelib:is_regular("/input.txt") ),
print_result( "Directory", "/docs", filelib:is_dir("/docs") ).
</syntaxhighlight>
=={{header|Euphoria}}==
<syntaxhighlight lang="euphoria">include file.e
 
procedure ensure_exists(sequence name)
object x
sequence s
x = dir(name)
if sequence(x) then
if find('d',x[1][D_ATTRIBUTES]) then
s = "directory"
else
s = "file"
end if
printf(1,"%s %s exists.\n",{name,s})
else
printf(1,"%s does not exist.\n",{name})
end if
end procedure
 
ensure_exists("input.txt")
ensure_exists("docs")
ensure_exists("/input.txt")
ensure_exists("/docs")</syntaxhighlight>
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">open System.IO
File.Exists("input.txt")
Directory.Exists("docs")
File.Exists("/input.txt")
Directory.Exists(@"\docs")</syntaxhighlight>
=={{header|Factor}}==
<syntaxhighlight lang="factor">: print-exists? ( path -- )
[ write ": " write ] [ exists? "exists." "doesn't exist." ? print ] bi ;
 
{ "input.txt" "/input.txt" "docs" "/docs" } [ print-exists? ] each</syntaxhighlight>
=={{header|Forth}}==
 
<syntaxhighlight lang="forth">: .exists ( str len -- )
2dup file-status nip 0= if
." exists: "
else
." does not exist: "
then
type
;
 
s" input.txt" .exists cr
s" /input.txt" .exists cr
s" docs" .exists cr
s" /docs" .exists cr
</syntaxhighlight>
 
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
 
Cannot check for directories in Fortran
<syntaxhighlight 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)</syntaxhighlight>
 
Actually, f90,f95 are able to deal with directory staff:
 
<syntaxhighlight 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</syntaxhighlight>
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
' Enable FileExists() function to be used
#include "file.bi"
 
' Use Win32 function to check if directory exists on Windows 10
Declare Function GetFileAttributes Lib "kernel32.dll" Alias "GetFileAttributesA" _
(ByVal lpFileName As ZString Ptr) As ULong
 
Const InvalidFileAttributes As ULong = -1UL
Const FileAttributeDirectory As ULong = &H10UL
 
Sub CheckFileExists(ByRef filePath As String)
If FileExists(filePath) Then
Print "'"; filePath; "' exists"
Else
Print "'"; filePath; "' does not exist"
End If
End Sub
 
Sub CheckDirectoryExists(ByVal dirPath As ZString Ptr)
Dim attrib As ULong = GetFileAttributes(dirPath)
Dim dirExists As ULong = attrib <> InvalidFileAttributes AndAlso (attrib And FileAttributeDirectory) <> 0
If dirExists Then
Print "'"; *dirPath; "' exists"
Else
Print "'"; *dirPath; "' does not exist"
End If
End Sub
 
CheckFileExists(CurDir + "\input.txt")
Dim dirPath As String = CurDir + "\docs"
CheckDirectoryExists(StrPtr(dirPath))
CheckFileExists("c:\input.txt")
CheckDirectoryExists(StrPtr("c:\docs"))
Print
Print "Press any key to quit the program"
Sleep</syntaxhighlight>
 
{{out}}
All files and directories were created first. The files are empty:
<pre>
'c:\FreeBasic\input.txt' exists
'c:\FreeBasic\docs' exists
'c:\input.txt' exists
'c:\docs' exists
</pre>
 
 
 
=={{header|Frink}}==
This checks that the file exists and is a file, and that the directory exists, and is a directory. (Many of the samples on this page do not check that the files are actually a file or the directories are actually a directory.) It also tries to find various Unicode encodings of the "unusual" filename that may be encoded in different Unicode compositions (e.g. using "precomposed" or "decomposed" representations for some characters.
 
<syntaxhighlight lang="frink">
checkFile[filename] :=
{
file = newJava["java.io.File", [filename]]
if file.exists[] and file.isFile[]
println["$filename is a file"]
else
println["$filename is not a file"]
}
 
checkDir[filename] :=
{
file = newJava["java.io.File", [filename]]
if file.exists[] and file.isDirectory[]
println["$filename is a directory"]
else
println["$filename is not a directory"]
}
 
checkFile["input.txt"]
checkFile["/input.txt"]
checkDir["docs"]
checkDir["/docs"]
 
// This tests the "unusual" filename with various Unicode
// normalizations that would look identical to a human
// For example, the á character could be written as either
// the Unicode sequences
// \u00e1 ('LATIN SMALL LETTER A WITH ACUTE')
// or
// \u0061\u0301 ('LATIN SMALL LETTER A' 'COMBINING ACUTE ACCENT')
unusual = "`Abdu'l-Bahá.txt"
checkFile[unusual]
checkFile[normalizeUnicode[unusual, "NFC"]]
checkFile[normalizeUnicode[unusual, "NFD"]]
</syntaxhighlight>
 
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
include "NSLog.incl"
 
CFURLRef url
url = fn URLFileURLWithPath( fn StringByExpandingTildeInPath(@"~/Desktop/input.txt") )
if (fn FileManagerFileExistsAtURL( url ) )
NSLog( @"File \"input.txt\" exists." )
else
NSLog( @"File \"input.txt\" does not exist at this location." )
end if
 
url = fn URLFileURLWithPath( fn StringByExpandingTildeInPath(@"~/Desktop/docs") )
if (fn FileManagerIsDirectoryAtURL( url ) )
NSLog( @"Directory \"docs\" exists." )
else
NSLog( @"Directory \"docs\" does not exist at this location." )
end if
 
HandleEvents
</syntaxhighlight>
 
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=fa06b7cc43166fb0ab48e878d86e3d1b Click this link to run this code]'''
<syntaxhighlight lang="gambas">Public Sub Main()
 
If Exist(User.Home &/ "input.txt") Then Print "'input.txt' does exist in the Home folder"
If Not Exist("/input.txt") Then Print "'input.txt' does NOT exist in Root" 'Not messing With my Root files
 
If Exist(User.home &/ "docs/") Then Print "The folder '~/docs' does exist"
If Not Exist("/docs/") Then Print "The folder '/docs' does NOT exist" 'Not messing With my Root files
 
File.Save(User.Home &/ "`Abdu'l-Bahá.txt", "")
If Exist(User.Home &/ "`Abdu'l-Bahá.txt") Then Print "'`Abdu'l-Bahá.txt' does exist (zero length and unusual name)"
 
End</syntaxhighlight>
Output:
<pre>
'input.txt' does exist in the Home folder
'input.txt' does NOT exist in Root
The folder '~/docs' does exist
The folder '/docs' does NOT exist
'`Abdu'l-Bahá.txt' does exist (zero length and unusual name)
</pre>
 
 
 
=={{header|GAP}}==
<syntaxhighlight lang="gap">IsExistingFile("input.txt");
IsDirectoryPath("docs");
IsExistingFile("/input.txt");
IsDirectoryPath("/docs");</syntaxhighlight>
=={{header|Genie}}==
<syntaxhighlight lang="genie">[indent=4]
/*
Check file exists, in Genie
valac --pkg=gio-2.0 checkFile.gs
*/
 
init
Intl.setlocale()
 
files:array of string[] = {"input.txt", "docs",
Path.DIR_SEPARATOR_S + "input.txt", Path.DIR_SEPARATOR_S + "docs", "`Abdu'l-Bahá.txt"}
for f:string in files
var file = File.new_for_path(f)
var exists = file.query_exists()
var dir = false
if exists
dir = file.query_file_type(0) == FileType.DIRECTORY
print("%s %sexist%s%s", f, exists ? "" : "does not ", exists ? "s" : "", dir ? " and is a directory" : "")</syntaxhighlight>
 
{{out}}
<pre>prompt$ valac --pkg=gio-2.0 checkFile.gs
prompt$ ./checkFile
input.txt exists
docs exists and is a directory
/input.txt does not exist
/docs does not exist
`Abdu'l-Bahá.txt does not exist</pre>
 
For the run, ''input.txt'' was zero length.
=={{header|Go}}==
<syntaxhighlight lang="go">package main
 
import (
"fmt"
"os"
)
 
func printStat(p string) {
switch i, err := os.Stat(p); {
case err != nil:
fmt.Println(err)
case i.IsDir():
fmt.Println(p, "is a directory")
default:
fmt.Println(p, "is a file")
}
}
 
func main() {
printStat("input.txt")
printStat("/input.txt")
printStat("docs")
printStat("/docs")
}</syntaxhighlight>
=={{header|Groovy}}==
<syntaxhighlight lang="groovy">println new File('input.txt').exists()
println new File('/input.txt').exists()
println new File('docs').exists()
println new File('/docs').exists()</syntaxhighlight>
=={{header|Haskell}}==
 
<syntaxhighlight lang="haskell">import System.Directory (doesFileExist, doesDirectoryExist)
 
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 :: IO ()
main = do
check doesFileExist "input.txt"
check doesDirectoryExist "docs"
check doesFileExist "/input.txt"
check doesDirectoryExist "/docs"</syntaxhighlight>
=={{header|hexiscript}}==
<syntaxhighlight lang="hexiscript">println "File \"input.txt\"? " + (exists "input.txt")
println "Dir \"docs\"? " + (exists "docs/")
println "File \"/input.txt\"? " + (exists "/input.txt")
println "Dir \"/docs\"? " + (exists "/docs/")</syntaxhighlight>
=={{header|HicEst}}==
<syntaxhighlight lang="hicest"> OPEN(FIle= 'input.txt', OLD, IOStat=ios, ERror=99)
OPEN(FIle='C:\input.txt', OLD, IOStat=ios, ERror=99)
! ...
99 WRITE(Messagebox='!') 'File does not exist. Error message ', ios </syntaxhighlight>
=={{header|HolyC}}==
<syntaxhighlight lang="holyc">U0 FileExists(U8 *f) {
if (FileFind(f) && !IsDir(f)) {
Print("'%s' file exists.\n", f);
} else {
Print("'%s' file does not exist.\n", f);
}
}
 
U0 DirExists(U8 *d) {
if (IsDir(d)) {
Print("'%s' directory exists.\n", d);
} else {
Print("'%s' directory does not exist.\n", d);
}
}
 
FileExists("input.txt");
FileExists("::/input.txt");
DirExists("docs");
DirExists("::/docs");</syntaxhighlight>
=={{header|i}}==
<syntaxhighlight lang="i">concept exists(path) {
open(path)
errors {
if error.DoesNotExist()
print(path, " does not exist!")
end
return
}
print(path, " exists!")
}
 
software {
exists("input.txt")
exists("/input.txt")
exists("docs")
exists("/docs")
exists("docs/Abdu'l-Bahá.txt")
}</syntaxhighlight>
=={{header|Icon}} and {{header|Unicon}}==
Icon doesn't support 'stat'; however, information can be obtained by use of the system function to access command line.
<syntaxhighlight lang="unicon">every dir := !["./","/"] do {
write("file ", f := dir || "input.txt", if stat(f) then " exists." else " doesn't exist.")
write("directory ", f := dir || "docs", if stat(f) then " exists." else " doesn't exist.")
}</syntaxhighlight>
Note: Icon and Unicon accept both / and \ for directory separators.
=={{header|IDL}}==
<syntaxhighlight lang="idl">
print, FILE_TEST('input.txt')
print, FILE_TEST(PATH_SEP()+'input.txt')
print, FILE_TEST('docs', /DIRECTORY)
print, FILE_TEST(PATH_SEP()+'docs', /DIRECTORY)
 
</syntaxhighlight>
=={{header|J}}==
<syntaxhighlight lang="j">require 'files'
fexist 'input.txt'
fexist '/input.txt'
direxist=: 2 = ftype
direxist 'docs'
direxist '/docs'</syntaxhighlight>
=={{header|Java}}==
This can be done with a <code>File</code> object.<br />
<syntaxhighlight lang="java">
new File("docs/input.txt").exists();
new File("/docs/input.txt").exists();
</syntaxhighlight>
Zero-length files are not a problem, and return as existent.<br />
Java supports UTF-16, so the unusual file name is not a problem.
<syntaxhighlight lang="java">
new File("`Abdu'l-Bahá.txt").exists()
</syntaxhighlight>
<syntaxhighlight lang="java">
new File("`Abdu'l-Bah\u00E1.txt").exists();
</syntaxhighlight>
<br />
Alternately
<syntaxhighlight lang="java">import java.io.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.separator + "input.txt");
test("directory", "docs");
test("directory", File.separator + "docs" + File.separator);
}
}</syntaxhighlight>
{{works with|Java|7+}}
<syntaxhighlight lang="java5">import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
public class FileExistsTest{
private static FileSystem defaultFS = FileSystems.getDefault();
public static boolean isFileExists(String filename){
return Files.exists(defaultFS.getPath(filename));
}
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", defaultFS.getSeparator() + "input.txt");
test("directory", "docs");
test("directory", defaultFS.getSeparator() + "docs" + defaultFS.getSeparator());
}
}</syntaxhighlight>
 
=={{header|JavaScript}}==
Javascript interpreters are now widely embedded in contexts which do have access to file systems, but the early context of browser scripting has precluded the inclusion of file system libraries in the definition of the language itself.
Each non-browser JS context is likely to have its own home-grown and unstandardised file system library.
===JScript===
<syntaxhighlight lang="javascript">var fso = new ActiveXObject("Scripting.FileSystemObject");
 
fso.FileExists('input.txt');
fso.FileExists('c:/input.txt');
fso.FolderExists('docs');
fso.FolderExists('c:/docs');</syntaxhighlight>
 
===macOS JavaScript for Automation===
====ES6: Sierra onwards====
{{Trans|Haskell}}
(Adopting function names used in the Haskell System.Directory library)
<syntaxhighlight lang="javascript">(() => {
 
// SYSTEM DIRECTORY FUNCTIONS
// FOR MAC OS 'JAVASCRIPT FOR AUTOMATION' SCRIPTING -----------------------
 
// doesDirectoryExist :: String -> IO Bool
const doesDirectoryExist = strPath => {
const
dm = $.NSFileManager.defaultManager,
ref = Ref();
return dm
.fileExistsAtPathIsDirectory(
$(strPath)
.stringByStandardizingPath, ref
) && ref[0] === 1;
};
 
// doesFileExist :: String -> Bool
const doesFileExist = strPath => {
var error = $();
return (
$.NSFileManager.defaultManager
.attributesOfItemAtPathError(
$(strPath)
.stringByStandardizingPath,
error
),
error.code === undefined
);
};
 
// getCurrentDirectory :: String
const getCurrentDirectory = () =>
ObjC.unwrap($.NSFileManager.defaultManager.currentDirectoryPath);
 
// getFinderDirectory :: String
const getFinderDirectory = () =>
Application('Finder')
.insertionLocation()
.url()
.slice(7);
 
// getHomeDirectory :: String
const getHomeDirectory = () =>
ObjC.unwrap($.NSHomeDirectory());
 
// setCurrentDirectory :: String -> IO ()
const setCurrentDirectory = strPath =>
$.NSFileManager.defaultManager
.changeCurrentDirectoryPath(
$(strPath)
.stringByStandardizingPath
);
 
// GENERIC FUNCTIONS FOR THE TEST -----------------------------------------
 
// A list of functions applied to a list of arguments
// <*> :: [(a -> b)] -> [a] -> [b]
const ap = (fs, xs) => //
[].concat.apply([], fs.map(f => //
[].concat.apply([], xs.map(x => [f(x)]))));
 
// show :: a -> String
const show = x => JSON.stringify(x, null, 2);
 
// TEST -------------------------------------------------------------------
return (
setCurrentDirectory('~/Desktop'),
show(ap(
[doesFileExist, doesDirectoryExist],
['input.txt', '/input.txt', 'docs', '/docs']
))
);
})();</syntaxhighlight>
{{Out}}
The first four booleans are returned by doesFileExist – the last four by
doesDirectoryExist, which returns false in the case of files which do
exist but are not directories.
<pre>[
true,
true,
true,
true,
false,
false,
true,
true
]</pre>
=={{header|Julia}}==
{{works with|Julia|0.6}}
 
<syntaxhighlight lang="julia">@show isfile("input.txt")
@show isfile("/input.txt")
@show isdir("docs")
@show isdir("/docs")
@show isfile("")
@show isfile("`Abdu'l-Bahá.txt")</syntaxhighlight>
=={{header|Klingphix}}==
<syntaxhighlight lang="klingphix">include ..\Utilitys.tlhy
 
"foo.bar" "w" fopen
"Hallo !" over fputs
fclose
"fou.bar" "r" fopen
dup 0 < ( ["Could not open 'fou.bar' for reading" print drop] [fclose] ) if
 
" " input</syntaxhighlight>
{{out}}
<pre>Could not open 'fou.bar' for reading</pre>
=={{header|Kotlin}}==
<syntaxhighlight lang="scala">// version 1.0.6
 
import java.io.File
 
fun main(args: Array<String>) {
val filePaths = arrayOf("input.txt", "c:\\input.txt", "zero_length.txt", "`Abdu'l-Bahá.txt")
val dirPaths = arrayOf("docs", "c:\\docs")
for (filePath in filePaths) {
val f = File(filePath)
println("$filePath ${if (f.exists() && !f.isDirectory) "exists" else "does not exist"}")
}
publicfor static(dirPath void main(Stringin args[]dirPaths) {
val test("file",d "input.txt"= File(dirPath);
println("$dirPath ${if (d.exists() && d.isDirectory) "exists" else "does not exist"}")
test("file", File.seperator + "input.txt");
test("directory", "docs");
test("directory", File.seperator + "docs" + File.seperator);
}
}</syntaxhighlight>
}
=={{header|LabVIEW}}==
{{libheader|LabVIEW CWD}}
{{VI snippet}}<br/>
[[File:Ensure_that_a_file_exists.png]]
 
==[[MAXScript]]==
[[Category:MAXScript]]
-- Here
doesFileExist "input.txt"
(getDirectories "docs").count == 1
-- Root
doesFileExist "\input.txt"
(getDirectories "C:\docs").count == 1
 
==[[PHP]]{{header|Lang}}==
{{libheader|lang-io-module}}
[[Category:PHP]]
<syntaxhighlight lang="lang">
// List of files.
# Load the IO module
$files = array('./input.txt','./docs','/input.txt','/docs');
# Replace "<pathToIO.lm>" with the location where the io.lm Lang module was installed to without "<" and ">"
// Process Each File
ln.loadModule(<pathToIO.lm>)
foreach($files as $fileToCheck){
 
// List Findings
$file1 = [[io]]::fp.openFile(input.txt)
print "The file ";
[[io]]::fp.existsFile($file1)
if( file_exists($fileToCheck) ){
[[io]]::fp.closeFile($file1)
print $fileToCheck." exists.\n";
 
}else{
$file2 = [[io]]::fp.openFile(/input.txt)
print $fileToCheck." does not exist.\n";
[[io]]::fp.existsFile($file2)
}
[[io]]::fp.closeFile($file2)
}
 
$dir1 = [[io]]::fp.openFile(docs)
[[io]]::fp.existsFile($dir1)
[[io]]::fp.closeFile($dir1)
 
$dir2 = [[io]]::fp.openFile(/docs)
[[io]]::fp.existsFile($dir2)
[[io]]::fp.closeFile($dir2)
</syntaxhighlight>
 
=={{header|langur}}==
The prop() function returns a hash of file/directory properties.
<syntaxhighlight lang="langur">val .printresult = impure fn(.file) {
write .file, ": "
if val .p = prop(.file) {
if .p'isdir {
writeln "is directory"
} else {
writeln "is file"
}
} else {
writeln "nothing"
}
}
 
.printresult("input.txt")
.printresult("/input.txt")
.printresult("docs")
.printresult("/docs")
</syntaxhighlight>
 
=={{header|Lasso}}==
<syntaxhighlight lang="lasso">// local file
file_exists('input.txt')
 
// local directory
file_exists('docs')
 
// file in root file system (requires permissions at user OS level)
file_exists('//input.txt')
 
// directory in root file system (requires permissions at user OS level)
file_exists('//docs')</syntaxhighlight>
=={{header|LFE}}==
From the LFE REPL:
<syntaxhighlight lang="lisp">
> (: filelib is_regular '"input.txt")
false
> (: filelib is_dir '"docs")
false
> (: filelib is_regular '"/input.txt")
false
> (: filelib is_dir '"/docs"))
false
</syntaxhighlight>
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">'fileExists.bas - Show how to determine if a file exists
dim info$(10,10)
input "Type a file path (ie. c:\windows\somefile.txt)?"; fpath$
if fileExists(fpath$) then
print fpath$; " exists!"
else
print fpath$; " doesn't exist!"
end if
end
 
'return a true if the file in fullPath$ exists, else return false
function fileExists(fullPath$)
files pathOnly$(fullPath$), filenameOnly$(fullPath$), info$()
fileExists = val(info$(0, 0)) > 0
end function
 
'return just the directory path from a full file path
function pathOnly$(fullPath$)
pathOnly$ = fullPath$
while right$(pathOnly$, 1) <> "\" and pathOnly$ <> ""
pathOnly$ = left$(pathOnly$, len(pathOnly$)-1)
wend
end function
 
'return just the filename from a full file path
function filenameOnly$(fullPath$)
pathLength = len(pathOnly$(fullPath$))
filenameOnly$ = right$(fullPath$, len(fullPath$)-pathLength)
end function</syntaxhighlight>
=={{header|Little}}==
<syntaxhighlight lang="c">if (exists("input.txt")) {
puts("The file \"input.txt\" exist");
}
if (exists("/input.txt")) {
puts("The file \"/input.txt\" exist");
}
if (exists("docs")) {
puts("The file \"docs\" exist");
}
if (exists("/docs")) {
puts("The file \"/docs\" exist");
}</syntaxhighlight>
=={{header|LiveCode}}==
<syntaxhighlight lang="livecode">there is a file "/input.txt"
there is a file "input.txt"
there is a folder "docs"
there is a file "/docs/input.txt"</syntaxhighlight>
LiveCode also allows setting a default folder for subsequent file commands. To check if a file exists in the doc folder
<syntaxhighlight lang="livecode">set the defaultFolder to "docs"
there is a file "input.txt"</syntaxhighlight>
=={{header|Logo}}==
{{works with|UCB Logo}}
<syntaxhighlight lang="logo">show file? "input.txt
show file? "/input.txt
show file? "docs
show file? "/docs</syntaxhighlight>
Alternatively, one can set a file prefix used for subsequent file commands.
<syntaxhighlight lang="logo">setprefix "/
show file? "input.txt</syntaxhighlight>
=={{header|Lua}}==
For directories, the following only works on platforms on which directories can be opened for reading like files.
<syntaxhighlight lang="lua">function output( s, b )
if b then
print ( s, " does not exist." )
else
print ( s, " does exist." )
end
end
 
output( "input.txt", io.open( "input.txt", "r" ) == nil )
output( "/input.txt", io.open( "/input.txt", "r" ) == nil )
output( "docs", io.open( "docs", "r" ) == nil )
output( "/docs", io.open( "/docs", "r" ) == nil )</syntaxhighlight>
 
The following more portable solution uses LuaFileSystem.
<syntaxhighlight lang="lua">require "lfs"
for i, path in ipairs({"input.txt", "/input.txt", "docs", "/docs"}) do
local mode = lfs.attributes(path, "mode")
if mode then
print(path .. " exists and is a " .. mode .. ".")
else
print(path .. " does not exist.")
end
end</syntaxhighlight>
=={{header|M2000 Interpreter}}==
Report print proportional text using word wrap, and justification. Can be used to calculate lines, and to render form a line, a number of lines. We can specify the width of the text, and by moving the cursor horizontal we can specify the left margin. This statement can be used to any layer, including user forms and printer page.
 
<syntaxhighlight lang="m2000 interpreter">
Module ExistDirAndFile {
Let WorkingDir$=Dir$, RootDir$="C:\"
task(WorkingDir$)
task(RootDir$)
Dir User ' return to user directroy
Sub task(WorkingDir$)
Local counter
Dir WorkingDir$
If Not Exist.Dir("docs") then Report "docs not exist in "+WorkingDir$ : counter++
If Not Exist("output.txt") Then {
Report "output.txt not exist in "+ WorkingDir$ : counter++
} Else.if Filelen("output.txt")=0 Then Report "output.txt has zero length"
If counter =0 then Report WorkingDir$+ " has docs directory and file output.txt"
End Sub
}
ExistDirAndFile
</syntaxhighlight>
=={{header|Maple}}==
<syntaxhighlight lang="maple">with(FileTools):
Exists("input.txt");
Exists("docs") and IsDirectory("docs");
Exists("/input.txt");
Exists("/docs") and IsDirectory("/docs");</syntaxhighlight>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">wd = NotebookDirectory[];
FileExistsQ[wd <> "input.txt"]
DirectoryQ[wd <> "docs"]
 
FileExistsQ["/" <> "input.txt"]
DirectoryQ["/" <> "docs"]</syntaxhighlight>
=={{header|MATLAB}} / {{header|Octave}}==
<syntaxhighlight lang="matlab"> exist('input.txt','file')
exist('/input.txt','file')
exist('docs','dir')
exist('/docs','dir')</syntaxhighlight>
=={{header|MAXScript}}==
<syntaxhighlight lang="maxscript">-- Here
doesFileExist "input.txt"
(getDirectories "docs").count == 1
-- Root
doesFileExist "\input.txt"
(getDirectories "C:\docs").count == 1</syntaxhighlight>
=={{header|Modula-3}}==
<syntaxhighlight lang="modula3">MODULE FileTest EXPORTS Main;
 
IMPORT IO, Fmt, FS, File, OSError, Pathname;
 
PROCEDURE FileExists(file: Pathname.T): BOOLEAN =
VAR status: File.Status;
BEGIN
TRY
status := FS.Status(file);
RETURN TRUE;
EXCEPT
| OSError.E => RETURN FALSE;
END;
END FileExists;
 
BEGIN
IO.Put(Fmt.Bool(FileExists("input.txt")) & "\n");
IO.Put(Fmt.Bool(FileExists("/input.txt")) & "\n");
IO.Put(Fmt.Bool(FileExists("docs/")) & "\n");
IO.Put(Fmt.Bool(FileExists("/docs")) & "\n");
END FileTest.</syntaxhighlight>
=={{header|Nanoquery}}==
<syntaxhighlight lang="nanoquery">import Nanoquery.IO
 
def exists(fname)
f = new(File, fname)
 
return f.exists()
end</syntaxhighlight>
=={{header|Neko}}==
<syntaxhighlight lang="actionscript">/**
Check that file/dir exists, in Neko
*/
 
var sys_exists = $loader.loadprim("std@sys_exists", 1)
var sys_file_type = $loader.loadprim("std@sys_file_type", 1)
var sys_command = $loader.loadprim("std@sys_command", 1)
 
var name = "input.txt"
$print(name, " exists as file: ", sys_exists(name), "\n")
 
$print(name = "docs", " exists as dir: ", sys_exists(name) && sys_file_type(name) == "dir", "\n")
$print(name = "neko", " exists as dir: ", sys_exists(name) && sys_file_type(name) == "dir", "\n")
 
$print(name = "/input.txt", " exists as file: ", sys_exists(name) && sys_file_type(name) == "file", "\n")
$print(name = "/docs", " exists as dir: ", sys_exists(name) && sys_file_type(name) == "dir", "\n")
$print(name = "/tmp", " exists as dir: ", sys_exists(name) && sys_file_type(name) == "dir", "\n")
 
/* bonus round */
name = "empty.txt"
var stat_size = $loader.loadprim("std@sys_stat", 1)(name).size
$print(name, " exists as empty file: ", sys_exists(name) && stat_size == 0, "\n")
 
name = "`Abdu'l-Bahá.txt"
$print(name, " exists as file: ", sys_exists(name) && sys_file_type(name) == "file", "\n")</syntaxhighlight>
 
{{out}}
<pre>prompt$ nekoc exists.neko
prompt$ neko exists.n
input.txt exists as file: true
docs exists as dir: false
neko exists as dir: true
/input.txt exists as file: false
/docs exists as dir: false
/tmp exists as dir: true
empty.txt exists as empty file: true
`Abdu'l-Bahá.txt exists as file: true</pre>
=={{header|Nemerle}}==
{{trans|C#}}
<syntaxhighlight lang="nemerle">using System.Console;
using System.IO;
WriteLine(File.Exists("input.txt"));
WriteLine(File.Exists("/input.txt"));
WriteLine(Directory.Exists("docs"));
WriteLine(Directory.Exists("/docs"));</syntaxhighlight>
=={{header|NetRexx}}==
<syntaxhighlight lang="netrexx">/* NetRexx */
options replace format comments java crossref symbols binary
 
runSample(arg)
return
 
-- . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
method isExistingFile(fn) public static returns boolean
ff = File(fn)
fExists = ff.exists() & ff.isFile()
return fExists
 
-- . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
method isExistingDirectory(fn) public static returns boolean
ff = File(fn)
fExists = ff.exists() & ff.isDirectory()
return fExists
 
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method runSample(arg) private static
parse arg files
if files = '' then files = 'input.txt F docs D /input.txt F /docs D'
loop while files.length > 0
parse files fn ft files
select case(ft.upper())
when 'F' then do
if isExistingFile(fn) then ex = 'exists'
else ex = 'does not exist'
say 'File '''fn'''' ex
end
when 'D' then do
if isExistingDirectory(fn) then ex = 'exists'
else ex = 'does not exist'
say 'Directory '''fn'''' ex
end
otherwise do
if isExistingFile(fn) then ex = 'exists'
else ex = 'does not exist'
say 'File '''fn'''' ex
end
end
end
 
return
</syntaxhighlight>
=={{header|NewLISP}}==
<syntaxhighlight lang="newlisp">(dolist (file '("input.txt" "/input.txt"))
(if (file? file true)
(println "file " file " exists")))
 
(dolist (dir '("docs" "/docs"))
(if (directory? dir)
(println "directory " dir " exists")))</syntaxhighlight>
=={{header|Nim}}==
<syntaxhighlight lang="nim">import os
 
echo fileExists "input.txt"
echo fileExists "/input.txt"
echo dirExists "docs"
echo dirExists "/docs"</syntaxhighlight>
=={{header|Objeck}}==
<syntaxhighlight lang="objeck">
use IO;
 
bundle Default {
class Test {
function : Main(args : String[]) ~ Nil {
File->Exists("input.txt")->PrintLine();
File->Exists("/input.txt")->PrintLine();
Directory->Exists("docs")->PrintLine();
Directory->Exists("/docs")->PrintLine();
}
}
</syntaxhighlight>
 
=={{header|Objective-C}}==
<syntaxhighlight lang="objc">NSFileManager *fm = [NSFileManager defaultManager];
NSLog(@"input.txt %s", [fm fileExistsAtPath:@"input.txt"] ? @"exists" : @"doesn't exist");
NSLog(@"docs %s", [fm fileExistsAtPath:@"docs"] ? @"exists" : @"doesn't exist");</syntaxhighlight>
 
=={{header|Odin}}==
 
<syntaxhighlight lang="odin">package main
 
import "core:os"
 
main :: proc() {
os.exists("input.txt")
os.exists("/input.txt")
os.exists("docs")
os.exists("/docs")
}</syntaxhighlight>
 
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">Sys.file_exists "input.txt";;
Sys.file_exists "docs";;
Sys.file_exists "/input.txt";;
Sys.file_exists "/docs";;</syntaxhighlight>
=={{header|ooRexx}}==
<syntaxhighlight lang="oorexx">/**********************************************************************
* exists(filespec)
* returns 1 if filespec identifies a file with size>0
* (a file of size 0 is deemed not to exist.)
* or a directory
* 0 otherwise
* 09.06.2013 Walter Pachl (retrieved from my toolset)
**********************************************************************/
exists:
parse arg spec
call sysfiletree spec, 'LIST', 'BL'
if list.0\=1 then return 0 -- does not exist
parse var list.1 . . size flags .
if size>0 then return 1 -- real file
If substr(flags,2,1)='D' Then Do
Say spec 'is a directory'
Return 1
End
If size=0 Then Say spec 'is a zero-size file'
Return 0</syntaxhighlight>
=={{header|Oz}}==
<syntaxhighlight lang="oz">declare
[Path] = {Module.link ['x-oz://system/os/Path.ozf']}
in
{Show {Path.exists "docs"}}
{Show {Path.exists "input.txt"}}
{Show {Path.exists "/docs"}}
{Show {Path.exists "/input.txt"}}</syntaxhighlight>
=={{header|PARI/GP}}==
<syntaxhighlight lang="parigp">trap(,"does not exist",read("input.txt");"exists")
trap(,"does not exist",read("c:\\input.txt");"exists")
trap(,"does not exist",read("c:\\dirname\\nul");"exists")</syntaxhighlight>
 
A better version would use <code>externstr</code>.
 
Under PARI it would typically be more convenient to use [[#C|C]] methods.
==[[Perl]]==
=={{header|Pascal}}==
[[Category:Perl]]
See [[Ensure_that_a_file_exists#Delphi | Delphi]]
use File::Spec::Functions qw(catfile rootdir);
=={{header|Perl}}==
# here
<syntaxhighlight lang="perl">use File::Spec::Functions qw(catfile rootdir);
print -e 'input.txt';
# here
print -d 'docs';
print -e 'input.txt';
# root dir
print -e catfile rootdir,d 'input.txtdocs';
# root dir
print -d catfile rootdir, 'docs';
print -e catfile rootdir, 'input.txt';
print -d catfile rootdir, 'docs';</syntaxhighlight>
 
'''Without a Perl Module'''
Line 94 ⟶ 2,355:
perl -e 'print -e "/input.txt", "\n";'
perl -e 'print -d "/docs", "\n";'
=={{header|Phix}}==
{{libheader|Phix/basics}}
<!--<syntaxhighlight lang="phix">(notonline)-->
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (file i/o)</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">check</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">name</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">bool</span> <span style="color: #000000;">bExists</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">file_exists</span><span style="color: #0000FF;">(</span><span style="color: #000000;">name</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">bDir</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">get_file_type</span><span style="color: #0000FF;">(</span><span style="color: #000000;">name</span><span style="color: #0000FF;">)=</span><span style="color: #004600;">FILETYPE_DIRECTORY</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">exists</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">bExists</span><span style="color: #0000FF;">?</span><span style="color: #008000;">"exists"</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"does not exist"</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">dfs</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">bExists</span><span style="color: #0000FF;">?</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">bDir</span><span style="color: #0000FF;">?</span><span style="color: #008000;">"directory "</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"file "</span><span style="color: #0000FF;">):</span><span style="color: #008000;">""</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s%s %s.\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">dfs</span><span style="color: #0000FF;">,</span><span style="color: #000000;">name</span><span style="color: #0000FF;">,</span><span style="color: #000000;">exists</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">check</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"input.txt"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">check</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"docs"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">check</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"/input.txt"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">check</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"/docs"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">check</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"/pagefile.sys"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">check</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"/Program Files (x86)"</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
file input.txt exists.
directory docs exists.
/input.txt does not exist.
/docs does not exist.
file /pagefile.sys exists.
directory /Program Files (x86) exists.
</pre>
=={{header|Phixmonti}}==
<syntaxhighlight lang="phixmonti">"foo.bar" "w" fopen
"Hallo !" over fputs
fclose
 
"fou.bar" "r" fopen
dup 0 < if "Could not open 'foo.bar' for reading" print drop else fclose endif</syntaxhighlight>
=={{header|PHP}}==
<syntaxhighlight lang="php">if (file_exists('input.txt')) echo 'input.txt is here right by my side';
if (file_exists('docs' )) echo 'docs is here with me';
if (file_exists('/input.txt')) echo 'input.txt is over there in the root dir';
if (file_exists('/docs' )) echo 'docs is over there in the root dir';</syntaxhighlight>
=={{header|PicoLisp}}==
<syntaxhighlight lang="picolisp">(if (info "file.txt")
(prinl "Size: " (car @) " bytes, last modified " (stamp (cadr @) (cddr @)))
(prinl "File doesn't exist") )
 
# for directory existing
# Nehal-Singhal 2018-05-25
 
(if (info "./docs")
(print 'exists)
(print 'doesNotExist)))
 
# To verify if it's really a directory, (CAR of return value will be 'T').
# abu 2018-05-25
 
(let I (info "./docs")
(prinl
(nond
(I "Does not exist")
((=T (car I)) "Is not a directory")
(NIL "Directory exists") ) ) )
</syntaxhighlight>
=={{header|Pike}}==
<syntaxhighlight lang="pike">import Stdio;
 
int main(){
if(exist("/var")){
write("/var exists!\n");
}
 
if(exist("file-exists.pike")){
write("I exist!\n");
}
}</syntaxhighlight>
=={{header|PL/I}}==
<syntaxhighlight lang="pli">*Process source or(!);
/*********************************************************************
* 20.10.2013 Walter Pachl
* 'set dd:f=d:\_l\xxx.txt,recsize(300)'
* 'tex'
*********************************************************************/
tex: Proc Options(main);
Dcl fid Char(30) Var Init('D:\_l\tst.txt');
Dcl xxx Char(30) Var Init('D:\_l\nix.txt');
Dcl r Char(1000) Var;
Dcl f Record input;
On Undefinedfile(f) Goto label;
Open File(f) Title('/'!!fid);
Read file(f) Into(r);
Put Skip List('First line of file '!!fid!!': '!!r);
Close File(f);
Open File(f) Title('/'!!xxx);
Read file(f) Into(r);
Put Skip List(r);
Close File(f);
Label: Put Skip List('File '!!xxx!!' not found');
End;</syntaxhighlight>
{{out}}
<pre>
First line of file D:\_l\tst.txt: Test line 1
File D:\_l\nix.txt not found
</pre>
=={{header|PL/M}}==
This sample assumes that the original 8080 PL/M compiler is used and that the program will be running under CP/M.
CP/M doesn't have a hierarchical file-system, so there are no folders or directories. This sample looks for INPUT.TXT on the current drive and also on the D: drive. An error will occur if there is no D: drive, so it is best not to run this if you haven't got one... :)
<br>Note that CP/M restricts file names to 7-bit ascii upper-case and not all non-letter, non-digit characters can be used.
<br>CP/M filenames are up to 8 characters long with an optional, up to three character extension.
<syntaxhighlight lang="plm">100H:
 
DECLARE FCB$SIZE LITERALLY '36';
 
BDOS: PROCEDURE( FN, ARG )BYTE; /* CP/M BDOS SYSTEM CALL, RETURNS A VALUE */
DECLARE FN BYTE, ARG ADDRESS;
GOTO 5;
END BDOS;
BDOS$P: PROCEDURE( FN, ARG ); /* CP/M BDOS SYSTEM CALL, NO RETURN VALUE */
DECLARE FN BYTE, ARG ADDRESS;
GOTO 5;
END BDOS$P;
PRINT$CHAR: PROCEDURE( C ); DECLARE C BYTE; CALL BDOS$P( 2, C ); END;
PRINT$STRING: PROCEDURE( S ); DECLARE S ADDRESS; CALL BDOS$P( 9, S ); END;
PRINT$NL: PROCEDURE; CALL PRINT$STRING( .( 0DH, 0AH, '$' ) ); END;
SEARCH$FIRST: PROCEDURE( FCB )BYTE; /* RETURN 0, 1, 2, 3 IF FILE IN FCB */
DECLARE FCB ADDRESS; /* EXISTS, 255 OTHERWISE */
RETURN BDOS( 17, FCB );
END SEARCH$FIRST ;
 
INIT$FCB: PROCEDURE( FCB, NAME ); /* INITIALISE A FILE-CONTROL-BLOCK */
DECLARE ( FCB, NAME ) ADDRESS; /* SETTING THE FILE NAME */
DECLARE ( F$PTR, N$PTR, X$PTR ) ADDRESS;
DECLARE F BASED F$PTR BYTE, N BASED N$PTR BYTE;
DECLARE BLANKS ( 5 )BYTE INITIAL( ' ', ' ', ' ', ' ', '$' );
X$PTR = .BLANKS;
N$PTR = NAME + 1;
F$PTR = FCB;
IF N <> ':' THEN DO; /* NO DRIVE LETTER */
F = 0;
N$PTR = NAME;
END;
ELSE DO; /* FIRST CHAR IS THE DRIVE LETTER */
N$PTR = NAME;
F = ( N + 1 ) - 'A';
N$PTR = N$PTR + 2;
END;
DO F$PTR = FCB + 1 TO FCB + 8; /* NAME */
IF N = '$' THEN N$PTR = .BLANKS;
ELSE IF N = '.' THEN DO; /* START OF THE EXTENSION */
X$PTR = N$PTR + 1;
N$PTR = .BLANKS;
END;
F = N;
N$PTR = N$PTR + 1;
END;
N$PTR = X$PTR; /* EXTENSION */
DO F$PTR = FCB + 9 TO FCB + 11;
IF N = '$' THEN N$PTR =.BLANKS;
F = N;
N$PTR = N$PTR + 1;
END;
DO F$PTR = FCB + 12 TO FCB + ( FCB$SIZE - 1 ); /* OTHER FIELDS */
F = 0;
END;
END INIT$FCB ;
 
EXISTS: PROCEDURE( FCB )BYTE; /* RETURNS TRUE IF THE FILE NAMED IN THE */
DECLARE FCB ADDRESS; /* FCB EXISTS */
RETURN ( SEARCH$FIRST( FCB ) < 4 );
END EXISTS ;
 
DECLARE FCB$1$DATA ( FCB$SIZE )BYTE; /* DECLARE A FILE-CONTROL-BLOCK */
DECLARE FCB$1 ADDRESS;
FCB$1 = .FCB$1$DATA;
 
/* CP/M DOES NOT HAVE DIRECTORIES/FOLDERS - THIS TESTS FOR INPUT.TXT IN */
/* THE CURRENT DEFAULT DRIVE */
CALL INIT$FCB( FCB$1, .'INPUT.TXT$' );
CALL PRINT$STRING( .'INPUT.TXT: $' );
IF EXISTS( FCB$1 ) THEN CALL PRINT$STRING( .'EXISTS$' );
ELSE CALL PRINT$STRING( .'DOES NOT EXIST$' );
CALL PRINT$NL;
 
/* CHECK FOR INPUT.TXT IN THE D: DRIVE */
/* !!! THIS WILL CAUSE AN ERROR IF THERE IS NO DRIVE D: !!! */
/* !!! OR THERE IS NO DISC IN DRIVE D: !!! */
CALL INIT$FCB( FCB$1, .'D:INPUT.TXT$' );
CALL PRINT$STRING( .'D:INPUT.TXT: $' );
IF EXISTS( FCB$1 ) THEN CALL PRINT$STRING( .'EXISTS$' );
ELSE CALL PRINT$STRING( .'DOES NOT EXIST$' );
CALL PRINT$NL;
 
EOF</syntaxhighlight>
{{out}}
Assuming there is no INPUT.TXT on the current drive, but there is one on D:.
<pre>
INPUT.TXT: DOES NOT EXIST
D:INPUT.TXT: EXISTS
</pre>
Assuming there is no INPUT.TXT on the current drive and there os no D: drive.
<pre>
INPUT.TXT: DOES NOT EXIST
D:INPUT.TXT:
Bdos Err on A: Select
</pre>
 
=={{header|Plain English}}==
{{libheader|Plain English-output}}
<syntaxhighlight lang="text">
To run:
Start up.
\ In the current working directory
Check that ".\input.txt" is in the file system.
Check that ".\docs\" is in the file system.
\ In the filesystem root
Check that "C:\input.txt" is in the file system.
Check that "C:\docs\" is in the file system.
Wait for the escape key.
Shut down.
 
To check that a path is in the file system:
If the path is in the file system, write the path then " exists" to the output; exit.
If the path is not in the file system, write the path then " does not exist" to the output; exit.
</syntaxhighlight>
 
=={{header|Pop11}}==
 
<syntaxhighlight lang="pop11">sys_file_exists('input.txt') =>
sys_file_exists('/input.txt') =>
sys_file_exists('docs') =>
sys_file_exists('/docs') =></syntaxhighlight>
 
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:
 
<syntaxhighlight 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;</syntaxhighlight>
 
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):
 
http://wwwcgi.rdg.ac.uk:8081/cgi-bin/cgiwrap/wsi14/poplog/pop11/ref/sysio
 
http://www.poplog.org/docs/popdocs/pop11/ref/sysio
 
http://www.cs.bham.ac.uk/research/projects/poplog/doc/popref/sysio
(Not so well formatted).
 
Users can easily define special cases of the general procedure.
=={{header|PowerShell}}==
 
<syntaxhighlight lang="powershell"> if (Test-Path -Path .\input.txt) {
write-host "File input exist"
}
else {
write-host "File input doesn't exist"
}</syntaxhighlight>
=={{header|Prolog}}==
 
{{works with|SWI-Prolog|6.6}}
 
<syntaxhighlight lang="prolog">
 
exists_file('input.txt'),
exists_directory('docs').
 
exits_file('/input.txt'),
exists_directory('/docs').
 
</syntaxhighlight>
=={{header|PureBasic}}==
<syntaxhighlight lang="purebasic">result = ReadFile(#PB_Any, "input.txt")
If result>0 : Debug "this local file exists"
Else : Debug "result=" +Str(result) +" so this local file is missing"
EndIf
 
result = ReadFile(#PB_Any, "/input.txt")
If result>0 : Debug "this root file exists"
Else : Debug "result=" +Str(result) +" so this root file is missing"
EndIf
 
result = ExamineDirectory(#PB_Any,"docs","")
If result>0 : Debug "this local directory exists"
Else : Debug "result=" +Str(result) +" so this local directory is missing"
EndIf
 
result = ExamineDirectory(#PB_Any,"/docs","")
If result>0 : Debug "this root directory exists"
Else : Debug "result=" +Str(result) +" so this root directory is missing"
EndIf </syntaxhighlight>
=={{header|Python}}==
 
<syntaxhighlight lang="python">import os
 
os.path.isfile("input.txt")
os.path.isfile("/input.txt")
os.path.isdir("docs")
os.path.isdir("/docs")</syntaxhighlight>
 
The more generic [https://docs.python.org/3/library/os.path.html#os.path.exists <code>os.path.exists(path)</code>] function will return True if the path exists, being it either a regular file or a directory.
=={{header|QB64}}==
<syntaxhighlight lang="qbasic">$NOPREFIX
PRINT DIREXISTS("docs")
PRINT DIREXISTS("\docs")
PRINT FILEEXISTS("input.txt")
PRINT FILEEXISTS("\input.txt")</syntaxhighlight>
=={{header|R}}==
<syntaxhighlight lang="r">file.exists("input.txt")
file.exists("/input.txt")
file.exists("docs")
file.exists("/docs")
 
# or
file.exists("input.txt", "/input.txt", "docs", "/docs")</syntaxhighlight>
 
The function <tt>file.exists</tt> returns a logical value (or a vector of logical values if more than one argument is passed)
 
This works with special names:
 
<syntaxhighlight lang="r">file.exists("`Abdu'l-Bahá.txt")</syntaxhighlight>
=={{header|Racket}}==
 
<syntaxhighlight lang="racket">
#lang racket
 
;; here
(file-exists? "input.txt")
(file-exists? "docs")
 
;; in the root
(file-exists? "/input.txt")
(file-exists? "/docs")
 
;; or in the root with relative paths
(parameterize ([current-directory "/"])
(and (file-exists? "input.txt")
(file-exists? "docs")))
</syntaxhighlight>
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" line>
my $path = "/etc/passwd";
say $path.IO.e ?? "Exists" !! "Does not exist";
 
given $path.IO {
when :d { say "$path is a directory"; }
when :f { say "$path is a regular file"; }
when :e { say "$path is neither a directory nor a file, but it does exist"; }
default { say "$path does not exist" }
}</syntaxhighlight>
 
<code>when</code> internally uses the smart match operator <code>~~</code>, so <code>when :e</code> really does <code>$given ~~ :e</code> instead of the method call <code>$given.e</code>; both test whether the file exists.
 
<syntaxhighlight lang="raku" line>
run ('touch', "♥ Unicode.txt");
 
say "♥ Unicode.txt".IO.e; # "True"
say "♥ Unicode.txt".IO ~~ :e; # same
</syntaxhighlight>
=={{header|Raven}}==
 
<syntaxhighlight 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</syntaxhighlight>
=={{header|REBOL}}==
<syntaxhighlight lang="rebol">exists? %input.txt
exists? %docs/
 
exists? %/input.txt
exists? %/docs/</syntaxhighlight>
=={{header|Red}}==
<syntaxhighlight lang="red">exists? %input.txt
exists? %docs/
exists? %/c/input.txt
exists? %/c/docs/
exists? %//input.txt
exists? %//docs/
 
>> exists? %`Abdu'l-Bahá.txt
== true</syntaxhighlight>
=={{header|REXX}}==
===version 1===
{{works with|PC/REXX}}
{{works with|Personal REXX}}
{{works with|Regina}}
<syntaxhighlight lang="rexx">/*REXX program creates a new empty file and directory in current directory and root dir.*/
fn= 'input.txt' /*default name of a file. */
dn= 'docs' /*default name of a directory (folder).*/
@.1= 'current directory'; @.2= 'root directory' /*messages used to indicate which pass.*/
parse upper version v /*obtain name of the REXX being used. */
regina= pos('REGINA' , v)\==0 /*is this the Regina REXX being used? */
r4 = pos('REXX-R4' , v)\==0 /*is this the R4 REXX being used? */
@doesnt= "doesn't exist in the"
@does = "does exist in the"
 
do j=1 for 2; say /* [↑] perform these statements twice.*/
if stream(fn, 'C', "QUERY EXISTS")=='' then say 'file ' fn @doesnt @.j
else say 'file ' fn @does @.j
 
if j==2 then iterate
if stream(dn, 'C', "QUERY EXISTS")=='' then say 'directory' dn @doesnt @.j
else say 'directory' dn @does @.j
if j==1 then select
when regina then call chdir '\' /*use Regina's version of CHDIR. */
when r4 then call stream '\', "C", 'CHDIR' /*R4's version. */
otherwise call doschdir '\' /*PC/REXX & Personal REXX version.*/
end /*select*/
end /*j*/ /*stick a fork in it, we're all done. */</syntaxhighlight>
 
===version 2===
{{works with|ARexx}}
{{works with|Regina 3.8 and later, with options: &nbsp; AREXX_BIFS &nbsp; AREXX_SEMANTICS}}
<syntaxhighlight lang="rexx">
/* Check if a file already exists */
filename='file.txt'
IF ~Openfile(filename) THEN CALL Openfile(':'filename)
EXIT 0
Openfile:
IF ~Exists(filename) THEN RETURN 0
CALL Open(filehandle,filename,'APPEND')
RETURN 1
</syntaxhighlight>
=={{header|Ring}}==
<syntaxhighlight lang="ring">
aFile = "C:\Ring\ReadMe.txt"
see aFile
if Fexists(aFile) see " exists" + nl
else see " doesn't exist" + nl ok
</syntaxhighlight>
=={{header|RLaB}}==
 
RLaB provides two user functions for the task, ''isfile'' and ''isdir''.
<syntaxhighlight lang="rlab">
>> isdir("docs")
0
>> isfile("input.txt")
0
</syntaxhighlight>
=={{header|RPL}}==
The 2 functions below take a word as an argument and return a boolean stating the presence or not of a 'file' (a 'variable' in RPL jargon) or a directory corresponding to the word.
« VARS SWAP POS
» '<span style="color:blue">ISHERE?</span>' STO
« PATH HOME
==[[Pop11]]==
VARS ROT POS
[[Category:Pop11]]
SWAP EVAL <span style="color:grey">@ Back to initial directory</span>
» '<span style="color:blue">ISHOME?</span>' STO
 
=={{header|Ruby}}==
sys_file_exists('input.txt') =>
<code>File.exist?</code> only checks if a file exists; it can be a regular file, a directory, or something else. <code>File.file?</code> or <code>File.directory?</code> checks for a regular file or a directory. Ruby also allows <code>FileTest.file?</code> or <code>FileTest.directory?</code>.
sys_file_exists('/input.txt') =>
sys_file_exists('docs') =>
sys_file_exists('/docs') =>
 
<syntaxhighlight lang="ruby">File.file?("input.txt")
Note that the above literally checks for existence. Namely sys_file_exists
File.file?("/input.txt")
returns true if file exists but can not be read.
File.directory?("docs")
File.directory?("/docs")</syntaxhighlight>
 
The next program runs all four checks and prints the results.
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:
 
<syntaxhighlight lang="ruby">["input.txt", "/input.txt"].each { |f|
;;; Define an auxilary function, returns boolean
printf "%s is a regular file? %s\n", f, File.file?(f) }
define file_readable(fname);
["docs", "/docs"].each { |d|
lvars f = sysopen(fname, 0, true, `A`);
printf "%s is a directory? %s\n", d, File.directory?(d) }</syntaxhighlight>
if f then
sysclose(f);
return (true);
else
return (false);
endif;
enddefine;
 
=={{header|Run BASIC}}==
==[[Python]]==
<syntaxhighlight lang="runbasic">files #f,"input.txt"
[[Category:Python]]
if #f hasanswer() = 1 then print "File does not exist"
'''Interpreter:''' [[Python]] 2.5
files #f,"docs"
if #f hasanswer() = 1 then print "File does not exist"
if #f isDir() = 0 then print "This is a directory"
</syntaxhighlight>
=={{header|Rust}}==
<syntaxhighlight lang="rust">use std::fs;
 
fn main() {
The ''os.path.exists'' method will return True if a path exists False if it does not.
for file in ["input.txt", "docs", "/input.txt", "/docs"].iter() {
match fs::metadata(file) {
Ok(attr) => {
if attr.is_dir() {
println!("{} is a directory", file);
}else {
println!("{} is a file", file);
}
},
Err(_) => {
println!("{} does not exist", file);
}
};
}
}
</syntaxhighlight>
=={{header|Scala}}==
{{libheader|Scala}}<syntaxhighlight lang="scala">import java.nio.file.{ Files, FileSystems }
 
object FileExistsTest extends App {
import os
os.path.exists("input.txt")
os.path.exists("/input.txt")
os.path.exists("docs")
os.path.exists("/docs")
 
val defaultFS = FileSystems.getDefault()
==[[Raven]]==
val separator = defaultFS.getSeparator()
[[Category:Raven]]
 
def test(filename: String) {
'input.txt' exists if 'input.txt exists' print
val path = defaultFS.getPath(filename)
'/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
 
println(s"The following ${if (Files.isDirectory(path)) "directory" else "file"} called $filename" +
==[[Smalltalk]]==
(if (Files.exists(path)) " exists." else " not exists."))
[[Category:Smalltalk]]
}
 
// main
"Smalltalk has no notion of 'current directory' because Smalltalk isn't tied to the shell that created it."
List("output.txt", separator + "output.txt", "docs", separator + "docs" + separator).foreach(test)
}</syntaxhighlight>
=={{header|Scheme}}==
{{works with|Scheme|R6RS}}[http://www.r6rs.org/final/html/r6rs-lib/r6rs-lib-Z-H-10.html]
<syntaxhighlight lang="scheme">(file-exists? filename)</syntaxhighlight>
=={{header|Seed7}}==
 
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
FileDirectory new fileExists: 'c:\serial'.
 
const proc: main is func
(FileDirectory on: 'c:\') directoryExists: 'docs'.
begin
writeln(fileType("input.txt") = FILE_REGULAR);
writeln(fileType("/input.txt") = FILE_REGULAR);
writeln(fileType("docs") = FILE_DIR);
writeln(fileType("/docs") = FILE_DIR);
end func;</syntaxhighlight>
=={{header|SenseTalk}}==
<syntaxhighlight lang="sensetalk">
put file "input.txt" exists
put folder "docs" exists
put file "/input.txt" exists
put there is a folder "/docs"
</syntaxhighlight>
=={{header|Sidef}}==
<syntaxhighlight lang="ruby"># Here
say (Dir.cwd + %f'input.txt' -> is_file);
say (Dir.cwd + %d'docs' -> is_dir);
 
# Root
==[[Tcl]]==
say (Dir.root + %f'input.txt' -> is_file);
[[Category:Tcl]]
say (Dir.root + %d'docs' -> is_dir);</syntaxhighlight>
NOTE: To check only for existence, use the method ''exists''
=={{header|Slate}}==
<syntaxhighlight lang="slate">(File newNamed: 'input.txt') exists
(File newNamed: '/input.txt') exists
(Directory root / 'input.txt') exists
(Directory newNamed: 'docs') exists
(Directory newNamed: '/docs') exists</syntaxhighlight>
=={{header|Smalltalk}}==
 
[[Squeak]] has no notion of 'current directory' because it isn't tied to the shell that created it.
Taking the meaning of the task from the DOS example:
 
<syntaxhighlight lang="smalltalk">FileDirectory new fileExists: 'c:\serial'.
if { [file exists input.txt] } { puts "input.txt exists" }
(FileDirectory on: 'c:\') directoryExists: 'docs'.</syntaxhighlight>
if { [file exists [file nativename /input.txt]] } { puts "/input.txt exists" }
 
In [[GNU Smalltalk]] instead you can do:
if { [file isdirectory docs] } { puts "docs exists and is a directory" }
if { [file isdirectory [file nativename /docs]] } { puts "/docs exists and is a directory" }
 
<syntaxhighlight lang="smalltalk">(Directory name: 'docs') exists ifTrue: [ ... ]
==[[Toka]]==
(Directory name: 'c:\docs') exists ifTrue: [ ... ]
[[Category:Toka]]
(File name: 'serial') isFile ifTrue: [ ... ]
(File name: 'c:\serial') isFile ifTrue: [ ... ]</syntaxhighlight>
 
Using ''exists'' in the third and fourth case will return true for directories too.
[ "R" file.open dup 0 <> [ dup file.close ] ifTrue 0 <> ] is exists?
=={{header|Standard ML}}==
" input.txt" exists? .
<syntaxhighlight lang="sml">OS.FileSys.access /("input.txt", exists? .[]);
OS.FileSys.access (" docs", exists? .[]);
OS.FileSys.access ("/input.txt", []);
" /docs" exists? .
OS.FileSys.access ("/docs", []);</syntaxhighlight>
=={{header|Stata}}==
Mata has functions to check the existence of files and directories:
<syntaxhighlight lang="stata">mata
fileexists("input.txt")
direxists("docs")
end</syntaxhighlight>
 
It's not as straightforward in Stata's macro language. For files, use [http://www.stata.com/help.cgi?confirm confirm]. Since it throws an error when the file does not exist, use [http://www.stata.com/help.cgi?capture capture] and check [http://www.stata.com/help.cgi?_variables _rc] afterwards.
==[[Visual Basic .NET]]==
[[Category:Visual Basic .NET]]
 
<syntaxhighlight lang="stata">capture confirm file input.txt
'''Platform:''' [[.NET]]
if !_rc {
* do something if the file exists
}</syntaxhighlight>
 
It's not possible to check the existence of a directory with confirm. One may use the [https://ideas.repec.org/c/boc/bocode/s435507.html confirmdir] package from SSC. The confirmdir command saves the current directory, then tries to chdir to the directory to test (with capture to prevent an error). Then the value of _rc is put in a [http://www.stata.com/help.cgi?return stored result]. Example of use:
'''Language Version:''' 9.0+
 
<syntaxhighlight lang="stata">confirmdir docs
'Current Directory
if !`r(confirmdir)' {
Console.WriteLine(If(IO.Directory.Exists("docs"), "directory exists", "directory doesn't exists"))
* do something if the directory exists
Console.WriteLine(If(IO.Directory.Exists("output.txt"), "file exists", "file doesn't exists"))
}</syntaxhighlight>
 
The command works with special names, but one has to be careful: the name "`Abdu'l-Bahá.txt" contains a backquote, which is used to denote macros in Stata. So this character must be escaped with a backslash:
 
<syntaxhighlight lang="stata">confirm file "\`Abdu'l-Bahá.txt"</syntaxhighlight>
=={{header|Tcl}}==
Taking the meaning of the task from the DOS example: <!-- multiline “if” because of formatting -->
<syntaxhighlight lang="tcl">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"
}</syntaxhighlight>
Note that these operations do not require the use of <tt>file nativename</tt> on either Windows or any version of Unix.
=={{header|Toka}}==
 
<syntaxhighlight lang="toka">[ "R" file.open dup 0 <> [ dup file.close ] ifTrue 0 <> ] is exists?
" input.txt" exists? .
" /input.txt" exists? .
" docs" exists? .
" /docs" exists? .</syntaxhighlight>
=={{header|True BASIC}}==
<syntaxhighlight lang="truebasic">SUB opener (a$)
WHEN EXCEPTION IN
OPEN #1: NAME f$
PRINT f$; " exists"
USE
PRINT f$; " not exists"
END WHEN
CLOSE #1
END SUB
 
LET f$ = "input.txt"
CALL opener (f$)
LET f$ = "\input.txt"
CALL opener (f$)
LET f$ = "docs\nul"
CALL opener (f$)
LET f$ = "\docs\nul"
CALL opener (f$)
 
LET f$ = "empty.tru"
CALL opener (f$)
LET f$ = "`Abdu'l-Bahá.txt"
CALL opener (f$)
END</syntaxhighlight>
=={{header|TUSCRIPT}}==
<syntaxhighlight lang="tuscript">
$$ MODE TUSCRIPT
file="input.txt",directory="docs"
IF (file=='file') THEN
PRINT file, " exists"
ELSE
PRINT/ERROR file," not exists"
ENDIF
IF (directory=='project') THEN
PRINT directory," exists"
ELSE
PRINT/ERROR "directory ",directory," not exists"
ENDIF
</syntaxhighlight>
{{out}}
<pre>
input.txt exists
@@@@@@@@ directory docs not exists @@@@@@@@
</pre>
=={{header|UNIX Shell}}==
<syntaxhighlight lang="bash">test -f input.txt
test -f /input.txt
test -d docs
test -d /docs</syntaxhighlight>
 
The next program runs all four checks and prints the results.
 
<syntaxhighlight lang="bash">for f in input.txt /input.txt; do
test -f "$f" && r=true || r=false
echo "$f is a regular file? $r"
done
for d in docs /docs; do
test -d "$d" && r=true || r=false
echo "$d is a directory? $r"
done</syntaxhighlight>
=={{header|Ursa}}==
The easiest way to do this in Ursa is to attempt to open the file in question. If it doesn't exist, an ioerror will be thrown.
<syntaxhighlight lang="ursa">def exists (string filename)
decl file f
try
f.open filename
return true
catch ioerror
return false
end try
end exists</syntaxhighlight>
=={{header|Vala}}==
This needs to be compiled with the gio-2.0 package: valac --pkg gio-2.0 check_that_file_exists.vala
<syntaxhighlight lang="vala">int main (string[] args) {
string[] files = {"input.txt", "docs", Path.DIR_SEPARATOR_S + "input.txt", Path.DIR_SEPARATOR_S + "docs"};
foreach (string f in files) {
var file = File.new_for_path (f);
print ("%s exists: %s\n", f, file.query_exists ().to_string ());
}
return 0;
}</syntaxhighlight>
A more complete version which informs whether the existing file is a regular file or a directory
<syntaxhighlight lang="vala">int main (string[] args) {
string[] files = {"input.txt", "docs", Path.DIR_SEPARATOR_S + "input.txt", Path.DIR_SEPARATOR_S + "docs"};
foreach (var f in files) {
var file = File.new_for_path (f);
var exists = file.query_exists ();
var name = "";
if (!exists) {
print ("%s does not exist\n", f);
} else {
var type = file.query_file_type (FileQueryInfoFlags.NOFOLLOW_SYMLINKS);
if (type == 1) {
name = "file";
} else if (type == 2) {
name = "directory";
}
print ("%s %s exists\n", name, f);
}
}
return 0;
}</syntaxhighlight>
=={{header|VBA}}==
 
<syntaxhighlight lang="vb">
Option Explicit
 
Sub Main_File_Exists()
Dim myFile As String, myDirectory As String
myFile = "Abdu'l-Bahá.txt"
myDirectory = "C:\"
Debug.Print File_Exists(myFile, myDirectory)
End Sub
 
Function File_Exists(F As String, D As String) As Boolean
If F = "" Then
File_Exists = False
Else
D = IIf(Right(D, 1) = "\", D, D & "\")
File_Exists = (Dir(D & F) <> "")
End If
End Function
</syntaxhighlight>
=={{header|VBScript}}==
 
<syntaxhighlight lang="vbscript">Set FSO = CreateObject("Scripting.FileSystemObject")
 
Function FileExists(strFile)
If FSO.FileExists(strFile) Then
FileExists = True
Else
FileExists = False
End If
End Function
Function FolderExists(strFolder)
'Root
If FSO.FolderExists(strFolder) Then
Console.WriteLine(If(IO.Directory.Exists("\docs"), "directory exists", "directory doesn't exists"))
FolderExists = True
Console.WriteLine(If(IO.Directory.Exists("\output.txt"), "file exists", "file doesn't exists"))
Else
Folderexists = False
End If
End Function
'''''Usage (apostrophes indicate comments-this section will not be run)'''''
'Root, platform independent
'If FileExists("C:\test.txt") Then
Console.WriteLine(If(IO.Directory.Exists(IO.Path.DirectorySeparatorChar & "docs"), _
' MsgBox "It Exists!"
"directory exists", "directory doesn't exists"))
'Else
Console.WriteLine(If(IO.Directory.Exists(IO.Path.DirectorySeparatorChar & "output.txt"), _
' Msgbox "awww"
"file exists", "file doesn't exists"))
'End If
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 
'Shorter version
 
If CreateObject("Scripting.FileSystemObject").FileExists("d:\test.txt") Then
Wscript.Echo "File Exists"
Else
Wscript.Echo "File Does Not Exist")
End If
 
 
</syntaxhighlight>
=={{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.
<syntaxhighlight lang="vedit">// 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") }
 
// 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") }</syntaxhighlight>
=={{header|Visual Basic}}==
{{works with|Visual Basic|VB6 Standard}}
The proposed solutions for VBA and VBScript work in VB6 as well, however here's a Windows API based approach:
<syntaxhighlight lang="vb">
'declarations:
Public Declare Function GetFileAttributes Lib "kernel32" _
Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
Public Const INVALID_FILE_ATTRIBUTES As Long = -1
Public Const ERROR_SHARING_VIOLATION As Long = 32&
 
'implementation:
Public Function FileExists(ByVal Filename As String) As Boolean
Dim l As Long
l = GetFileAttributes(Filename)
If l <> INVALID_FILE_ATTRIBUTES Then
FileExists = ((l And vbDirectory) = 0)
ElseIf Err.LastDllError = ERROR_SHARING_VIOLATION Then
FileExists = True
End If
End Function
</syntaxhighlight>
=={{header|Visual Basic .NET}}==
'''Platform:''' [[.NET]]
 
{{works with|Visual Basic .NET|9.0+}}
<syntaxhighlight 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"))</syntaxhighlight>
=={{header|V (Vlang)}}==
<syntaxhighlight lang="go">// Check file exists in V
// Tectonics: v run check-that-file-exists.v
module main
import os
 
// starts here
pub fn main() {
// file and directory checks
_ := os.execute("touch input.txt")
println("os.is_file('input.txt'): ${os.is_file('input.txt')}")
 
// make doc directory in current dir if it doesn't exist
_ := os.execute("mkdir -p doc")
println("os.is_dir('doc'): ${os.is_dir('doc')}")
 
// check in the root dir
println("os.is_file('/input.txt'): ${os.is_file('/input.txt')}")
println("os.is_dir('/doc'): ${os.is_dir('/doc')}")
 
// check for file, with empty file
_ := os.execute("truncate -s 0 empty.txt")
println("os.is_file('empty.txt'): ${os.is_file('empty.txt')}")
 
// check for file, with exotic name
wfn := "`Abdu'l-Bahá.txt"
efn := wfn.replace_each(["'", r"\'", "`", r"\`"])
_ := os.execute('touch $efn')
println('os.is_file("$wfn"): ${os.is_file(wfn)}')
}</syntaxhighlight>
 
{{out}}
<pre>prompt$ v run check-that-file-exists.v
os.is_file('input.txt'): true
os.is_dir('doc'): true
os.is_file('/input.txt'): false
os.is_dir('/doc'): false
os.is_file('empty.txt'): true
os.is_file("`Abdu'l-Bahá.txt"): true</pre>
 
=={{header|Wren}}==
Empty files and directories have been created beforehand.
 
To check a file or directory exists in the root, just change "input.txt" to "/input.txt" and "docs" to "/docs" in the following script.
 
Since in Linux an ''empty'' directory has a size of 4K bytes, we check the number of files it contains to confirm that it's empty.
<syntaxhighlight lang="wren">import "io" for Directory, File
 
for (name in ["input.txt", "`Abdu'l-Bahá.txt"]) {
if (File.exists(name)) {
System.print("%(name) file exists and has a size of %(File.size(name)) bytes.")
} else {
System.print("%(name) file does not exist.")
}
}
 
var dir = "docs"
// if it exists get number of files it contains
if (Directory.exists(dir)) {
var files = Directory.list(dir).count
System.print("%(dir) directory exists and contains %(files) files.")
} else {
System.print("%(dir) directory does not exist.")
}</syntaxhighlight>
 
{{out}}
<pre>
input.txt file exists and has a size of 0 bytes.
`Abdu'l-Bahá.txt file exists and has a size of 0 bytes.
docs directory exists and contains 0 files.
</pre>
 
=={{header|XPL0}}==
Attempting to open a non-existent file or directory will cause an error.
A zero-length file is detected as existing.
<syntaxhighlight lang="xpl0">
int FD; \file descriptor
[Trap(false); \prevent errors from aborting program
FD:= FOpen("input.txt", 0);
if GetErr then Text(0, "input.txt doesn't exist^m^j");
FD:= FOpen("dir", 0);
if GetErr then Text(0, "dir doesn't exist^m^j");
FD:= FOpen("/input.txt", 0);
if GetErr then Text(0, "/input.txt doesn't exist^m^j");
FD:= FOpen("/dir", 0);
if GetErr then Text(0, "/dir doesn't exist^m^j");
]</syntaxhighlight>
=={{header|Yabasic}}==
<syntaxhighlight lang="yabasic">open "foo.bar" for writing as #1
print #1 "Hallo !"
close #1
if (not open(1,"foo.bar")) print "Could not open 'foo.bar' for reading"
close #1
if (not open(1,"buzz.bar")) print "Could not open 'buzz.bar' for reading"
</syntaxhighlight>
=={{header|zkl}}==
<syntaxhighlight lang="zkl">File.exists("input.txt") //--> True (in this case a sym link)
File.exists("/input.txt") //-->False
File.isDir("/") //-->True
File.isDir("docs") //-->False
</syntaxhighlight>
{{omit from|Befunge|No filesystem support}}
{{omit from|EasyLang}}
{{omit from|HTML}}
{{omit from|Scratch|No filesystem support}}
{{omit from|TI-83 BASIC|Does not have a filesystem, just namespaced variables.}}
{{omit from|TI-89 BASIC|Does not have a filesystem, just namespaced variables.}}
885

edits