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?
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 .
To check for specific files use File. Exists(path) , which will return a boolean indicating wheter the file at path exists.
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.
The method java. io. File. exists() is used to check whether a file or a directory exists or not.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With