7/24/2023 0 Comments Cmd find file containing stringSince you want just the file names, we can actually use another GNUWin32 utility called sed to obtain these. Note that there is some additional explanation of these last two GNUWin32 grep specific quirks at end of this answer. It's also worth mentioning that the reason I have placed the "searchwords.txt" file in a separate directory is that GNUWin32 grep will include this file in the "matched" items otherwise.įinally, while it shouldn't be a deal-breaker in most instances, GNUWin32 grep uses Unix-style line feeds for its output, as well as a mix of \ and / for paths. doc1.txt doc2.txt doc3.txt) in place of the directory. If you wish to search only specific files, you can list them on the command line individually (e.g. Any paths with spaces require double quotes ("") as normal. For instance, -L is not the same option as -l (lowercase L). Be aware, however, that unlike FINDSTR, grep options are typically case-sensitive. Suppress normal output (list files only, including full path).īoth FINDSTR and grep return full paths to a file, so you will likely need to do additional processing on the output of either. For your use case, you can try: grep -rilf C:\path\to\searchwords.txt C:\path\to\search > results.txt While grep implementations can vary in the options they support, GNUWin32 grep is a port of GNU grep (albeit currently v2.5.4 rather than v3.0) which can read search terms from a file. Note, however, FINDSTR has undocumented limits and bugs which may not make it a suitable option.įor a non-native solution, you might be interested in a port of the grep utility, such as GNUWin32 grep. Print only file names (which happens to include the full directory path). Use the search words as string literals (rather than regular expressions). For your use case, you could try: findstr /l /s /i /m /g:searchwords.txt /f:filestosearch.txt > results.txt Keep in mind that FIND command is used to search for a single string ONLY.For a native Windows solution, FINDSTR is a possible option. Below command search for the string in all “.txt” files. When you use wildcards, the syntax will be easier. C:\>find "task" tasks.txt notes.txt address.txt For example, below command finds the text in multiple files. You need to pass each file separately or you can use wildcards to select multiple files. C:\>find "task" sample.txt /cįIND command allows to search the given string in multiple files. For example, below command shows the count of lines where the given text “task” is found. It needs “/C” as the parameter to display the count. C:\>find "Steps" sample.txt /nĭisplay count of lines where it finds the textįIND command provides a count value of number of lines where the given text is found in the file(s). Display the line numberīy using the parameter “/N” it will display the line number where it finds the given text and the line of text. C:\>find "codesteps" sample.txt /iīy using “/V” parameter it displays the lines of text where the given string NOT found. Below is the example (it treats both lowercase and uppercase letters are the same). To ignore the case, you need to pass a parameter “/I” to it. Observe that, you see the entire line from the file where it has “hello” text. For example, below command search for the string “ hello” in the file “ sample.txt“. The string should be in double quotes otherwise, you will see above message. FIND: Parameter format not correctīasic parameters you need to pass a string to search and the path of the file. You need to pass relevant arguments to this command otherwise, Windows will display below message. This command is used to search for a string in the files. DIR command doesn’t useful to search the string in the files. We have discussed finding files in the current and specified directories using DIR command from Windows command prompt.
0 Comments
Leave a Reply. |