Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading file size and last modified date for file using Deno

How can I use Deno to read the size of a file and its last modified date/time?

In the browser I can use instanceOfFile.size and instanceOfFile.lastModified but these don't work if I provide the path to the file on the server.

const file = '/home/test/data.json'
const isFile = await fileExists(file)
if (isFile) {
  console.log(file.size)          // returns `undefined`
  console.log(file.lastModified). // returns `undefined`
}
like image 652
Mark Tyers Avatar asked Oct 24 '25 09:10

Mark Tyers


1 Answers

You can use Deno.stat for that.

const file = await Deno.stat("/home/test/data.json");
if (file.isFile) {
  console.log("Last modified:", file.mtime?.toLocaleString());
  console.log("File size in bytes:", file.size);
}
like image 176
Satya Rohith Avatar answered Oct 26 '25 19:10

Satya Rohith