有没有一种更简单的方法来复制文件夹及其所有内容,而无需手动执行一系列fs.readir
, fs.readfile
,fs.writefile
递归?
我只是想知道我是否缺少一个理想情况下可以像这样工作的功能:
fs.copy("/path/to/source/folder", "/path/to/destination/folder");
有没有一种更简单的方法来复制文件夹及其所有内容,而无需手动执行一系列fs.readir
, fs.readfile
,fs.writefile
递归?
我只是想知道我是否缺少一个理想情况下可以像这样工作的功能:
fs.copy("/path/to/source/folder", "/path/to/destination/folder");
这是我在没有任何额外模块的情况下解决此问题的方法。只需使用内置fs
和path
模块。
注意:这确实使用了 fs 的读/写功能,因此它不会复制任何元数据(创建时间等)。从 Node.js 8.5 开始,有一个copyFileSync
可用的函数调用操作系统复制函数,因此也复制元数据。我还没有测试它们,但它应该可以替换它们。(见https://nodejs.org/api/fs.html#fs_fs_copyfilesync_src_dest_flags)
var fs = require('fs');
var path = require('path');
function copyFileSync( source, target ) {
var targetFile = target;
// If target is a directory, a new file with the same name will be created
if ( fs.existsSync( target ) ) {
if ( fs.lstatSync( target ).isDirectory() ) {
targetFile = path.join( target, path.basename( source ) );
}
}
fs.writeFileSync(targetFile, fs.readFileSync(source));
}
function copyFolderRecursiveSync( source, target ) {
var files = [];
// Check if folder needs to be created or integrated
var targetFolder = path.join( target, path.basename( source ) );
if ( !fs.existsSync( targetFolder ) ) {
fs.mkdirSync( targetFolder );
}
// Copy
if ( fs.lstatSync( source ).isDirectory() ) {
files = fs.readdirSync( source );
files.forEach( function ( file ) {
var curSource = path.join( source, file );
if ( fs.lstatSync( curSource ).isDirectory() ) {
copyFolderRecursiveSync( curSource, targetFolder );
} else {
copyFileSync( curSource, targetFolder );
}
} );
}
}
看起来ncp和wrench都不再维护了。可能最好的选择是使用fs-extra
扳手的开发者指示用户使用fs-extra
他已弃用他的库
copySync和moveSync都将复制和移动文件夹,即使它们有文件或子文件夹,您也可以使用它轻松移动或复制文件
const fse = require('fs-extra');
const srcDir = `path/to/file`;
const destDir = `path/to/destination/directory`;
// To copy a folder or file
fse.copySync(srcDir, destDir, function (err) {
if (err) { ^
console.error(err); |___{ overwrite: true } // add if you want to replace existing folder or file with same name
} else {
console.log("success!");
}
});
或者
// To Move a folder or file
fse.moveSync(srcDir, destDir, function (err) {
if (err) { ^
console.error(err); |___{ overwrite: true } // add if you want to replace existing folder or file with same name
} else {
console.log("success!");
}
});
这是一个递归地将目录及其内容复制到另一个目录的函数:
const fs = require("fs")
const path = require("path")
/**
* Look ma, it's cp -R.
* @param {string} src The path to the thing to copy.
* @param {string} dest The path to the new copy.
*/
var copyRecursiveSync = function(src, dest) {
var exists = fs.existsSync(src);
var stats = exists && fs.statSync(src);
var isDirectory = exists && stats.isDirectory();
if (isDirectory) {
fs.mkdirSync(dest);
fs.readdirSync(src).forEach(function(childItemName) {
copyRecursiveSync(path.join(src, childItemName),
path.join(dest, childItemName));
});
} else {
fs.copyFileSync(src, dest);
}
};
有一些模块支持复制文件夹及其内容。最受欢迎的是wrench.js:
// Deep-copy an existing directory
wrench.copyDirSyncRecursive('directory_to_copy', 'location_where_copy_should_end_up');
另一种方法是node-fs-extra:
fs.copy('/tmp/mydir', '/tmp/mynewdir', function (err) {
if (err) {
console.error(err);
} else {
console.log("success!");
}
}); // Copies directory, even if it has subdirectories or files
对于 Linux/Unix 操作系统,您可以使用 shell 语法
const shell = require('child_process').execSync;
const src = `/path/src`;
const dist = `/path/dist`;
shell(`mkdir -p ${dist}`);
shell(`cp -r ${src}/* ${dist}`);
就是这样!
这就是我个人的做法:
function copyFolderSync(from, to) {
fs.mkdirSync(to);
fs.readdirSync(from).forEach(element => {
if (fs.lstatSync(path.join(from, element)).isFile()) {
fs.copyFileSync(path.join(from, element), path.join(to, element));
} else {
copyFolderSync(path.join(from, element), path.join(to, element));
}
});
}
它适用于文件夹和文件。
fs-extra 模块就像一个魅力。
安装 fs-extra:
$ npm install fs-extra
以下是将源目录复制到目标目录的程序。
// Include the fs-extra package
var fs = require("fs-extra");
var source = 'folderA'
var destination = 'folderB'
// Copy the source folder to the destination
fs.copy(source, destination, function (err) {
if (err){
console.log('An error occurred while copying the folder.')
return console.error(err)
}
console.log('Copy completed!')
});
这对于 Node.js 10 来说非常简单:
const FSP = require('fs').promises;
async function copyDir(src,dest) {
const entries = await FSP.readdir(src, {withFileTypes: true});
await FSP.mkdir(dest);
for(let entry of entries) {
const srcPath = Path.join(src, entry.name);
const destPath = Path.join(dest, entry.name);
if(entry.isDirectory()) {
await copyDir(srcPath, destPath);
} else {
await FSP.copyFile(srcPath, destPath);
}
}
}
这假设dest
不存在。
我知道这里已经有很多答案,但没有人以简单的方式回答。
关于 fs-exra官方文档,你可以很容易地做到这一点。
const fs = require('fs-extra')
// Copy file
fs.copySync('/tmp/myfile', '/tmp/mynewfile')
// Copy directory, even if it has subdirectories or files
fs.copySync('/tmp/mydir', '/tmp/mynewdir')
我创建了一个小型工作示例,只需几个步骤即可将源文件夹复制到另一个目标文件夹(基于shift66 使用 ncp的回答):
第 1 步 - 安装 ncp 模块:
npm install ncp --save
第 2 步 - 创建 copy.js(将srcPath和destPath变量修改为您需要的任何内容):
var path = require('path');
var ncp = require('ncp').ncp;
ncp.limit = 16;
var srcPath = path.dirname(require.main.filename); // Current folder
var destPath = '/path/to/destination/folder'; // Any destination folder
console.log('Copying files...');
ncp(srcPath, destPath, function (err) {
if (err) {
return console.error(err);
}
console.log('Copying files complete.');
});
第 3 步 - 运行
node copy.js
具有符号链接支持的那个:
const path = require("path");
const {
existsSync,
mkdirSync,
readdirSync,
lstatSync,
copyFileSync,
symlinkSync,
readlinkSync,
} = require("fs");
export function copyFolderSync(src, dest) {
if (!existsSync(dest)) {
mkdirSync(dest);
}
readdirSync(src).forEach((entry) => {
const srcPath = path.join(src, entry);
const destPath = path.join(dest, entry);
const stat = lstatSync(srcPath);
if (stat.isFile()) {
copyFileSync(srcPath, destPath);
} else if (stat.isDirectory()) {
copyFolderSync(srcPath, destPath);
} else if (stat.isSymbolicLink()) {
symlinkSync(readlinkSync(srcPath), destPath);
}
});
}
从 Node v16.7.0 开始可以使用fs.cp
或fs.cpSync
运行。
fs.cp(src, dest, {recursive: true});
当前稳定性(在 Node v17.0.1 中)是Experimental。
因为我只是在构建一个简单的 Node.js 脚本,我不希望脚本的用户需要导入一堆外部模块和依赖项,所以我戴上我的思考帽并搜索了运行命令Bash 外壳。
此 Node.js 代码片段递归地将名为 node-webkit.app 的文件夹复制到名为 build 的文件夹中:
child = exec("cp -r node-webkit.app build", function(error, stdout, stderr) {
sys.print("stdout: " + stdout);
sys.print("stderr: " + stderr);
if(error !== null) {
console.log("exec error: " + error);
} else {
}
});
感谢dzone的 Lance Pollard 让我开始。
上面的代码片段仅限于基于 Unix 的平台,如 macOS 和 Linux,但类似的技术可能适用于 Windows。
fs-extra做了这件事,如果你不提供回调,它甚至可以返回一个Promise !:)
const path = require('path')
const fs = require('fs-extra')
let source = path.resolve( __dirname, 'folderA')
let destination = path.resolve( __dirname, 'folderB')
fs.copy(source, destination)
.then(() => console.log('Copy completed!'))
.catch( err => {
console.log('An error occurred while copying the folder.')
return console.error(err)
})
我为在目录之间递归地复制(copyFileSync)或移动(renameSync)文件编写了这个函数:
// Copy files
copyDirectoryRecursiveSync(sourceDir, targetDir);
// Move files
copyDirectoryRecursiveSync(sourceDir, targetDir, true);
function copyDirectoryRecursiveSync(source, target, move) {
if (!fs.lstatSync(source).isDirectory())
return;
var operation = move ? fs.renameSync : fs.copyFileSync;
fs.readdirSync(source).forEach(function (itemName) {
var sourcePath = path.join(source, itemName);
var targetPath = path.join(target, itemName);
if (fs.lstatSync(sourcePath).isDirectory()) {
fs.mkdirSync(targetPath);
copyDirectoryRecursiveSync(sourcePath, targetPath);
}
else {
operation(sourcePath, targetPath);
}
});
}
挑选包裹时要小心。某些包(如 copy-dir)不支持复制长度超过 0X1FFFFFE8 个字符(约 537 MB)的大文件。
它会抛出一些错误,例如:
buffer.js:630 未捕获的错误:无法创建长度超过 0x1fffffe8 个字符的字符串
我在我的一个项目中经历过类似的事情。最终,我不得不更改我正在使用的包并调整大量代码。我会说这不是一次非常愉快的经历。
如果需要多个源和多个目标副本,您可以使用better-copy并编写如下内容:
// Copy from multiple source into a directory
bCopy(['/path/to/your/folder1', '/path/to/some/file.txt'], '/path/to/destination/folder');
甚至:
// Copy from multiple source into multiple destination
bCopy(['/path/to/your/folder1', '/path/to/some/file.txt'], ['/path/to/destination/folder', '/path/to/another/folder']);
我尝试了 fs-extra 和 copy-dir 以递归方式复制文件夹。但我希望它
所以我自己写了:
// Node.js module for Node.js 8.6+
var path = require("path");
var fs = require("fs");
function copyDirSync(src, dest, options) {
var srcPath = path.resolve(src);
var destPath = path.resolve(dest);
if(path.relative(srcPath, destPath).charAt(0) != ".")
throw new Error("dest path must be out of src path");
var settings = Object.assign(Object.create(copyDirSync.options), options);
copyDirSync0(srcPath, destPath, settings);
function copyDirSync0(srcPath, destPath, settings) {
var files = fs.readdirSync(srcPath);
if (!fs.existsSync(destPath)) {
fs.mkdirSync(destPath);
}else if(!fs.lstatSync(destPath).isDirectory()) {
if(settings.overwrite)
throw new Error(`Cannot overwrite non-directory '${destPath}' with directory '${srcPath}'.`);
return;
}
files.forEach(function(filename) {
var childSrcPath = path.join(srcPath, filename);
var childDestPath = path.join(destPath, filename);
var type = fs.lstatSync(childSrcPath).isDirectory() ? "directory" : "file";
if(!settings.filter(childSrcPath, type))
return;
if (type == "directory") {
copyDirSync0(childSrcPath, childDestPath, settings);
} else {
fs.copyFileSync(childSrcPath, childDestPath, settings.overwrite ? 0 : fs.constants.COPYFILE_EXCL);
if(!settings.preserveFileDate)
fs.futimesSync(childDestPath, Date.now(), Date.now());
}
});
}
}
copyDirSync.options = {
overwrite: true,
preserveFileDate: true,
filter: function(filepath, type) {
return true;
}
};
还有一个类似的函数mkdirs,它是 mkdirp 的替代品:
function mkdirsSync(dest) {
var destPath = path.resolve(dest);
mkdirsSync0(destPath);
function mkdirsSync0(destPath) {
var parentPath = path.dirname(destPath);
if(parentPath == destPath)
throw new Error(`cannot mkdir ${destPath}, invalid root`);
if (!fs.existsSync(destPath)) {
mkdirsSync0(parentPath);
fs.mkdirSync(destPath);
}else if(!fs.lstatSync(destPath).isDirectory()) {
throw new Error(`cannot mkdir ${destPath}, a file already exists there`);
}
}
}
如果您在 Linux 上,并且性能不是问题,您可以使用模块中的exec
函数child_process
来执行 Bash 命令:
const { exec } = require('child_process');
exec('cp -r source dest', (error, stdout, stderr) => {...});
在某些情况下,我发现这个解决方案比下载整个模块甚至使用fs
模块更干净。
使用shelljs
npm i -D shelljs
const bash = require('shelljs');
bash.cp("-rf", "/path/to/source/folder", "/path/to/destination/folder");
这段代码可以正常工作,递归地将任何文件夹复制到任何位置。但它只是 Windows。
var child = require("child_process");
function copySync(from, to){
from = from.replace(/\//gim, "\\");
to = to.replace(/\//gim, "\\");
child.exec("xcopy /y /q \"" + from + "\\*\" \"" + to + "\\\"");
}
它非常适合用于创建新玩家的基于文本的游戏。
如果要递归复制源目录的所有内容,则需要传递recursive
选项 astrue
并由try
catch
fs-extra 记录sync
因为fs-extra
是完全替换fs
所以你不需要导入基本模块
const fs = require('fs-extra');
let sourceDir = '/tmp/src_dir';
let destDir = '/tmp/dest_dir';
try {
fs.copySync(sourceDir, destDir, { recursive: true })
console.log('success!')
} catch (err) {
console.error(err)
}
我是这样做的:
let fs = require('fs');
let path = require('path');
然后:
let filePath = // Your file path
let fileList = []
var walkSync = function(filePath, filelist)
{
let files = fs.readdirSync(filePath);
filelist = filelist || [];
files.forEach(function(file)
{
if (fs.statSync(path.join(filePath, file)).isDirectory())
{
filelist = walkSync(path.join(filePath, file), filelist);
}
else
{
filelist.push(path.join(filePath, file));
}
});
// Ignore hidden files
filelist = filelist.filter(item => !(/(^|\/)\.[^\/\.]/g).test(item));
return filelist;
};
然后调用方法:
This.walkSync(filePath, fileList)
当前的最佳答案可以大大简化。
const path = require('path');
const fs = require('fs');
function recursiveCopySync(source, target) {
if (fs.lstatSync(source).isDirectory()) {
if (!fs.existsSync(target)) {
fs.mkdirSync(target);
}
let files = fs.readdirSync(source);
files.forEach((file) => {
recursiveCopySync(path.join(source, file), path.join(target, file));
});
} else {
if (fs.existsSync(source)) {
fs.writeFileSync(target, fs.readFileSync(source));
}
}
}
ncp
虽然cool
...您可能想要/应该承诺它的功能来制作它super cool
。当您使用它时,将其添加到tools
文件中以重复使用它。
下面是一个工作版本,它是Async
并使用Promises
.
const {copyFolder} = require('./tools/');
return copyFolder(
yourSourcePath,
yourDestinationPath
)
.then(() => {
console.log('-> Backup completed.')
}) .catch((err) => {
console.log("-> [ERR] Could not copy the folder: ", err);
})
const ncp = require("ncp");
/**
* Promise Version of ncp.ncp()
*
* This function promisifies ncp.ncp().
* We take the asynchronous function ncp.ncp() with
* callback semantics and derive from it a new function with
* promise semantics.
*/
ncp.ncpAsync = function (sourcePath, destinationPath) {
return new Promise(function (resolve, reject) {
try {
ncp.ncp(sourcePath, destinationPath, function(err){
if (err) reject(err); else resolve();
});
} catch (err) {
reject(err);
}
});
};
/**
* Utility function to copy folders asynchronously using
* the Promise returned by ncp.ncp().
*/
const copyFolder = (sourcePath, destinationPath) => {
return ncp.ncpAsync(sourcePath, destinationPath, function (err) {
if (err) {
return console.error(err);
}
});
}
module.exports.copyFolder = copyFolder;
另一种使用 glob 通配符复制文件的方法。使用它,您可以有选择地复制,例如/my/dir/**/*.js
const glob = require('glob');
const path = require('path');
const fs = require('fs');
function copy(from, to) { //accepting glob pattern
const wildcard = from.indexOf('*') !== -1;
const pattern = !wildcard && fs.lstatSync(from).isDirectory() ? `${from}/**/*` : from;
glob.sync(pattern).forEach(file => {
const fromDirname = path.dirname(from.replace(/\/\*.*/, '/wildcard'));
const target = file.replace(fromDirname, to);
const [targetDir, recursive] = [path.dirname(target), true];
!fs.existsSync(targetDir) && fs.mkdirSync(targetDir, {recursive});
fs.lstatSync(file).isDirectory() ?
fs.mkdirSync(target, {recursive}) : fs.copyFileSync(file, target);
})
}
例子
copy('src/tools', 'dist')
copy('src/components/**/*', 'dist/elements')
copy('src/*.html', 'dist')
copy('src/*.css', 'dist')
copy('public/*', 'dist')
解决这个问题的最简单方法是仅使用“fs”和“Path”模块以及一些逻辑......
如果您只想设置版本号,则使用新名称复制根文件夹中的所有文件,即“var v = 'Your Directory Name'”
在文件名前缀加上文件名的内容。
var fs = require('fs-extra');
var path = require('path');
var c = 0;
var i = 0;
var v = "1.0.2";
var copyCounter = 0;
var directoryCounter = 0;
var directoryMakerCounter = 0;
var recursionCounter = -1;
var Flag = false;
var directoryPath = [];
var directoryName = [];
var directoryFileName = [];
var fileName;
var directoryNameStorer;
var dc = 0;
var route;
if (!fs.existsSync(v)) {
fs.mkdirSync(v);
}
var basePath = path.join(__dirname, v);
function walk(dir) {
fs.readdir(dir, function(err, items) {
items.forEach(function(file) {
file = path.resolve(dir, file);
fs.stat(file, function(err, stat) {
if(stat && stat.isDirectory()) {
directoryNameStorer = path.basename(file);
route = file;
route = route.replace("gd", v);
directoryFileName[directoryCounter] = route;
directoryPath[directoryCounter] = file;
directoryName[directoryCounter] = directoryNameStorer;
directoryCounter++;
dc++;
if (!fs.existsSync(basePath + "/" + directoryName[directoryMakerCounter])) {
fs.mkdirSync(directoryFileName[directoryMakerCounter]);
directoryMakerCounter++;
}
}
else {
fileName = path.basename(file);
if(recursionCounter >= 0) {
fs.copyFileSync(file, directoryFileName[recursionCounter] + "/" + v + "_" + fileName, err => {
if(err) return console.error(err);
});
copyCounter++;
}
else {
fs.copyFileSync(file, v + "/" + v + "_" + fileName, err => {
if(err) return console.error(err);
});
copyCounter++;
}
}
if(copyCounter + dc == items.length && directoryCounter > 0 && recursionCounter < directoryMakerCounter-1) {
console.log("COPY COUNTER: " + copyCounter);
console.log("DC COUNTER: " + dc);
recursionCounter++;
dc = 0;
copyCounter = 0;
console.log("ITEM DOT LENGTH: " + items.length);
console.log("RECURSION COUNTER: " + recursionCounter);
console.log("DIRECOTRY MAKER COUNTER: " + directoryMakerCounter);
console.log(": START RECURSION: " + directoryPath[recursionCounter]);
walk(directoryPath[recursionCounter]); //recursive call to copy sub-folder
}
})
})
});
}
walk('./gd', function(err, data) { // Just pass the root directory which you want to copy
if(err)
throw err;
console.log("done");
})