How do I check if some string (line from a text file for the record) starts with a specific word in batch?
I know how to check if the word (sub-string) exists in the sentence/line (string),
But how can I Check Weather it STARTS with this word?
Thanks 🙂
This can be done with
FINDSTRlike this:The
^at the beginning of a search expression specifies that you are looking for the substring specifically at the beginning of a line.Note that by default
FINDSTRperforms a case-sensitive search. To search regardless of letter case, specify the/Iswitch:The source to search can be specified:
as a parameter:
using
stdinredirection:using piping:
(in this case, the source is the output of the
command)If you want to search multiple files, particularly if they are in the same directory or directory tree, it is probably easiest to use the first pattern of the above three, because the
filenamein this case can be a mask:The above will search files in the specified directory only. To search the entire tree, i.e. including all the subdirectories, add the
/Sswitch:Other useful options can be found in the built-in help (run
FINDSTR /?).