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

如何在Windows系统中编写脚本查找包含指定字符串的有效路径?

Hey there! Let's tackle your two questions about finding paths containing a specific string, and scripting this on Windows.

1. Finding Paths Containing a Specific String (Manual Methods)

If you don't want to write a script right away, you can use built-in Windows tools to get the job done quickly:

  • Command Prompt (CMD)
    Use the dir command with recursive and bare-format flags to output only full paths:

    dir /s /b "*your_target_string*"
    
    • /s: Recursively searches all subdirectories under the current working directory
    • /b: Returns only the full path of matching files/folders (no extra metadata)
      Replace your_target_string with the text you're looking for, and the * wildcards let you match the string anywhere in the path.
  • PowerShell
    For more control, PowerShell offers a flexible way to filter paths directly:

    Get-ChildItem -Recurse -ErrorAction SilentlyContinue | 
      Where-Object { $_.FullName -like "*your_target_string*" } | 
      Select-Object -ExpandProperty FullName
    

    This skips directories you don't have permission to access (-ErrorAction SilentlyContinue) and outputs only the full, valid paths in your desired \path\to\file format.

2. Scripting the Search: Feasibility & Windows Implementation

Absolutely feasible! Scripting this lets you automate repeated searches, save results, or add custom logic. Here are two reliable approaches for Windows:

Batch Script (.bat)

Simple and compatible with all Windows versions, no extra tools needed:

@echo off
:: Set your target string and root search path here
set "TARGET=your_search_text"
set "ROOT=C:\"

echo Searching for paths containing "%TARGET%" in %ROOT%...
:: Recursively find paths and save results to a text file
dir /s /b "%ROOT%\*%TARGET%*" > found_paths.txt

echo Done! Found paths saved to found_paths.txt
pause
  • Customize TARGET and ROOT to match your needs
  • Results are saved to found_paths.txt in the same folder as the script

PowerShell Script (.ps1)

More powerful, with error handling and flexibility:

# Define your search parameters
$targetString = "your_search_text"
$rootPath = "C:\"

# Recursively search for paths containing the target string
$foundPaths = Get-ChildItem -Path $rootPath -Recurse -ErrorAction SilentlyContinue |
              Where-Object { $_.FullName -like "*$targetString*" } |
              Select-Object -ExpandProperty FullName

# Output results to console and save to file
if ($foundPaths) {
    Write-Host "Found $($foundPaths.Count) matching paths:"
    $foundPaths | ForEach-Object { Write-Host "  $_" }
    $foundPaths | Out-File -FilePath "found_paths.txt" -Encoding UTF8
    Write-Host "`nResults saved to found_paths.txt"
} else {
    Write-Host "No paths containing '$targetString' were found."
}
  • Handles permission errors gracefully
  • Counts and lists results in the console before saving to a file
  • Easy to extend (e.g., add filters for file types, exclude specific directories)

Key Notes

  • Permissions: To search system directories like C:\Windows, run the script/command prompt as an administrator—otherwise, you'll get access denied errors for restricted folders.
  • Performance: Searching an entire drive (like C:) can take time. Narrow down the ROOT path to a specific folder if possible to speed things up.
  • Exact Matches: If you need to match the string exactly (not as a substring), remove the * wildcards (e.g., dir /s /b "your_target_string" or change -like to -eq in PowerShell).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 03:43:11