Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can one check if a file or directory exists using Deno?

The Deno TypeScript runtime has built-in functions, but none of them address checking the existence of a file or directory. How can one check if a file or directory exists?

like image 559
Amani Avatar asked Jun 18 '19 23:06

Amani


People also ask

How do you check whether a file exists or not?

To check if a file exists, you pass the file path to the exists() function from the os. path standard library. If the file exists, the exists() function returns True . Otherwise, it returns False .

How do you check if a file exists in a specific folder?

To check for specific files use File. Exists(path) , which will return a boolean indicating wheter the file at path exists.

How do you check if a file exists in a directory Ruby?

The File. file?() function checks whether or not a file exists. This function returns TRUE if the file exists, otherwise it returns FALSE. Apart from the above methods Ruby also provides File.

How do you check if a file already exists in a directory in Java?

The method java. io. File. exists() is used to check whether a file or a directory exists or not.


1 Answers

There is the standard library implementation, here: https://deno.land/std/fs/mod.ts

import {existsSync} from "https://deno.land/std/fs/mod.ts";

const pathFound = existsSync(filePath)
console.log(pathFound)

This code will print true if the path exists and false if not.

And this is the async implementation:

import {exists} from "https://deno.land/std/fs/mod.ts"

exists(filePath).then((result : boolean) => console.log(result))

Make sure you run deno with the unstable flag and grant access to that file:

deno run --unstable  --allow-read={filePath} index.ts
like image 57
yonBav Avatar answered Oct 02 '22 19:10

yonBav