Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Finding text files with matching string including wildcards (Win10)

Posted on 2016-11-15
16
Medium Priority
?
89 Views
Last Modified: 2016-11-15
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
0
Comment
Question by:Stephen Kairys
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 2
  • +1
16 Comments
 
LVL 9

Expert Comment

by:James Bilous
ID: 41888215
1
 
LVL 85

Assisted Solution

by:oBdA
oBdA earned 2000 total points
ID: 41888222
findstr.exe supports something vaguely resembling regular expressions; the "." stands for any single character:
findstr.exe /i /r /c:"My.Target.String" *.html

Open in new window

If the target string shows up repeatedly in a single file, and you only want the file names, paste this into a command prompt:
for %a in (*.html) do @(findstr.exe /i /r /c:"My.Target.String" "%a" >NUL&if not errorlevel 1 echo %a)

Open in new window

0
 
LVL 4

Author Comment

by:Stephen Kairys
ID: 41888226
OBDA - Trying your solution now on a test fileset. Thanks.
0
Create the perfect environment for any meeting

You might have a modern environment with all sorts of high-tech equipment, but what makes it worthwhile is how you seamlessly bring together the presentation with audio, video and lighting. The ATEN Control System provides integrated control and system automation.

 
LVL 4

Author Comment

by:Stephen Kairys
ID: 41888232
Follow-up:
I can't seem to get HELP for FINDSTR from the PowerShell prompt per the below.

PowerShell HELP problem
Accordingly, can I assume the /i option means "ignore case"?

Thanks again.
0
 
LVL 9

Expert Comment

by:James Bilous
ID: 41888237
findstr is a DOS command, not powershell. If you want to use powershell you should use gci (get-childitem) like I suggested.
1
 
LVL 85

Expert Comment

by:oBdA
ID: 41888238
Since you were using find, the commands I posted assume the Command prompt as well, not Powershell.
0
 
LVL 4

Author Comment

by:Stephen Kairys
ID: 41888240
All,
Going for FINDSTR, seems simpler. Thanks for the heads-up that FINDSTR is DOS not PowerShell. :)
0
 
LVL 85

Expert Comment

by:oBdA
ID: 41888253
For Powershell, the equivalent would be
Select-String -Pattern "My.Target.String" -List -Path *.html

Open in new window

0
 
LVL 4

Author Comment

by:Stephen Kairys
ID: 41888280
@OBDA- Your first (simpler) solution

findstr.exe /i /r /c:"My.Target.String" *.html

Open in new window

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!
0
 
LVL 85

Expert Comment

by:oBdA
ID: 41888292
You left out the "for %a in (*.html) " at the beginning of the command.
for %a in (*.html) do @(findstr.exe /i /r /c:"My.Target.String" "%a" >NUL&if not errorlevel 1 echo %a)

Open in new window

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

Open in new window

0
 
LVL 57

Expert Comment

by:Bill Prew
ID: 41888294
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
1
 
LVL 85

Accepted Solution

by:
oBdA earned 2000 total points
ID: 41888303
Forget about the "for" loop. findstr.exe can do the file list all by its lonesome, with /m (not using findstr too often, because of the very limited "regular expression" support):
findstr.exe /m /i /r /c:"My.Target.String" *.txt

Open in new window

0
 
LVL 4

Author Closing Comment

by:Stephen Kairys
ID: 41888334
@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
1
 
LVL 4

Author Comment

by:Stephen Kairys
ID: 41888814
@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:
H:\>find-ats-for.bat
"My.Target.String" was unexpected at this time.

Open in new window


Thanks again.
0
 
LVL 85

Expert Comment

by:oBdA
ID: 41888856
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!
)

Open in new window

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)

Open in new window

0
 
LVL 4

Author Comment

by:Stephen Kairys
ID: 41888864
@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.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
In this modest contribution, I want to share with the IT community (especially system administrators, IT Support Engineers and IT Help Desks) about Windows crashes/hangs and how to deal with these particular problems.
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question