Stephen Kairys
asked on
Finding text files with matching string including wildcards (Win10)
Win10:
I have a directory with a bunch of HTML files. I need to identify any file with next matching a specific token.
I can use the legacy FIND command from the command prompt e.g.
FIND "My Target String" *.HTML
but I need also to find "My+Target+String", "My-Target-String" etc.
Is there a variant of the FIND command that will support wildcards e.g.
FIND "My?Target?String" *.HTML
I'll gladly accept either a command line (maye PowerShell?) or Windows (GUI) solution.
Thanks,
Steve
I have a directory with a bunch of HTML files. I need to identify any file with next matching a specific token.
I can use the legacy FIND command from the command prompt e.g.
FIND "My Target String" *.HTML
but I need also to find "My+Target+String", "My-Target-String" etc.
Is there a variant of the FIND command that will support wildcards e.g.
FIND "My?Target?String" *.HTML
I'll gladly accept either a command line (maye PowerShell?) or Windows (GUI) solution.
Thanks,
Steve
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
OBDA - Trying your solution now on a test fileset. Thanks.
ASKER
findstr is a DOS command, not powershell. If you want to use powershell you should use gci (get-childitem) like I suggested.
Since you were using find, the commands I posted assume the Command prompt as well, not Powershell.
ASKER
All,
Going for FINDSTR, seems simpler. Thanks for the heads-up that FINDSTR is DOS not PowerShell. :)
Going for FINDSTR, seems simpler. Thanks for the heads-up that FINDSTR is DOS not PowerShell. :)
For Powershell, the equivalent would be
Select-String -Pattern "My.Target.String" -List -Path *.html
ASKER
@OBDA- Your first (simpler) solution
T:\>
T:\>do @(findstr.exe /i /r /c:"Ad.Tag.Setup" "%a" >NUL&if not errorlevel 1 echo %a)
'do' is not recognized as an internal or external command,
operable program or batch file.
Also, just curious. I understand that the period (.) means wildcard. If I were truly seaching on a string that includes a period and don't care about wildcards, i guess I'd remove the /r switch?
Thanks again. I think we're almost there!
findstr.exe /i /r /c:"My.Target.String" *.html
worked nicely. However, b/c of multiple string occurreences I figured I'd try the FOR loop. However....it threw an error.T:\>
T:\>do @(findstr.exe /i /r /c:"Ad.Tag.Setup" "%a" >NUL&if not errorlevel 1 echo %a)
'do' is not recognized as an internal or external command,
operable program or batch file.
Also, just curious. I understand that the period (.) means wildcard. If I were truly seaching on a string that includes a period and don't care about wildcards, i guess I'd remove the /r switch?
Thanks again. I think we're almost there!
You left out the "for %a in (*.html) " at the beginning of the command.
This would expect the target string to end with a period:
for %a in (*.html) do @(findstr.exe /i /r /c:"My.Target.String" "%a" >NUL&if not errorlevel 1 echo %a)
To specify a literal "." in a RegEx, you escape it with a backslash.This would expect the target string to end with a period:
findstr.exe /i /r /c:"My.Target.String\." *.html
If you need to search for multiple strings you can place a list of the desired strings or patterns into a text file and then use FINDSTR with the /g: option telling it to get the search strings from that file rather than the command line. If it just a couple though you can also do that with multiple /c: parms on a single command line.
~bp
~bp
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@ODBA - Thank you for persisting to solve this issue. (And I love how "old" technology can still be our friend! :)
@James and @Bill - thanks for your comments. I wasn't able to try out your solutions; however, I appreciate your efforts to help. (If you disagree that I'm not assigning you credit or points, please feel free to object.)
Steve
@James and @Bill - thanks for your comments. I wasn't able to try out your solutions; however, I appreciate your efforts to help. (If you disagree that I'm not assigning you credit or points, please feel free to object.)
Steve
ASKER
@OBDA - OK, I'm playing around with this solution and I have two follow-ups.
1. Is there an option to tell me how many instances are in each matched file?
2. For your FOR loop solution, I tried it in a batch file; however, it threw the following message:
Thanks again.
1. Is there an option to tell me how many instances are in each matched file?
2. For your FOR loop solution, I tried it in a batch file; however, it threw the following message:
H:\>find-ats-for.bat
"My.Target.String" was unexpected at this time.
Thanks again.
This will give you the number of lines, not the number instances.
@echo off
setlocal enabledelayedexpansion
set Search=D:\Temp\*.html
for %%a in ("%Search%") do (
set /a Count = 0
for /f %%a in ('findstr.exe /i /r /c:"My.Target.String" "%%~a"') do set /a Count += 1
if !Count! gtr 0 echo %%~nxa: !Count!
)
The other error is because in a script, you need double percent signs for the loop variables.for %%a in (*.html) do @(findstr.exe /i /r /c:"My.Target.String" "%%a" >NUL&if not errorlevel 1 echo %%a)
ASKER
@oBdA - Thank you. I need the # of instances, but that's OK. I can always look through the HTML page manually....none of them are that big. FIgured it was worth a shot.
https://www.safaribooksonline.com/library/view/windows-powershell-cookbook/9780596528492/ch17s06.html