获取某一个目录下的所有文件
想把服务器上某个目录的文件全部拉一下信息,然后入库。
使用的模块
引入模块
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() path.posix() path.toNamespacedPath() path.isAbsolute()
|