Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Js

Idiom #144 Check if file exists

Set boolean b to true if file at path fp exists on filesystem; false otherwise.

Beware that you should not do this and then in the next instruction assume the result is still valid, this is a race condition on any multitasking OS.

import { access } from 'fs/promises';
let b = true;
try {
	await access(fp);
} catch {
	b = false;
}

Sets b to false if the access function fails due to fp not being visible to the process.
try {
	Deno.statSync(fp)
} catch(_e) {console.error("File does not exist.")}
const fs = require('fs');
const b = fs.existsSync(fp);
with Ada.Directories;
B : constant Boolean := Ada.Directories.Exists (FP);

New implementation...
< >
programming-idioms.org