const path = require('path');
const fs = require('fs');
// console.log(process.argv);
//process是一个全局对象
//process.argv可以拿到我们执行node命令时传递的参数,argv是一个数组
//复制文件的开始路径
const fromPath = process.argv[2];
//复制文件的目标路径
const toPath = process.argv[3];
const copyFile = (fromPath,toPath)=>{
//读取数据来源的文件夹
fs.readdir(fromPath,(err,files)=>{
if(!err){
//创建文件夹
fs.mkdir(path.resolve(toPath,'./CopyFile'),err=>{
if(!err){
console.log('目标文件创建成功');
files.forEach((item,index) => {
const startFilePath = path.resolve(fromPath,item);
const endFilePath = path.resolve(toPath, './CopyFile/' + (index + '').padStart(2, 0) + '.mp4');
if(item.endsWith('.mp4')){
fs.copyFile(startFilePath, endFilePath,(err)=>{
if(!err){
console.log('复制成功');
}
})
}
})
}else{
console.log(err);
}
})
}
})
}
copyFile(fromPath, toPath);
运行时使用node xxx.js 源文件的路径 要复制去的文件路径
这段代码演示了如何使用Node.js的fs模块从指定路径批量复制MP4文件到目标文件夹,通过process.argv获取命令行参数作为输入路径。
181

被折叠的 条评论
为什么被折叠?



