Dean
Dean

Reputation: 5946

Escaping a backslash in Batch File using FINDSTR

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?

Upvotes: 3

Views: 13368

Answers (6)

BSalita
BSalita

Reputation: 8961

In the case of findstr consisting of the two character sequence \" the search string must be \\\\" (four backslashes).

Upvotes: 0

bobbymcr
bobbymcr

Reputation: 24167

Do you actually need a regular expression in this case? If you're just searching for the substring "\obj\" you can use /C instead of /R to treat the text as a literal match string:

{command} | findstr /C:\obj\

Upvotes: 0

Dean
Dean

Reputation: 5946

I solved this using the following.

:CHECKOBJDIRWITHFILES
"C:\Program Files\VisualSVN Server\bin\svnlook.exe" changed -t %2 %1  > "C:\Repositories\SoftwareRepository\hooks\out.txt"
"C:\Program Files\VisualSVN Server\bin\svnlook.exe" changed -t %2 %1 | FindStr /R ./obj/.
echo %ERRORLEVEL% > "C:\Repositories\SoftwareRepository\hooks\error.txt"
IF %ERRORLEVEL% EQU 1 GOTO CHECKOBJDIRWITHOUTFILES
echo "obj directories and their files cannot be committed" >&2
exit 1
:CHECKOBJDIRWITHOUTFILES
"C:\Program Files\VisualSVN Server\bin\svnlook.exe" changed -t %2 %1  > "C:\Repositories\SoftwareRepository\hooks\out.txt"
"C:\Program Files\VisualSVN Server\bin\svnlook.exe" changed -t %2 %1 | FindStr /R ./obj
echo %ERRORLEVEL% > "C:\Repositories\SoftwareRepository\hooks\error.txt"
IF %ERRORLEVEL% EQU 1 GOTO OK
echo "obj directories cannot be committed" >&2
exit 1
:OK
echo %ERRORLEVEL%  >&2
exit 0

Upvotes: 1

Patrick Cuff
Patrick Cuff

Reputation: 29806

What's the error you're getting?

This may be a red herring, but SVN uses / as the path seperator, which causes some problems under Windows. I had to put the following in all my hook scripts to change / to \:

SET REPOS=%1

:: Transform forward-slashes to back-slashes for Windows
SET REPOS=%REPOS:/=^\%

Upvotes: 1

VonC
VonC

Reputation: 1326982

In a regexp, backslash should be double escaped to be correctly interpreted in a String regex:

FindStr /R "\\.obj\\\\"

But in your case, since your regex should match both .obj files and "obj" directories, I would suggest:

FindStr /R "\\.?obj\\\\?"

because your orignal regex (".obj\\") would only have detected ".obj" directory, not "obj". Hence the '?'

Since '.' means any character, you also need "\\" before it to change its interpretation.

Upvotes: 1

j_random_hacker
j_random_hacker

Reputation: 51246

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.)

Upvotes: 3

Related Questions