Regex – Escaping a backslash in Batch File using FINDSTR

batch-filefindstrpre-commit-hookregex

In my svn Pre commit hooks I use findstr to block certain file types beign committed.
I now want to extend this to directories, in the first instance \obj\ directories however I am having problems with the Regular expression and escaping the \ of the dir

Currently I have

"C:\Program Files\VisualSVN Server\bin\svnlook.exe" changed -t %2 %1 | FindStr /R ".obj\\\"
IF %ERRORLEVEL% EQU 1 GOTO OK
echo "obj directories cannot be committed" >&2
exit 1
:OK
exit 0

i have tried with just \ at the end but that seems to escape the double quotes as well?

Any Ideas?

Best Solution

Empirically, either of the following commands does what you want:

... | findstr /R \.obj\\

... | findstr /R "\.obj\\\\"

Since you specified /R, you also need a backslash before the . because it will otherwise be interpreted as a wildcard character.

Side note: it appears from my tests that findstr.exe uses the somewhat strange quoting rules used by MS's C library, described on Microsoft's website. In this particular case the relevant rule is the one mentioning that a double-quote character preceded by an even number of backslashes is interpreted to be half as many backslashes. (Yes, it's weird, and it gets weirder when you realise that cmd.exe treats double-quote characters specially too... Quoting things properly on Windows is a world of pain, frankly.)