Escaping a backslash in Batch File using FINDSTR

12,400

Solution 1

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

Solution 2

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

Solution 3

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:/=^\%

Solution 4

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.

Share:
12,400
1977
Author by

1977

None

Updated on June 09, 2022

Comments

  • 1977
    1977 almost 2 years

    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?

  • Ankit Roy
    Ankit Roy about 15 years
    Due to weird quoting rules, your examples won't work -- you need to double the number of backslashes before any double-quote character. Also, you should put a backslash before the "." to indicate it should be treated literally, rather than as a wildcard.
  • Ankit Roy
    Ankit Roy about 15 years
    You didn't test this did you. The backslash before the "." must be a single backslash, and the 4 backslashes at the end become 2 if you add a "?" before the final double-quote. The quoting rules are pretty strange I grant you.
  • bahrep
    bahrep over 11 years
    Why not use %VISUALSVN_SERVER% environment variable?