你可以使用Node.js的文件系统模块(fs)来实现这个功能。下面是一个示例的JavaScript代码:

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

// 递归遍历目录,查找相同名称的文件
function searchFiles(directory, filename) {
  fs.readdir(directory, (err, files) => {
    if (err) throw err;

    files.forEach(file => {
      const filePath = path.join(directory, file);

      // 判断文件是否存在
      fs.stat(filePath, (err, stats) => {
        if (err) throw err;

        if (stats.isFile() && file === filename) {
          console.log(filePath);
        } else if (stats.isDirectory()) {
          // 递归遍历子目录
          searchFiles(filePath, filename);
        }
      });
    });
  });
}

// 调用函数,传入目录和文件名
searchFiles('/path/to/directory', 'filename.txt');

你需要将/path/to/directory替换为你要搜索的目录的实际路径,filename.txt替换为你要搜索的文件名。这段代码将遍历指定目录及其子目录,查找相同名称的文件,并输出文件的完整路径

如何查找不同目录下相同名称的文件输出文件的路径?js代码

原文地址: http://www.cveoy.top/t/topic/iyQW 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录