You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js文件字符串检索脚本优化:添加未找到提示并修复循环问题

Fixing the "Nothing found" Duplicate Output Issue in Your File Search Script

Hey there! Sounds like your Node.js file search script is misbehaving when scanning multiple files—outputting Nothing found repeatedly even when some files do contain your target string. The root cause here is almost certainly that you're checking each file individually and printing Nothing found every time a single file doesn't match, instead of waiting until you've scanned all files to see if any matches exist.

Let's fix this with a straightforward approach: collect all matching files first, then decide what to print once the entire scan is done.

Step 1: Core Logic Overhaul

Instead of printing results as you check each file, we'll:

  1. Recursively scan the target directory and subdirectories
  2. Collect all files that match your extension and contain the target string into an array
  3. After the full scan, check if the array has any entries:
    • If yes: Print each matching file path
    • If no: Print Nothing found once

Step 2: Fixed Synchronous Script

Here's a complete working version using synchronous file operations (great for small directories):

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

// Grab command line arguments
const [scriptName, targetDir, searchStr, fileExt] = process.argv.slice(2);

// Validate input
if (!targetDir || !searchStr || !fileExt) {
  console.error('Usage: node [script-name.js] [target-folder] [search-string] [file-extension]');
  process.exit(1);
}

// Array to store all matching files
const matchingFiles = [];

// Recursive directory scan function
function scanDir(currentDir) {
  const files = fs.readdirSync(currentDir);

  for (const file of files) {
    const fullPath = path.join(currentDir, file);
    const fileStats = fs.statSync(fullPath);

    if (fileStats.isDirectory()) {
      // Recurse into subdirectories
      scanDir(fullPath);
    } else if (path.extname(file) === `.${fileExt}`) {
      // Check if file has the right extension, then read its content
      try {
        const fileContent = fs.readFileSync(fullPath, 'utf8');
        if (fileContent.includes(searchStr)) {
          matchingFiles.push(fullPath);
        }
      } catch (err) {
        console.error(`Failed to read file ${fullPath}: ${err.message}`);
      }
    }
  }
}

// Start the scan
scanDir(targetDir);

// Output results after full scan
if (matchingFiles.length > 0) {
  matchingFiles.forEach(file => {
    console.log(`Your word was found in directory: ${file}`);
  });
} else {
  console.log('Nothing found');
}

Step 3: Asynchronous Version (For Large Directories)

If you're scanning big directories, synchronous operations can block your Node.js event loop. Here's an async/await alternative that's non-blocking:

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

async function scanDir(currentDir, searchStr, fileExt) {
  const matchingFiles = [];
  const files = await fs.readdir(currentDir);

  for (const file of files) {
    const fullPath = path.join(currentDir, file);
    const fileStats = await fs.stat(fullPath);

    if (fileStats.isDirectory()) {
      // Recurse into subdirectories and merge results
      const subDirMatches = await scanDir(fullPath, searchStr, fileExt);
      matchingFiles.push(...subDirMatches);
    } else if (path.extname(file) === `.${fileExt}`) {
      try {
        const fileContent = await fs.readFile(fullPath, 'utf8');
        if (fileContent.includes(searchStr)) {
          matchingFiles.push(fullPath);
        }
      } catch (err) {
        console.error(`Failed to read file ${fullPath}: ${err.message}`);
      }
    }
  }

  return matchingFiles;
}

async function main() {
  const [scriptName, targetDir, searchStr, fileExt] = process.argv.slice(2);

  if (!targetDir || !searchStr || !fileExt) {
    console.error('Usage: node [script-name.js] [target-folder] [search-string] [file-extension]');
    process.exit(1);
  }

  const matchingFiles = await scanDir(targetDir, searchStr, fileExt);

  if (matchingFiles.length > 0) {
    matchingFiles.forEach(file => {
      console.log(`Your word was found in directory: ${file}`);
    });
  } else {
    console.log('Nothing found');
  }
}

// Run the main function and catch any errors
main().catch(err => console.error('Scan failed:', err.message));

How This Fixes Your Problem

  • No more repeated Nothing found messages: We only check for matches after scanning every file in the target directory and subdirectories.
  • Clear, consistent output: You'll get a list of all matching files if any exist, or a single Nothing found if zero matches are found.

Test this with your scenario (4 files, 1 matching) and you'll see only the matching file path is printed—no extra Nothing found lines!

内容的提问来源于stack exchange,提问作者Anatsu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 04:21:58