Nodejs check file exists, if not, wait till it exist

前端 未结 7 694
一向
一向 2021-02-05 08:54

I\'m generating files automatically, and I have another script which will check if a given file is already generated, so how could I implement such a function:

f         


        
7条回答
  •  囚心锁ツ
    2021-02-05 09:14

    function verifyFileDownload(extension) {
        browser.sleep(150000); //waiting for file to download
        const fs = require('fs');
        let os = require('os');
        var flag = true;
        console.log(os.userInfo());
        fs.readdir('/Users/' + require("os").userInfo().username + '/Downloads/', (error, file) => {
            if (error) {
                throw error;
            }
            console.log('File name' + file);
            for (var i = 0; i < file.length; i++) {
                const fileParts = file[i].split('.');
                const ext = fileParts[fileParts.length - 1];
                if (ext === extension) {
                    flag = false;
                }
            }
            if (!flag) {
                return;
            }
            throw error;
        });
    };
    

提交回复
热议问题