I have
fileUploadPath="d:/downloads";
and
entry.name='155ce0e4-d763-4153-909a-407dc4e328d0/63690689-e183-46ae-abbe-bb4ba5507f1a_MULTI_0_3/output/res2/res2.fcs';
when try to create res2.fcs file with following above path it shows gives me error, why it is not creating the folder structure?
code:
data.entries.forEach(entry => { console.log(entry.name); if (fs.existsSync(fileUploadPath)) { var sourceFilePath = fileUploadPath + '/' + entry.name; if (!fs.existsSync(sourceFilePath)) { fs.mkdir(sourceFilePath, {recursive: true }, (err) => {if (err) { console.log("Failed :" + err); } else { const fstream = fs.createWriteStream(require('path').format({dir: fileUploadPath, base: entry.name })); fstream.write('fileContent'); fstream.end(); fstream.on("finish", f => { console.log(f) }); fstream.on("error", e => { console.log(e) }); } }); } else { console.log('d'); } } else { console.log('ssss') } })
error:
[Error: ENOENT: no such file or directory, open 'D:downloads626a69d18d2468c5082fc6e1MULTI_1_2022-04-28_11-00-38outputres1res1.fcs'] { errno: -4058, code: 'ENOENT', syscall: 'open', path: 'D:\downloads\626a69d18d2468c5082fc6e1\MULTI__2022-04-28_11-00-38\output\res1\res1.fcs'
Advertisement
Answer
you’re passing file path to .mkdir
instead of folder, so you should first create a folder, and then write the file
(as you’re creating a folder that has the same name as the file, you should’ve got EISDIR
error, instead of this one, which also says the file write didn’t work)
use path.dirname to extract folder and pass it to .mkdir
:
// require path module at the top of the script const path = require('path'); fs.mkdir(path.dirname(sourceFilePath), { recursive: true }, (err) => { //...