获取某一个目录下的所有文件

想把服务器上某个目录的文件全部拉一下信息,然后入库。

使用的模块

引入模块

1
2
const fs = require('fs');
const path = require('path');

使用函数

1
2
3
4
5
6
7
8
9
10
11
fs.readdirSync( path: PathLike,options?:| {encoding: BufferEncoding | null;withFileTypes?: false | undefined;recursive?: boolean | undefined;}| BufferEncoding| null,) //读取目录下的文件
path.join(...paths: string[]) //拼接路径
path.resolve(paths) //获取绝对路径
path.basename(paths) //获取文件名
path.dirname(paths) //获取文件夹路径
path.extname(paths) //获取文件扩展名
fs.statSync(path: PathLike, options?: undefined) //获取文件信息
stat.isDirectory() //判断是否是文件夹
stat.isFile() //判断是否是文件
stat.ctime //获取文件的创建时间
stat.size //获取文件的大小

代码实现

代码实现写的比较随意,只有获取文件信息的部分,入库在BaoKeDun的整体服务里。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const fs = require('fs');
const path = require('path');

function showfileinfo(filePath)
{
console.log(filePath);
console.log(path.extname(filePath));
console.log(path.dirname(filePath));
console.log(path.basename(filePath));
console.log(fs.statSync(filePath).size);
console.log(fs.statSync(filePath).ctime.toLocaleDateString());
}

function fileTest(filedir) {
fs.readdirSync(filedir).map(file => {
const filePath = path.join(filedir,file)
const fileStat = fs.statSync(filePath)
if(fileStat.isDirectory()){
fileTest(filePath)
}
if(fileStat.isFile()){
console.log(path.resolve(filePath));
showfileinfo(path.resolve(filePath))
}
})

}

fileTest('../source')

console.log("End");

fs模块下的各类函数

node的fs是文件系统模块,它提供了文件读取、写入、删除、重命名等操作。

文件读取

1
2
fs.readFile(path: PathLike, options: BufferEncoding | string | null | undefined, callback: (err: NodeJS.ErrnoException | null, data: Buffer) => void): void;
fs.readFileSync(path: PathLike, options?: string | BufferEncoding | null): Buffer;

文件写入

1
2
fs.writeFile(path: PathLike, data: any, options: WriteFileOptions, callback: (err: NodeJS.ErrnoException | null) => void): void;
fs.writeFileSync(path: PathLike, data: any, options?: string | BufferEncoding | null): void;

文件夹读取

1
2
fs.readdir(path: PathLike, options: { encoding: BufferEncoding | null; withFileTypes?: boolean | null | undefined; } | BufferEncoding | null, callback: (err: NodeJS.ErrnoException | null, files: string[]) => void): void;
fs.readdirSync(path: PathLike, options?: { encoding: BufferEncoding | null; withFileTypes?: boolean | null | undefined; } | BufferEncoding | null): string[];

文件夹写入

1
2
fs.mkdir(path: PathLike, options: MakeDirectoryOptions | undefined | null, callback: (err: NodeJS.ErrnoException | null) => void): void;
fs.mkdirSync(path: PathLike, options?: MakeDirectoryOptions | undefined | null): void;

文件夹删除

1
2
fs.rmdir(path: PathLike, callback: (err: NodeJS.ErrnoException | null) => void): void;
fs.rmdirSync(path: PathLike): void;

文件删除

1
2
fs.unlink(path: PathLike, callback: (err: NodeJS.ErrnoException | null) => void): void;
fs.unlinkSync(path: PathLike): void;

文件夹重命名

1
2
fs.rename(oldPath: PathLike, newPath: PathLike, callback: (err: NodeJS.ErrnoException | null) => void): void;
fs.renameSync(oldPath: PathLike, newPath: PathLike): void;

文件夹复制

1
2
fs.copyFile(src: PathLike, dest: PathLike, flags: number, callback: (err: NodeJS.ErrnoException | null) => void): void;
fs.copyFileSync(src: PathLike, dest: PathLike, flags: number): void;

文件夹移动

1
2
fs.rename(oldPath: PathLike, newPath: PathLike, callback: (err: NodeJS.ErrnoException | null) => void): void;
fs.renameSync(oldPath: PathLike, newPath: PathLike): void;

文件夹遍历

1
2
fs.readdir(path: PathLike, options: { encoding: BufferEncoding | null; withFileTypes?: boolean | null | undefined; } | BufferEncoding | null, callback: (err: NodeJS.ErrnoException | null, files: string[]) => void): void;
fs.readdirSync(path: PathLike, options?: { encoding: BufferEncoding | null; withFileTypes?: boolean | null | undefined; } | BufferEncoding | null): string[];

文件夹创建

1
2
fs.mkdir(path: PathLike, options: MakeDirectoryOptions | undefined | null, callback: (err: NodeJS.ErrnoException | null) => void): void;
fs.mkdirSync(path: PathLike, options?: MakeDirectoryOptions | undefined | null): void;

文件夹删除

1
2
fs.rmdir(path: PathLike, callback: (err: NodeJS.ErrnoException | null) => void): void;
fs.rmdirSync(path: PathLike): void;

文件删除

1
2
fs.unlink(path: PathLike, callback: (err: NodeJS.ErrnoException | null) => void): void;
fs.unlinkSync(path: PathLike): void;

path模块下的各类函数

path模块提供了一些用于处理文件路径的函数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
path.join() //拼接路径
path.resolve() //获取绝对路径
path.basename() //获取文件名
path.dirname() //获取文件夹路径
path.extname() //获取文件扩展名
path.parse() //获取文件信息
path.format() //格式化路径
path.normalize() //标准化路径
path.relative() //获取相对路径
path.sep //获取路径分隔符
path.delimiter //获取路径分隔符
path.win32() //获取windows路径
path.posix() //获取posix路径
path.toNamespacedPath() //获取命名空间路径
path.isAbsolute() //判断是否是绝对路径