To list all the files in the current directory in TypeScript, we can make use of the Node.js fs
module. Specifically, we can use the readdirSync
function to read the contents of the directory synchronously, and the path
module to construct the full paths to each file.
Here's an example TypeScript code snippet that lists all the files in the current directory:
index.ts340 chars11 lines
This code snippet first gets the current directory using the process.cwd()
function, and then reads the files in the directory using fs.readdirSync()
. It then iterates through the array of file names, constructs the full path to each file using path.join()
, and logs the path to the console.
Note that fs.readdirSync()
only returns the names of the files, not their full paths, so we need to use path.join()
to construct the full path.
gistlibby LogSnag