我被我的剧本困住了。我的目标是:我有一个图像文件夹,想要将它们分割并移动到多个其他文件夹(它们总是在那里)。文件夹限制为 200 个文件,在我的循环中,在第一个文件夹成功填充 200 个文件后,在第二个文件夹的第二个文件中,我总是得到错误:
Error: ENOENT: no such file or directory, rename '/home/.../chunkDirsMoveFiles/input/resize/20150818_051550_web.jpg' -> './output/dir_2/20150818_051550_web.jpg'] {
errno: -2,
code: 'ENOENT',
syscall: 'rename',
path: '/home/.../chunkDirsMoveFiles/input/resize/20150818_051550_web.jpg',
dest: './output/dir_2/20150818_051550_web.jpg'
}
我的代码(我猜问题通常从 currentdir = 1 开始):
const { clear } = require("console");
const fs = require("fs")
const path = require("path")
// const { fileSystemHelper } = require('../allhelpers/fileSystemHelper.js')
const dirLimit = 200
clear()
// Fn to get all file paths of the input folder
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(path.join(__dirname, dirPath, "/", file))
}
})
return arrayOfFiles
}
const images = getAllFiles("input/resize")
const dirAmount = Math.round(images.length/dirLimit)
for (let i = 0; i < dirAmount; i++) {
fs.mkdir(`./output/dir_${i + 1}`, function(err) {
// if (err) {
// console.log(err)
// } else {
// console.log("New directory successfully created.")
// }
})
}
currentDir = 1
for (let i = 0; i < dirAmount; i++) {
for (let k = 0; k < dirLimit; k++) {
const theFilePath = images[k].split('/')
const newPath = theFilePath[(theFilePath.length)-1]
fs.rename(images[k], `./output/dir_${currentDir}/${newPath}`, function (err) {
if (err) throw err
console.log('Successfully renamed/moved!')
})
}
currentDir += 1
}