Make FINDSTR stop searching after a match is found - windows

Is there a way to make the FINDSTR command in Windows Command Prompt/Batch files stop searching after it finds a match?
I have a large document repository where I am searching using:
SET /P SEARCHTERM=Enter ID number _
FINDSTR /s /m /f:"%~dp0_Reports\MasterFilelist.txt" /c:%SEARCHTERM%
to search the documents for a specific UNIQUE string that will only appear in ONE of the documents. For the sake of saving time, I want FINDSTR to stop searching once it finds a match. Is this possible?

Related

How to use findstr to search multiple files

When I use findstr on a single file, like this:
findstr "import" filename.py
it returns a number of matches. But when I extend it to cover the current folder and sub-folders:
findstr /S "import" .
it doesn't find any matches. What am I doing wrong?
OK i've figured it out. The /S option indicates searching current directory and all sub-directories, but I still need to specify a filename pattern.
This indicates all files:
findstr /S "import" *

Can I search for multiple strings in one "find" command in batch script?

I have a windows batch script that will look for a string within a file
find /i "WD6" %Inputpath%file.txt
if %errorlevel% == 0 GOTO somestuff
Currently this is what my code looks like. I've come across a new string I want to search for in the same file and do the same action if it finds it, it stored it in a variable called %acctg_cyc% can I search for both strings in one line of code? I tried this:
find /i "WD6" %acctg_cyc% %Inputpath%file.txt
if %errorlevel% == 0 GOTO somestuff
But it seems to ignore the %acctg_cyc% and only look for "WD6" in file.txt. I tried testing where %acctg_cyc% is in file.txt and when it is not and it passes both times.
Any thoughts? I know I could do this in more lines of code but I'm really trying to avoid that right now. Maybe it's just not possible.
Thank you for any help!
find isn't very powerful. It searches for one string only (even if it is two words): find "my string" file.txt looks for the string my string.
findstr has much more power, but you have to be careful how to use it:
findstr "hello world" file.txt
finds any line, that contains either hello or world or both of them.
see findstr /? for more info.
Finding both words in one line is possible with (find or findstr):
find "word1" file.txt|find "word2"
finding both words scattered over the file (find or findstr):
find "word1" file.txt && find "word2" file.txt
if %errorlevel%==0 echo file contains both words
I tried findstr with multiple /C: arguments (one for each to be searched sentence) which did the trick in my case.
So this is my solution for finding multiple sentences in one file and redirect the output:
findstr /C:"the first search" /C:" a second search " /C:"and another" sourcefile.txt > results.txt
I used this. Maybe not much orthodox, but works!
It waits until browsers dismiss
:do_while_loop
rem ECHO LOOP %result%
rem pause
tasklist /NH | find "iexplore.exe"
set result=%ERRORLEVEL%
tasklist /NH | find "firefox.exe"
set result=%result%%ERRORLEVEL%
if not "%result%"=="11" goto :do_while_loop

Windows command line to findstr 1 level of subdirectory down

Looking for the command line (or batch) that will do the following,
I have 3 levels of directories and need to search for a specific string contained in text files, searching all files, 1 level subdirectory down.
So I know this will recursively search for the string
findstr /S "STRING" *
But I don't want the search to go father than only 1 subdirectory level down.
Seems like a simple thing but I could not find anything solid. The closest thing I could find was something like this thread..
windows batch to recursively scan files in 1 level subfolders
However, before going batch is there a simpler (command line) way to this rather, apparently simple task?
Thanks for your time!
This should do it from the command line and write file.log into the current folder
for /d %a in (*) do findstr "STRING" "%a\*">>file.log
Child Directory Search Only (No Parents or Grandchildren)
Here is a one line command to enumerate the current working directory children directories dir /ad/b and search each directory for files containing the search string. Results are saved out to a text file.
for /f "delims=" %A in ('dir /ad/b') do #findstr "STRING" "%~A\*" >> results.txt
Note: That as currently configured, a result line will be output for each matching line in a file. Meaning that multiple find results may be output for a single file.
Example of results.txt
ChildFolderA\File1.txt:line with STRING
ChildFolderA\File1.txt:another line with STRING in file1
ChildFolderA\File2.txt:another file and line with STRING
ChildFolderB\File5.txt:another folder with a file containing the search STRING
#echo off
setlocal enableextensions
(#for /d %%a in (*) do #for %%b in ("%%~fa\*") do #echo(%%~fb)|findstr /f:/ /c:"string" > file.txt
This will enumerate all the files one level down and send this list of files to a findstr command. findstr will search the string into the indicated list and send output to final file
To use from command line, replace the double percent signs %% with single ones %

how to enter multiple variables into a single batch command

I have a lot of variables to place in this certain command, is there a way to add many variables in it without rewriting the command?
dir c:\ /s /b /a | find "my file"
for example i want to search for "my file" and 50 other things.
thanks for the answers
In short: no. You have to rewrite the command, unless the specific command actually does accept multiple parameters (which may be variables). In your case it doesn't so you need to rewrite it.
One option would be to use findstr instead of find. You can pass multiple search patterns:
dir c:\ /s /b /a | findstr /c:"my file" /c:"other" /c:"other 2" ...
I don't know how well that scales to about "50 other things" though, but then no such solution may. Maybe you can condense the filenames to a view using regular expressions (check findstr /?).
You could also simply do:
for /R c:\ %i in ("my file" "other" "other2") do #echo %i
Both solutions bear the option for duplicates, however. They essentially search based on a "contains" semantic. So, both "C:\foo\my file" and "C:\foo\bar\my file\something.txt" would match. But then your original solution had that issue as well.
If you can make your search patterns unique or can live with false positives, than that shouldn't be an issue. But be aware of it nevertheless.
Extending Christian's suggestion to use FINDSTR instead of FIND - You can simply put all 50 search terms in a text file and reference them using the /G:"filename" option.
But there is one important caveat - There is a nasty FINDSTR bug when searching for multiple literal strings. See Why doesn't this FINDSTR example with multiple literal search strings find a match?.
As explained in the link, the work-around is to either do a case insensitive search using the /I option, or else use regular expression search terms with the /R option.
For a "complete" listing of undocumented FINDSTR features and bugs, see What are the undocumented features and limitations of the Windows FINDSTR command?.
You could wrap it in a for loop:
for %i in ("my file" "second file") do dir c:\ /s /b /a | find %i

Windows CMD: List files in dir & subdir WITHOUT given extensions

I'd like to recursively search a directory and find files, which have NOT a certain extension, or precisely, which have NOT a certain set of extensions.
Sketch: find in "dir" all files without "ext1", "ext2", "ext3" and print results to .txt
I tried around several hours with DIR and ATTRIB, but unfortunately without bigger success.
Your consideration is highly regarded! Thanks.
Try this:
dir /b /s /a-d | findstr /vi ".ext1$ .ext2$ .ext3$"
The /a-d switch excludes directories, giving you only files. The findstr parameter lets you search the files for strings, and the /vi switch indicates to exclude files containing the next parameter, the search being case insensitive.
As Joey pointed out, the $ is necessary to indicate end of the line.

Resources