栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

遍历文件夹Node.JS中的文件

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

遍历文件夹Node.JS中的文件

带回调的旧答案

您想要使用fs.readdir函数获取目录内容,并使用fs.rename函数实际执行重命名。如果您
需要 等待它们完成之后再运行代码,则这两个函数都具有同步版本。

我写了一个快速脚本来完成您所描述的。

var fs = require('fs');var path = require('path');// In newer Node.js versions where process is already global this isn't necessary.var process = require("process");var moveFrom = "/home/mike/dev/node/sonar/moveme";var moveTo = "/home/mike/dev/node/sonar/tome"// Loop through all the files in the temp directoryfs.readdir(moveFrom, function (err, files) {  if (err) {    console.error("Could not list the directory.", err);    process.exit(1);  }  files.forEach(function (file, index) {    // Make one pass and make the file complete    var fromPath = path.join(moveFrom, file);    var toPath = path.join(moveTo, file);    fs.stat(fromPath, function (error, stat) {      if (error) {        console.error("Error stating file.", error);        return;      }      if (stat.isFile())        console.log("'%s' is a file.", fromPath);      else if (stat.isDirectory())        console.log("'%s' is a directory.", fromPath);      fs.rename(fromPath, toPath, function (error) {        if (error) {          console.error("File moving error.", error);        } else {          console.log("Moved file '%s' to '%s'.", fromPath, toPath);        }      });    });  });});

在我的本地机器上测试。

node testme.js '/home/mike/dev/node/sonar/moveme/hello' is a file.'/home/mike/dev/node/sonar/moveme/test' is a directory.'/home/mike/dev/node/sonar/moveme/test2' is a directory.'/home/mike/dev/node/sonar/moveme/test23' is a directory.'/home/mike/dev/node/sonar/moveme/test234' is a directory.Moved file '/home/mike/dev/node/sonar/moveme/hello' to '/home/mike/dev/node/sonar/tome/hello'.Moved file '/home/mike/dev/node/sonar/moveme/test' to '/home/mike/dev/node/sonar/tome/test'.Moved file '/home/mike/dev/node/sonar/moveme/test2' to '/home/mike/dev/node/sonar/tome/test2'.Moved file '/home/mike/dev/node/sonar/moveme/test23' to '/home/mike/dev/node/sonar/tome/test23'.Moved file '/home/mike/dev/node/sonar/moveme/test234' to '/home/mike/dev/node/sonar/tome/test234'.

更新:fs.promises函数具有异步/等待功能

受ma11hew28答案的启发,这与上面相同,但具有fs.promises中的异步功能。如ma11hew28所述,与v12.12.0中添加的fs.promises.opendir
相比,它可能具有内存限制。

下面的快速代码。

//jshint esversion:8//jshint node:trueconst fs = require( 'fs' );const path = require( 'path' );const moveFrom = "/tmp/movefrom";const moveTo = "/tmp/moveto";// Make an async function that gets executed immediately(async ()=>{    // Our starting point    try {        // Get the files as an array        const files = await fs.promises.readdir( moveFrom );        // Loop them all with the new for...of        for( const file of files ) { // Get the full paths const fromPath = path.join( moveFrom, file ); const toPath = path.join( moveTo, file ); // Stat the file to see if we have a file or dir const stat = await fs.promises.stat( fromPath ); if( stat.isFile() )     console.log( "'%s' is a file.", fromPath ); else if( stat.isDirectory() )     console.log( "'%s' is a directory.", fromPath ); // Now move async await fs.promises.rename( fromPath, toPath ); // Log because we're crazy console.log( "Moved '%s'->'%s'", fromPath, toPath );        } // End for...of    }    catch( e ) {        // Catch anything bad that happens        console.error( "We've thrown! Whoops!", e );    }})(); // Wrap in parenthesis and call now


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/507061.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号