使用 FINDSTR 在批处理文件中转义反斜杠

发布于 2024-07-14 09:28:51 字数 399 浏览 12 评论 0原文

在我的 svn Pre commit hooks 中,我使用 findstr 来阻止提交某些文件类型。 我现在想将其扩展到目录,首先是 \obj\ 目录,但是我在正则表达式和转义目录的 \ 方面遇到问题

目前我已经

"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

尝试过仅在末尾使用 \ 但似乎逃脱了双引号也是如此?

有任何想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

蹲墙角沉默 2024-07-21 09:28:51

根据经验,以下任一命令都可以满足您的要求:

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

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

由于您指定了 /R,因此您还需要在 . 之前添加反斜杠,否则它会被解释为通配符。

旁注:从我的测试来看,findstr.exe 使用了 MS 的 C 库所使用的有点奇怪的引用规则,如 Microsoft 网站。 在这种特殊情况下,相关规则提到,前面有偶数个反斜杠的双引号字符被解释为反斜杠数的一半。 (是的,这很奇怪,而且当您意识到 cmd.exe 也特别对待双引号字符时,情况会变得更奇怪......坦率地说,在 Windows 上正确引用事物是一个痛苦的世界。)

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

送君千里 2024-07-21 09:28:51

在正则表达式中,反斜杠应该双转义才能在字符串正则表达式中正确解释:

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

但在您的情况下,因为您的正则表达式应该匹配 .obj 文件和“obj”目录,我建议:

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

因为您的原始正则表达式(“.obj\\”)只会检测到“.obj”目录,而不是“obj”。 因此,“?

自“.”起 表示任何字符,您还需要在其前面添加“\\”来更改其解释。

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.

帝王念 2024-07-21 09:28:51

您遇到的错误是什么?

这可能是一个转移注意力的话题,但 SVN 使用 / 作为路径分隔符,这会在 Windows 下导致一些问题。 我必须将以下内容放入所有挂钩脚本中,以将 / 更改为 \

SET REPOS=%1

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

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:/=^\%
耀眼的星火 2024-07-21 09:28:51

我使用以下方法解决了这个问题。

: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

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
寒尘 2024-07-21 09:28:51

在这种情况下你真的需要正则表达式吗? 如果您只是搜索子字符串“\obj\”,您可以使用 /C 而不是 /R 将文本视为文字匹配字符串:

{command} | findstr /C:\obj\

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\
热血少△年 2024-07-21 09:28:51

如果 findstr 由两个字符序列 \" 组成,则搜索字符串必须为 \\\\"(四个反斜杠)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文