简体   繁体   中英

Batch file to find all files in a directory containing an HTML string then output list to a text file

I've made several attempts at this but get nothing but "can't open..." errors, so I'm asking here:

I want to find all instances of the string "SOME TEXT" within a directory full of HTML files. Then the search results should be output to a file in that same directory (D:\\myfiles)

Here's a sample batch file that'll do the trick.

@echo off
setlocal
pushd D:\myfiles

rem case-insensitive search for the string "SOME TEXT" in all html files
rem in the current directory, piping the output to the results.txt file
rem in teh same directory
findstr /ip /c:"SOME TEXT" *.html > results.txt

popd
endlocal

Update : Some caveats to using findstr command.

If your string contains angle brackets, you have to escape them using the CMD escape character - ^. So, if you want to search for <TITLE> , you have to specify it as /c:"^<TITLE^>" .

If you want only file names, change /ip to /im. Also, you can add /s to search subfolders. In general, you can play with the different findstr options as listed in findstr /?.

Findstr will find the text only in UTF-8 encoded files. If the HTML files are UTF-16 encoded (ie, each character takes two bytes), findstr will not find the text.

I would also suggest running the command without the piping to the results.txt first to get the right findstr options and make sure it outputs what you need.

for %%f in (*.html) do findstr /i /m /p /c:"SOME TEXT" "%%f" >> results.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM