目录
文件|目录 存在,可读、可写权限
/**
* 文件|目录 存在、可读、可写
* @param path
* @returns
*/
export function checkExistSync(path: string): boolean {
try {
_fs.accessSync(
path,
_fs.constants.F_OK | _fs.constants.R_OK | _fs.constants.W_OK
);
// console.log(`Directory "${path}" exists.`);
return true;
} catch (err) {
console.log(`Directory "${path}" does not exist.`);
return false;
}
}
常量 | 描述 |
---|---|
F_OK | 指示文件对调用进程可见的标志。 这对于确定文件是否存在很有用,但没有说明 rwx 权限。 未指定模式时的默认值。 |
R_OK | 指示文件可以被调用进程读取的标志。 |
W_OK | 指示文件可以被调用进程写入的标志。 |
X_OK | 指示文件可以被调用进程执行的标志。 这在 Windows 上不起作用(行为类似于 fs.constants.F_OK )。 |
拷贝文件
/**
* 拷贝文件
* @param source
* @param destination
* @returns
*/
export async function copyFile(
srcPath: string,
desPath: string
): Promise<string> {
return new Promise((resolve, reject) => {
try {
const writeStream = _fs.createWriteStream(desPath);
const readStream = _fs.createReadStream(srcPath);
readStream.on("error", reject);
readStream.on("close", resolve);
writeStream.on("error", reject);
writeStream.on("finish", () => {
console.log("File copied successfully!");
});
//将原文件流导向目标文件流
readStream.pipe(writeStream);
} catch (error) {
console.error(error);
}
});
}
递归拷贝目录
/**
* 递归拷贝目录
* @param source
* @param destination
*/
export async function copyDirectory(srcPath: string, desPath: string) {
try {
const isSrcExist = checkExistSync(srcPath);
if (!isSrcExist) _fs.ensureDirSync(srcPath);
const isDesExist = checkExistSync(desPath);
if (!isDesExist) _fs.ensureDirSync(desPath);
// 读取源目录中的所有文件和子目录
const files = await _fs.readdir(srcPath);
for (const file of files) {
const copyPath = _path.join(srcPath, file);
const destPath = _path.join(desPath, file);
const stats = await _fs.stat(copyPath);
if (stats.isDirectory()) {
// 如果是子目录,递归拷贝
await copyDirectory(copyPath, destPath);
} else {
// 如果是文件,直接拷贝
await copyFile(copyPath, destPath);
}
}
// console.log(`Directory copied successfully from ${srcPath} to ${desPath}`);
} catch (error: any) {
console.error(`Error copying directory: ${error.message}`);
}
}
递归读取目录 并按照regex筛选出需要的文件路径
/**
* 递归读取目录 并按照regex筛选出需要的文件路径
* @param dirPath
* @param regex
* @returns
*/
export async function readDirRecursively(
dirPath: string,
regex: RegExp
): Promise<string[]> {
const filter: string[] = [];
const files = await _fs.readdir(dirPath);
for (const file of files) {
const tempPath = _path.resolve(dirPath, file);
const stat = await _fs.stat(tempPath);
if (stat.isDirectory()) {
const tempFilter = await readDirRecursively(tempPath, regex);
filter.push(...tempFilter);
} else if (stat.isFile()) {
const extname = _path.basename(tempPath);
if (regex.test(extname)) {
filter.push(tempPath);
}
}
}
return filter;
}
完整代码
import * as _fs from "fs-extra";
import * as _path from "path";
/**
* 文件|目录 存在、可读、可写
* @param path
* @returns
*/
export function checkExistSync(path: string): boolean {
try {
_fs.accessSync(
path,
_fs.constants.F_OK | _fs.constants.R_OK | _fs.constants.W_OK
);
return true;
} catch (err) {
return false;
}
}
/**
* 递归读取目录 并按照regex筛选出需要的文件路径
* @param dirPath
* @param regex
* @returns
*/
export async function readDirRecursively(
dirPath: string,
regex: RegExp
): Promise<string[]> {
const filter: string[] = [];
const files = await _fs.readdir(dirPath);
for (const file of files) {
const tempPath = _path.resolve(dirPath, file);
const stat = await _fs.stat(tempPath);
if (stat.isDirectory()) {
const tempFilter = await readDirRecursively(tempPath, regex);
filter.push(...tempFilter);
} else if (stat.isFile()) {
const extname = _path.basename(tempPath);
if (regex.test(extname)) {
filter.push(tempPath);
}
}
}
return filter;
}
/**
* 拷贝文件
* @param source
* @param destination
* @returns
*/
export async function copyFile(
srcPath: string,
desPath: string
): Promise<string> {
return new Promise((resolve, reject) => {
try {
const writeStream = _fs.createWriteStream(desPath);
const readStream = _fs.createReadStream(srcPath);
readStream.on("error", reject);
readStream.on("close", resolve);
writeStream.on("error", reject);
writeStream.on("finish", () => {
console.log("File copied successfully!");
});
//将原文件流导向目标文件流
readStream.pipe(writeStream);
} catch (error) {
console.error(error);
}
});
}
/**
* 递归拷贝目录
* @param source
* @param destination
*/
export async function copyDirectory(srcPath: string, desPath: string) {
try {
const isSrcExist = checkExistSync(srcPath);
if (!isSrcExist) _fs.ensureDirSync(srcPath);
const isDesExist = checkExistSync(desPath);
if (!isDesExist) _fs.ensureDirSync(desPath);
// 读取源目录中的所有文件和子目录
const files = await _fs.readdir(srcPath);
for (const file of files) {
const copyPath = _path.join(srcPath, file);
const destPath = _path.join(desPath, file);
const stats = await _fs.stat(copyPath);
if (stats.isDirectory()) {
// 如果是子目录,递归拷贝
await copyDirectory(copyPath, destPath);
} else {
// 如果是文件,直接拷贝
await copyFile(copyPath, destPath);
}
}
// console.log(`Directory copied successfully from ${srcPath} to ${desPath}`);
} catch (error: any) {
console.error(`Error copying directory: ${error.message}`);
}
}
使用示例
拷贝目录
const srcPath = "/Users/leiming/cocos/shader/shader2x";
const desPath = "/Users/leiming/cocos/shader/copyShader2x";
await copyDirectory(srcPath, desPath);
从目录中筛选指定文件的路径并拷贝到filterPng目录下
//找到shader2x 目录中的所有png 图片地址
const dirPath = "/Users/leiming/cocos/shader/shader2x";
const files = await filterFilesInDirByRegex(dirPath, /\.png$/);
console.log(files);
//把png图片拷贝到filterPng目录下
const desPath = "/Users/leiming/cocos/shader/filterPng";
_fs.ensureDirSync(desPath);
for (const file of files) {
const desFilePath = _path.resolve(desPath, _path.basename(file));
await copyFile(file, desFilePath);
}