在监视新的.ogg文件的文件夹时,下面的脚本非常有效。它成功创建了一个具有新文件名的文件夹,然后根据其创建的日期/
根据文件重命名该文件然而,当我在脚本尝试创建已存在的文件夹的同时添加多个文件时出现问题,这表明它正在以某种方式混合两个文件名。有没有人建议我可能做错了什么?我假设其简单的代码结构,虽然我无法找出原因。
var baseDir = './',
path = require('path'),
fs = require('fs');
// watch the directory for new files
fs.watch(baseDir, function(event, file) {
var ext = path.extname(file)
basename = path.basename(file).substring(0, path.basename(file).length - ext.length);
// check it wasnt a delete action
fs.exists(baseDir + file, function(exists) {
// check we have the right file type
if(exists && ext === '.ogg'){
// get the created date
fs.stat(baseDir + file, function (err, stats){
if (err)
throw err;
var year = stats.ctime.getFullYear();
var month = stats.ctime.getMonth()+1;
var day = stats.ctime.getDate();
var hour = stats.ctime.getHours();
var sec = stats.ctime.getSeconds();
if(month < 10){
month = '0' + month;
}
if(day < 10){
day = '0' + day;
}
if(hour < 10){
hour = '0' + hour;
}
if(sec < 10){
sec = '0' + sec;
}
var name = year + '' + month + '' + day + '' + hour + '' + sec;
// does the basename directory exist?
fs.exists(baseDir + '/' + basename, function(exists) {
// if the directory doesnt exist
if(!exists){
// make the directory
fs.mkdir(baseDir + '/' + basename, 0777, function (err, stats){
if (err)
throw err;
moveFile(file, basename, name, ext);
});
} else {
moveFile(file, basename, name, ext);
}
});
});
}
});
});
function moveFile(file, basename, name, ext){
// move the file to the new directory
fs.rename(baseDir + file, baseDir + '/' + basename + '/' + name + ext, function (err) {
if (err)
throw err;
// console.log('Rename complete');
});
}
答案 0 :(得分:1)
好的,所以我多花了几分钟,决定去找你。我稍微重构了你的代码,但基本结构应该很容易识别。
var baseDir = './test', path = require('path'), fs = require('fs'); // watch the directory for new files fs.watch(baseDir, function(event, file) { var ext = path.extname(file), basename = path.basename(file).substring(0, path.basename(file).length - ext.length); // check it wasnt a delete action // check we have the right file type var filePath = path.join(baseDir, file); if(fs.existsSync(filePath) && ext === '.ogg'){ // get the created date var stats = fs.statSync(filePath); var name = getName(stats); // if the directory doesnt exist var baseDirPath = path.join(baseDir, basename); if(!fs.existsSync(baseDirPath)){ // make the directory fs.mkdirSync(baseDirPath, 0777); } moveFile(file, basename, name, ext); } }); function getName (stats) { var year = stats.ctime.getFullYear(); var month = stats.ctime.getMonth()+1; var day = stats.ctime.getDate(); var hour = stats.ctime.getHours(); // need minutes! var minutes = stats.ctime.getMinutes(); var sec = stats.ctime.getSeconds(); if(month %lt 10){ month = '0' + month; } if(day < 10){ day = '0' + day; } if(hour < 10){ hour = '0' + hour; } if(minutes < 10){ minutes = '0' + minutes; } if(sec < 10){ sec = '0' + sec; } // missing the minute, previously return year + '' + month + '' + day + '' + hour + '' + minutes + '' + sec; } function moveFile(file, basename, name, ext){ // move the file to the new directory var src = path.join(baseDir, file), dest = path.join(baseDir, basename, name+ext); console.log("Moving ", src, "-", dest); fs.renameSync(src, dest); }
一些提示/更正:
在处理像这样的简单脚本时,坚持使用以Sync同步的同步fs
方法。虽然node.js以其异步能力而闻名,但它有点过早优化IMO。例如,如果您需要将其嵌入到高性能的网络服务器中,那么就要优化,而不是之前。
创建新文件名时,您错过了minutes
变量。这很可能导致名称冲突,所以我纠正了它。
尝试更多地利用path
库(如path.join
),因为手动连接路径的字符串通常会导致代码变得脆弱。
还有一些边缘情况会导致崩溃。创建一个没有扩展名的文件,该扩展名与您将基于另一个文件创建的目录具有相同的名称。 (文件不能成为目录,您无法在另一个文件中移动文件。)。如果您计划进入生产环境,则需要至少通过几次单元测试来强化代码。
干杯,
丹