如何在Windows系统中编写脚本查找包含指定字符串的有效路径?
Hey there! Let's tackle your two questions about finding paths containing a specific string, and scripting this on Windows.
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 thedircommand 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)
Replaceyour_target_stringwith 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 FullNameThis skips directories you don't have permission to access (
-ErrorAction SilentlyContinue) and outputs only the full, valid paths in your desired\path\to\fileformat.
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
TARGETandROOTto match your needs - Results are saved to
found_paths.txtin 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
ROOTpath 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-liketo-eqin PowerShell).
内容的提问来源于stack exchange,提问作者nano

