Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

182
Views
Find first file with given filename

Hello I am trying to find first file with given filename ( piece of filename ).

It works fine but it take a while to take result

There is code

const fs = require("fs");

const dirCheckIn =
    "\\\\192.168.2.4\\Photos";


exports.checkUploadedFiles = (req, res) => {
    let fileName = req.params.filename;

    const getAllFiles = function (dirPath, arrayOfFiles) {
        files = fs.readdirSync(dirPath);

        arrayOfFiles = arrayOfFiles || [];

        files.forEach(function (file) {
            if (fs.statSync(dirPath + "/" + file).isDirectory()) {
                arrayOfFiles = getAllFiles(dirPath + "/" + file, arrayOfFiles);
            } else {
                arrayOfFiles.push(file);
            }
        });

        return arrayOfFiles;
    };

    const uploadedFiles = getAllFiles(inventDirCheckIn);
    console.log(uploadedFiles)
    let result = uploadedFiles.find(
        (result) => result.startsWith(fileName));

    if (!result) {
        res.send('nothing found')
    } else if (result) {
        res.send(result)
    }

}

It works fine but for example if I have over 7000 photos it takes about 5 sec to get result. Maybe there is smarter solution?

How can I make it in better way? I want to check if file is uploaded into dir Photos. I got simple api route /api/getUploadedFiles/:filename

Also I want use startsWith because sometimes I do not know full name of file

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

/**
 *
 * @param filePath path to file which is to be checked if it exists.
 */
private checkFileExistsSync(filePath: string) {
  let flag = true;
  try {
    fs.accessSync(filePath, fs.constants.F_OK);
  } catch (e) {
    flag = false;
  }
  return flag;
}

// Example usage
// path to the file
const dirCheckIn =
    "\\\\192.168.2.4\\Photos";
    
if (checkFileExistsSync(dirCheckIn)) {
  // if the file exists do something...
}

if (!checkFileExistsSync(dirCheckIn)) {
  // if the file doesn't exists do something...
}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!