0

Just took up TypeScript a couple of weeks ago without much knowledge in JavaScript.

I am trying to go through all the files in the specified directory and put each file name (string) and change time (number) into an array of array and sort by change time.

It looks like this: [['natalie.jpg', 143], ['mike.jpg', 20], ['john.jpg', 176], ['Jackie.jpg', 6]]

Problem 1: I do not know how to specify the inner array content, string and number. Type? Interface? Class? Tuple?

Problem 2: I do not know how to sort by change time in ascending order, so that the array changes to: [['Jackie.jpg', 6], ['mike.jpg', 20], ['natalie.jpg', 143], ['john.jpg', 176]]

import fs from 'fs'

const dirPath = '/home/me/Desktop/'

type imageFileDesc = [string, number] // tuple

const imageFileArray = [imageFileDesc] // ERROR HERE!

function readImageFiles (dirPath: string) {
  try {
    const dirObjectNames = fs.readdirSync(dirPath)

    for (const dirObjectName of dirObjectNames) {
      const dirObject = fs.lstatSync(dirPath + '/' + dirObjectName)
      if (dirObject.isFile()) {
        imageFileArray.push([dirObjectName, dirObject.ctimeMs]) // ERROR HERE!
      }
    }

    imageFileArray.sort(function (a: number, b: number) {
      return b[1] - a[1] // ERROR HERE! Can we do something like b.ctime - a.ctime?
    })
  } catch (error) {
    console.error('Error in reading ' + dirPath)
  }
}

readImageFiles(dirPath)
console.log(imageFileArray)
3
  • Is the type you're looking for [ string, number ][]? Commented Jul 6, 2021 at 3:14
  • @bitomic: When I change to const imageFileArray = [ string, number ][], it says, " 'string/number' only refers to a type, but is being used as a value here." Commented Jul 6, 2021 at 3:20
  • You are incorrectly using types, I suggest you to check the Typescript Documentation at typescriptlang.org/docs. They explain it way better than I could in a comment. 😅 Commented Jul 6, 2021 at 3:27

1 Answer 1

1
import * as fs from 'fs'

// Read files in folder
const files = fs.readdirSync( './files' )
// This will store the file name and their modification time
const imageFileArray: [ string, Date ][] = []

for ( const file of files ) {
    // You can get a file's last modified time through fs.stat
    const stats = fs.statSync( `./files/${file}` )
    const modifiedTime = stats.mtime
    // Push a tuple containing the filename and the last modified time
    imageFileArray.push( [ file, modifiedTime ] )
}

// Sort from older to newer
const sorted = imageFileArray.sort( (a, b) => a[1].getTime() - b[1].getTime() )

console.log( sorted )

The modified time returns a Date object. If you want to sort from newer to older, just invert the operation in the sort function.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.