Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js NodeJS-递归复制和重命名现有目录中的所有内容_Node.js_Filesystems - Fatal编程技术网

Node.js NodeJS-递归复制和重命名现有目录中的所有内容

Node.js NodeJS-递归复制和重命名现有目录中的所有内容,node.js,filesystems,Node.js,Filesystems,我有一个目录,里面有文件夹和文件。我想将整个目录及其所有内容复制到另一个位置,同时将所有文件重命名为更有意义的文件。我想使用nodejs来完成这一系列操作。除了一个接一个地移动和重命名它之外,还有什么简单的方法可以做到这一点 谢谢 --谢谢你的评论!下面是我想到的一个示例目录: -MyFridge - MyFood.txt - MyApple.txt - MyOrange.txt - ... - MyDrinks - MySoda - MyDietCoke.txt

我有一个目录,里面有文件夹和文件。我想将整个目录及其所有内容复制到另一个位置,同时将所有文件重命名为更有意义的文件。我想使用nodejs来完成这一系列操作。除了一个接一个地移动和重命名它之外,还有什么简单的方法可以做到这一点

谢谢

--谢谢你的评论!下面是我想到的一个示例目录:

-MyFridge
 - MyFood.txt
  - MyApple.txt
  - MyOrange.txt
  - ...
 - MyDrinks
  - MySoda
    - MyDietCoke.txt
  - MyMilk.txt
  - ...
 - MyDesserts
 - MyIce
 ...
例如,我想用“Tom”替换“My”,并且我还想在所有文本文件中将“My”重命名为Tom。我可以使用node fs extra将目录复制到其他位置,但重命名文件名时遇到了困难

定义自己的工具

const fs=require('fs');
const path=require('path');
函数renameFileRecursive(dir、from、to){
fs.readdirSync(dir.forEach)(它=>{
const itsPath=path.resolve(dir,it);
常量itstat=fs.statSync(itsPath);
if(itsPath.search(from)>-1){
fs.renameSync(itsPath,itsPath.replace(from,to))
}
如果(itstat.isDirectory()){
RenameFileRecursive(itsPath.replace(from,to),from,to)
} 
})
}
用法

const dir=path.resolve(uu dirname,'src/app');
重命名文件递归(dir,/^My/,'Tom');
重命名FileRecursive(dir,/\.txt$/,'.class');

您应该发布一个文件层次结构示例,以便我们可以看到您心目中的新文件名映射。谢谢!我已经编辑了这篇文章来给出一个例子。从walk和fs libs开始,然后从那里开始。在上面的例子中,resursive调用中没有传递“from”和“to”参数,如下所示:renameFilesRecursive(itsPath,from,to)@NielsBoogaard如何将重命名后的文件放入新文件夹?