简体   繁体   English

如何使用 Node.js 制作应用程序,它可以分隔所有具有相同扩展名的文件?

[英]How can i make an application using Node.js, which can separate all the files with same extension?

在此处输入图像描述

How to write code in node.JS in which we select a directory and the code automatically separates all the files of that selected directory with the same extension and put then in a separate folder.如何在 node.JS 中编写代码,其中我们 select 一个目录,代码自动分隔该选定目录的所有具有相同扩展名的文件,然后放在一个单独的文件夹中。

Something like this will work.像这样的东西会起作用。 Using fs module and path module使用fs模块和path模块

Which will first check the extension and rename file (move) to new folder.这将首先检查扩展名并将文件重命名(移动)到新文件夹。

You can make changes accordingly.您可以进行相应的更改。 Use if使用if


const testFolder = './';
const fs = require('fs');
var path = require('path')

var oldPath = 'old/path/file.txt'
var newPath = 'new/path/file.txt'

fs.readdir(testFolder, (err, files) => {
    files.forEach(file => {
        const ext = path.extname(file);
        fs.rename(oldPath, newPath, function (err) {
            if (err) throw err
            console.log('Successfully renamed - AKA moved!')
        })
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Node.js:组织我的应用程序:在单独的文件中使用路由和模型,如何从路由中获取模型? - Node.js : organising my application : using routes and models in separate files, how can I get models from routes? 如何将代码分成两个文件,并使其仍在node.js中工作? - How can I separate a code in two files, and still make it work in node.js? 如何使用 Node.JS 将文件移动到目录? - How can I move files to a directory using Node.JS? 如何在Node.js Express应用程序中使脚本文件可用于客户端? - How can I make script files available to the client in a Node.js Express app? 如何定义可以使用node.js从不同文件访问的变量? - How can I define variables that can be accessed from different files using node.js? 如何从独立的 node.js 应用程序使用 ethers.js 连接到 Metamask 钱包? - How can I connect to a Metamask wallet using ethers.js from a standalone node.js application? 如何使用node.js和express.js从表单发出和删除请求? - How can I make put and delete requests from a form using node.js and express.js? 如何启动这个 Node.JS 应用程序? - How can I start this Node.JS application? 如何返回目录文件? (JavaScript,Node.js) - How can I return the files of a directory? (JavaScript, Node.js) 如何在 node.js 中使此请求同步 - How can I make this request synchronous in node.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM