javascript/node.js中如何删除当前目录下的多个PDF文件
How to delete multiple PDF files in the current directory in javascript/node.js
我想删除当前目录下多个以.pdf结尾的文件。假设我有 3 个不同的 pdf 文件、1 个图像文件和一个文本文件,那么在这些文件中,我只想删除这 3 个不同的 pdf 文件.
我试过了
第一种方法
fs.unlinkSync('./'+*+pdfname);
-> 我知道这没有意义
第二种方法
try {
var files = (here the list of files should come. However i am failing to get those);
var path="./"
files.forEach(path => fs.existsSync(path) && fs.unlinkSync(path))
} catch (err) {
console.error("not exist")
}
任何不同的方法将不胜感激。
解决方案更新:
我已经找到了满足我要求的解决方案,我只是想让我的函数删除所有 pdf 文件并且函数是同步的。然而,99%的解决方案由以下作者给出 ->
fs.readdir
是异步的,只需要让它同步即可 fs.readdirSync
.
下面是更新后的代码,所有的功劳都应该归功于作者 。
更新代码
try {
const path = './'
// Read the directory given in `path`
fs.readdirSync(path).forEach((file) => {
// Check if the file is with a PDF extension, remove it
if (file.split('.').pop().toLowerCase() === 'pdf') {
console.log(`Deleting file: ${file}`);
fs.unlinkSync(path + file)
}
});
console.log("Deleted all the pdf files")
return true;
} catch (err) {
console.error("Error in deleting files",err);
}
您可以使用 fs.readdir
读取目录,然后检查 PDF 文件并将其删除。像这样:
fs = require('fs');
try {
path = './'
// Read the directory given in `path`
const files = fs.readdir(path, (err, files) => {
if (err)
throw err;
files.forEach((file) => {
// Check if the file is with a PDF extension, remove it
if (file.split('.').pop().toLowerCase() == 'pdf') {
console.log(`Deleting file: ${file}`);
fs.unlinkSync(path + file)
}
});
});
} catch (err) {
console.error(err);
}
初读
The current working directory of the Node.js process - 请参阅下面的更新
Path 方法
resolve
extName
File System 方法
fs.readdirSync
fs.unlinkSync
和类
例子
"use strict";
const fs = require('fs');
const path = require('path');
const cwd = process.cwd();
fs.readdirSync( cwd, {withFileTypes: true})
.forEach( dirent => {
if(dirent.isFile()) {
const fileName = dirent.name;
if( path.extname(fileName).toLowerCase() === ".pdf") {
fs.unlinkSync( path.resolve( cwd, fileName));
}
}
});
备注
- 未经测试的代码
- 如果
unlinkSync
失败,我会根据文档中链接的 unlink(2) 手册页假设它 returns -1。我个人会使用 cwd
. 中不存在的文件名进行测试
- 我相信
readdirSync
returns dirent
对象的 {withFileTypes: true}
选项具有 mime 类型值,可以让您检查 [=21] 类型的文件=] 无论扩展名如何(示例中未尝试)。
更新:path(resolve)
必要时默认将当前工作目录添加到返回路径的开头。 path.resolve(fileName)
与示例中的 path.resolve(cwd, fileName)
一样有效。
您似乎知道如何删除 (unlink
) 文件 - 您是在问如何获取文件路径?
尝试使用 glob
:
const pdfFiles = require("glob").globSync("*.pdf");
您好,我附上了用于删除目录中所有(仅).pdf 文件而不是其他扩展文件(如 .txt、.docs 等)的测试代码。
注意:您只能从服务器端删除任何文件或目录。
const fs = require('fs');
const path = require('path')
fs.readdir('../path to directory', (err, files) => {
const pdfFiles = files.filter(el => path.extname(el) === '.pdf')
pdfFiles.forEach(file => {
console.log("Removing File -> ",file);
var filename = "../path to directory/"+file;
fs.unlink(filename,function(err){
if(err) return console.log(err);
console.log('file deleted successfully');
});
});
});
这将在控制台日志中给出以下结果。
正在删除文件 -> note.pdf
删除文件 -> note2.pdf
文件删除成功
文件删除成功
如果有任何疑问,请随时发表评论..
我想删除当前目录下多个以.pdf结尾的文件。假设我有 3 个不同的 pdf 文件、1 个图像文件和一个文本文件,那么在这些文件中,我只想删除这 3 个不同的 pdf 文件.
我试过了
第一种方法
fs.unlinkSync('./'+*+pdfname);
-> 我知道这没有意义
第二种方法
try {
var files = (here the list of files should come. However i am failing to get those);
var path="./"
files.forEach(path => fs.existsSync(path) && fs.unlinkSync(path))
} catch (err) {
console.error("not exist")
}
任何不同的方法将不胜感激。
解决方案更新:
我已经找到了满足我要求的解决方案,我只是想让我的函数删除所有 pdf 文件并且函数是同步的。然而,99%的解决方案由以下作者给出 ->
fs.readdir
是异步的,只需要让它同步即可 fs.readdirSync
.
下面是更新后的代码,所有的功劳都应该归功于作者
更新代码
try {
const path = './'
// Read the directory given in `path`
fs.readdirSync(path).forEach((file) => {
// Check if the file is with a PDF extension, remove it
if (file.split('.').pop().toLowerCase() === 'pdf') {
console.log(`Deleting file: ${file}`);
fs.unlinkSync(path + file)
}
});
console.log("Deleted all the pdf files")
return true;
} catch (err) {
console.error("Error in deleting files",err);
}
您可以使用 fs.readdir
读取目录,然后检查 PDF 文件并将其删除。像这样:
fs = require('fs');
try {
path = './'
// Read the directory given in `path`
const files = fs.readdir(path, (err, files) => {
if (err)
throw err;
files.forEach((file) => {
// Check if the file is with a PDF extension, remove it
if (file.split('.').pop().toLowerCase() == 'pdf') {
console.log(`Deleting file: ${file}`);
fs.unlinkSync(path + file)
}
});
});
} catch (err) {
console.error(err);
}
初读
The current working directory of the Node.js process - 请参阅下面的更新
Path 方法
resolve
extName
File System 方法
fs.readdirSync
fs.unlinkSync
和类
例子
"use strict";
const fs = require('fs');
const path = require('path');
const cwd = process.cwd();
fs.readdirSync( cwd, {withFileTypes: true})
.forEach( dirent => {
if(dirent.isFile()) {
const fileName = dirent.name;
if( path.extname(fileName).toLowerCase() === ".pdf") {
fs.unlinkSync( path.resolve( cwd, fileName));
}
}
});
备注
- 未经测试的代码
- 如果
unlinkSync
失败,我会根据文档中链接的 unlink(2) 手册页假设它 returns -1。我个人会使用cwd
. 中不存在的文件名进行测试
- 我相信
readdirSync
returnsdirent
对象的{withFileTypes: true}
选项具有 mime 类型值,可以让您检查 [=21] 类型的文件=] 无论扩展名如何(示例中未尝试)。
更新:path(resolve)
必要时默认将当前工作目录添加到返回路径的开头。 path.resolve(fileName)
与示例中的 path.resolve(cwd, fileName)
一样有效。
您似乎知道如何删除 (unlink
) 文件 - 您是在问如何获取文件路径?
尝试使用 glob
:
const pdfFiles = require("glob").globSync("*.pdf");
您好,我附上了用于删除目录中所有(仅).pdf 文件而不是其他扩展文件(如 .txt、.docs 等)的测试代码。
注意:您只能从服务器端删除任何文件或目录。
const fs = require('fs'); const path = require('path') fs.readdir('../path to directory', (err, files) => { const pdfFiles = files.filter(el => path.extname(el) === '.pdf') pdfFiles.forEach(file => { console.log("Removing File -> ",file); var filename = "../path to directory/"+file; fs.unlink(filename,function(err){ if(err) return console.log(err); console.log('file deleted successfully'); }); }); });
这将在控制台日志中给出以下结果。
正在删除文件 -> note.pdf
删除文件 -> note2.pdf
文件删除成功
文件删除成功
如果有任何疑问,请随时发表评论..