Javascript:关于打开一个文件夹并每隔x秒重命名文件的错误



我在JS和Nodejs非常新的,我正在努力编写一个脚本:每隔1秒它将读取文件夹中的文件,并在对每个文件的行执行特定任务后将重命名文件。下面的代码是我的实现:


const path = require('path');
const fs = require('fs');
const csv = require('csv-parser');
const { parse } = require("csv-parse");

const directoryPath = path.join(__dirname, 'Documents');
console.log(directoryPath);
function FTP(){
console.log("------------- SCAN THE DIRECTORY --------------------------")
fs.readdir(directoryPath, function (err, files) {

if (err) {
return console.log('Unable to scan directory: ' + err);
} 
files.forEach(function (file) {    
console.log(`${file}`)

fs.createReadStream(`${directoryPath}/${file}`)
.pipe(parse({ delimiter: `t`, from_line: 2 }))
.on("data", function (row) {
let arrayToString = JSON.stringify(Object.assign({}, row));  // convert array to string
let stringToJsonObject_row = JSON.parse(arrayToString); 
// DO A JOB ....
})
.on("end", function () {
console.log("finished");
})
.on("error", function (error) {
console.log(error.message);
})

});
})
fs.readdir(directoryPath, function (err, files) {

if (err) {
return console.log('Unable to scan directory: ' + err);
} 

files.forEach(function (file) {    

fs.rename(`${directoryPath}/${file}`,`${directoryPath}/${file}0`,  () => {
console.log(`${file} Has been renamed`)});

});
});
}
setInterval(FTP, 1000);

这是我得到的错误:

/home/nikos/Desktop/TL2/FTP_server/Documents
------------- SCAN THE DIRECTORY --------------------------
sample_new
sample_new1
sample_new Has been renamed
sample_new1 Has been renamed
finished
finished
------------- SCAN THE DIRECTORY --------------------------
sample_new0
sample_new10
sample_new0 Has been renamed
sample_new10 Has been renamed
finished
finished
------------- SCAN THE DIRECTORY --------------------------
sample_new00
sample_new100
sample_new00 Has been renamed
sample_new100 Has been renamed
finished
finished
------------- SCAN THE DIRECTORY --------------------------
sample_new000
sample_new1000
sample_new000 Has been renamed
sample_new1000 Has been renamed
finished
finished
------------- SCAN THE DIRECTORY --------------------------
sample_new0000
sample_new10000
sample_new0000 Has been renamed
sample_new10000 Has been renamed
finished
finished
------------- SCAN THE DIRECTORY --------------------------
sample_new00000
sample_new100000
sample_new00000 Has been renamed
sample_new100000 Has been renamed
node:events:505
throw er; // Unhandled 'error' event
^
Error: ENOENT: no such file or directory, open '/home/nikos/Desktop/TL2/FTP_server/Documents/sample_new00000'
Emitted 'error' event on ReadStream instance at:
at emitErrorNT (node:internal/streams/destroy:157:8)
at emitErrorCloseNT (node:internal/streams/destroy:122:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: '/home/nikos/Desktop/TL2/FTP_server/Documents/sample_new00000'
}
[Done] exited with code=1 in 6.107 seconds

因为它似乎几个迭代,它的工作原理,但然后我采取上述错误,谁能解释我为什么?

谢谢!

正如我在阅读异步编程后可以理解的那样,我认为由于nodejs是非阻塞的,它不会等待IO,它会转到下面的代码行,当文件将要被处理时,它已经被重命名命令重命名。你认为这是正确的解释吗?

相关内容

  • 没有找到相关文章

最新更新