Search code examples
regexbatch-filepre-commit-hookfindstr

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?


Solution

  • 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