Filter by keyword batch script issue

Nicholas Steuart 20 Reputation points
2025-06-05T05:27:28.5533333+00:00

Hi,

I am new to batch scripting, and I am currently trying to write a script that filters through a directory for filenames containing a keyword and write those files to a text document. It will then iterate onto the next keyword in the string and repeat the process. Unfortunately, I need the keywords to include the parenthesis as otherwise it will include unnecessary files when filtering certain keywords, "TH" for example.

This is the script I have written. It completes but it does not save the filenames to the outfiles. It currently just echoes the print statements to console, creates the outfiles, and the outfiles contain no data. What have I done wrong? Sorry if I have posed this question to the wrong tag, there doesn't seem to be a batch script tag.

@echo off

setlocal EnableDelayedExpansion

set "searchDir=P:"

set keywords="(BC)" "(AKF)"

for %%K in (%keywords%) do (

set "keyword=%%~K" 

set "outfile=%USERPROFILE%\Documents\PTA !keyword!.txt" 

echo Searching for files with keyword "!keyword!" in the filename... 

> "!outfile!" echo Files containing "!keyword!" in the filename: 

for %%F in ("%searchDir%\*.*") do ( 

	set "filename=%%~nxF" 

	echo !filename! | findstr /L /I /C:"!keyword!" >nul 

	if not errorlevel 1 ( 

		echo %%~nxF >> "!outfile!" 

	) 

) 

echo Done with keyword "!keyword!". Results saved to: !outfile!. 

)

endlocal

Windows for business Windows Client for IT Pros User experience Other
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 36,291 Reputation points
    2025-06-05T22:16:09.7033333+00:00

    there doesn't seem to be a batch script tag.

    That's probably because the .bat script syntax is outdated and, in my opinion, just terrible. Powershell is a much better choice as a scripting language.

    The added benefit is that as you become more familiar with Powershell, you can use it for all kinds of support tasks.

    cls
    $SearchDir = "c:\utils"
    $Keywords = "IIS", "invoke-command", "xyzzy"
    $results = @() 
    foreach ($kw in $Keywords) { 
        Write-Host "Searching for $kw"
        foreach ($f in (Get-ChildItem -Path $SearchDir -recurse -File)) {
            $search = Select-String -Path $f.fullname -Pattern $kw
            if ($search.count -gt 0) {
                $results +=   [PSCustomObject]@{
                    KeyWord = $kw
                    Count = $search.count
                    File = $f.FullName
                }
            }
        }
    }
    Write-Host "Here is what I found."
    $results                                    # show the results 
    # Uncomment to save the results to a file
    # $results | Out-File C:\Temp\results.txt  
    # $results | Export-Csv -Path "c:\temp\results.csv" -NoTypeInformation
     
    

    Use Powershell_ISE to develop and test the code.

    https://www.bing.com/search?q=powershell+tutorial

    User's image


1 additional answer

Sort by: Most helpful
  1. Since NT 3.5 10 Reputation points
    2025-06-05T13:49:01.6066667+00:00

    Can you not just go simple old school?

    (search for files that contain "th")

    dir th.* /s >> output.txt

    there are options for the DIR command (remove headers, etc)


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.