Search code examples
javascriptnode.jsprotractorglobfs

How to use wild card inside of fs.existsSync or statSync or readFileSync


My spec below clicks on a download button and then verifies the download. The only problem in the program is that you need to know the name of the file in advance.

Rather I want it to look for a wild card in the directory like *.mp4 I have read up on glob but not sure how to implement it.

Can someone show me how to integrate it or another way, into code below? -

element(by.css('.download-recording .pgi-button-wrapper')).click();

browser.driver.wait(function() {

    return fs.existsSync('/home/user,/Downloads/Recordings/ProtractorMeeting-2019-01-24T21_56_24.000Z.mp4')}, 8000).then(function() {

        console.log("File Downloaded");
        console.log(fs.statSync('/home/user/Downloads/Recordings/ProtractorMeeting-2019-01-24T21_56_24.000Z.mp4').size);
        console.log(fs.readFileSync('/home/user/Downloads/Recordings/ProtractorMeeting-2019-01-24T21_56_24.000Z.mp4').length);
});

Solution

  • You can read whole directory with

    fs.readdirSync

    and then just filter received array of results, and generate abs path:

    let fs = require('fs')
    let folderfiles = fs.readdirSync('./')
    console.log(folderfiles)
    let mp4filePaths = folderfiles
        .filter(file=> file.endsWith('.mp4'))
        .map(file=> path.resolve(__dirname, file))
    console.log(mp4filePaths)