I have 50 files named like: CPUNAME_installer.txt
and the content of each is something like:
CPUNAME;11724;sdaasdasdasdasdasdasd
CPUNAME;1034;231asddasd
CPUNAME;1035;231asddasd
CPUNAME;10741;231asddasd
CPUNAME;10111;231asddasd
...and so on, for hundreds of lines. I want to check if there are lines with the code 11724, 1034 or 1487 and if true, pipe the entire line to another textfile with the same name in another folder.
For now I have this working solution:
for %%f in (*_installer.txt) do (
type %%f | findstr /I /C:"11724">>outfolder\%%f
type %%f | findstr /I /C:"1034">>outfolder\%%f
type %%f | findstr /I /C:"1487">>outfolder\%%f
)
...but it takes a lot of time since it's doing six commands for each file.
Is it possible to improve this by reducing the number of commands for each file (or similar)?
Read the FINDSTR documentation, (Type HELP FINDSTR
from a command window) - You will see that FINDSTR can search for multiple strings with one pass.
It is good you are using the /I option with multiple search strings because of this bug: Why doesn't this FINDSTR example with multiple literal search strings find a match?.
Also, no need to TYPE the content and pipe to FINDSTR. You can simply pass the file name as an argument.
In your case, since your strings do not contain spaces, your code could look the following: the search strings are delimited by spaces.
for %%f in (*_installer.txt) do findstr /i "11724 1034 1487" "%%f" >"outfolder\%%f"
Be careful with the above. If you change the search strings you may need to add the /L
option to force the strings to be treated as string literals instead of regular expressions.
The other way to specify multiple searches is to use multiple /C
options. These search strings are always treated as literals unless the /R
option is used.
for %%f in (*_installer.txt) do findstr /i /c:"11724" /c:"1034" /c:"1487" "%%f" >"outfolder\%%f"
You might also want to investigate the /G:file
option. It allows you to put your search strings in a separate text file, one per line. You can conveniently search for many strings using this technique.
P.S.
There are many undocumented gotchas when using FINDSTR. You might be interested in reading What are the undocumented features and limitations of the Windows FINDSTR command?