How to get the exact match of a string in file using FIND or FINDSTR in dos
The /x is not going to work as the entire line needs to match the search string, and in this case, it is prefixed with path =
. Also, the \
symbols need to be escaped so they are not interpreted as commands. Finally, regular expression mode will help capture the right line, as it ends in a space then the end of line - the end of line can be represented as a $
:
H:\>findstr /r /c:"\\Users\\sachin.a.goyal\\Desktop\\test\\ *$" biggest.txt
path = \Users\sachin.a.goyal\Desktop\test\
The reason for your search hanging is probably one of the \
was matching a command.
If you are using a variable to hold the path being searched for, you would still need to escape the backslashes. You can do this with the replace feature:
set path="c:\Users\sachin.a.goyal\Desktop\test\"
set path=%path:\=\\%
echo path
c:\\Users\\sachin.a.goyal\\Desktop\\test\\
The second line says, replace all backslahes with double backslashes. Unfortunately I don't have a Windows box to test on right now, but you may need to escape the backslashes still:
set path=%path:\\=\\\\%
Sachin
Updated on September 18, 2022Comments
-
Sachin over 1 year
I am trying to get the exact match of a path ("\Users\sachin.a.goyal\Desktop\test\") which is there in a file:
Biggest.txt: 1 number: name = DMS 3.0 R2 - Deployment Workbook_APAC v4_WMT size = 15634835 path = \Users\sachin.a.goyal\Desktop\test\ 2 number: name = Scenarios size = 254 path = \Users\sachin.a.goyal\Desktop\test\New folder\
I am using this command:
findstr /x /c:"\Users\sachin.a.goyal\Desktop\test\" Biggest.txt
But after this command, DOS window is stucked. It is not giving any error but looks like it is expecting some other parameter. Please help.
-
Daniel Andersson almost 12 yearsWhich operating system? It could be MS-DOS, but it is most likely just the command prompt in Windows [something]. I'm tagging the post "Windows" for the time being. If it explicitly is MS-DOS, feel free to change it.
-
-
Sachin almost 12 yearsThank you very much!!It worked. But The actual problem is that I have stored the path in a variable and I am matching the value of that variable in file. Then I have to save the path in the variable as you told with regular expression '\' ?
-
Paul almost 12 yearsYeah that should be fine - are you still getting stuck with the backslashes?
-
Sachin almost 12 yearsYes....that variable contains the path with single backslashes only
-
Paul over 11 yearsCan you test it by populating the var with double backslashes and if it works we'll figure a way to fix it
-
Sachin over 11 yearsYes it works when populate the var like that
code
set var = \\Users\\sachin.a.goyal\\Desktop\\test\\ *$ -
Sachin over 11 yearsNow can u help me please how will I change the path stored in the variable as regular expression
-
Paul over 11 yearsUpdated answer with variable escaping