【问题标题】:how to check if the variable contains special characters in batch file如何检查变量是否包含批处理文件中的特殊字符
【发布时间】:2015-11-09 08:58:44
【问题描述】:

如何检查批处理文件中的变量是否包含特殊字符? 例如,如果我的变量包含文件名,则文件名不应包含 \/ < > | : * " ? 这些字符。

@echo off
set param="XXX"
echo %param%| findstr /r "^[^\\/?%*:|"<>\.]*$">nul

if %errorlevel% equ 0 goto :next
echo "Invalid Attribute"

:next
echo correct

我用这个链接作为参考regex for validating folder name & file name

【问题讨论】:

  • 到目前为止你尝试过什么?请通过编辑您的帖子分享您的尝试...
  • 我不需要转义或替换,我只需要找到变量是否有字符,如果变量有字符,我需要从批处理文件中退出
  • 您肯定需要转义,因为您必须在脚本中逐字说明字符;其余的有两个可能的起点: 1. 字符串替换,如if "%VAR%"=="%VAR:/=%",因此如果VAR 不包含/,则条件为真; 2. 像for /F "tokens=2 delims=\/: %C in ("%VAR%#") 这样的for /F 循环,仅当至少出现任何给定字符\/: 时才执行循环;

标签: windows batch-file


【解决方案1】:

你不能使用echo %param%| findstr /r ... 甚至echo !param!| findstr /r ...。有关如何解析和处理管道的更多信息,请查看以下问题和答案:Why does delayed expansion fail when inside a piped block of code

使用辅助(临时)文件如下:

@ECHO OFF
SETLOCAL EnableExtensions EnableDelayedExpansion
:testloop
  set "param="
  set /P "param=string to test (hit <Enter> to end)> "
  if not defined param goto :endtest
  >"%temp%\33605517.txt" echo(!param!

  rem next line for debugging purposes
  for /F "usebackq delims=" %%G in ("%temp%\33605517.txt") do set "_marap=%%G"

  findstr /r ".*[<>:\"\"/\\|?*%%].*" "%temp%\33605517.txt" >nul 

  if %errorlevel% equ 0 (
    echo !errorlevel! Invalid !param! [!_marap!]
  ) else (
    echo !errorlevel! correct !param! [!_marap!]
  )
  goto :testloop
:endtest
ENDLOCAL
goto :eof

.*[&lt;&gt;:\"\"/\\|?*%%].*正则表达式解释

  • .* - 字符串前面的任何字符出现零次或多次;
  • [&lt;&gt;:\"\"/\\|?*%%] 保留字符集中的任意一个字符:
    • &lt;(小于);
    • &gt;(大于);
    • :(冒号);
    • "(双引号)转义为 \"\"(findstr 和批处理解析器);
    • /(正斜杠);
    • \(反斜杠)转义为\\(findstr);
    • |(竖条或竖管);
    • ?(问号);
    • *(星号);
    • %(百分号)转义为%%(对于批处理解析器);事实上,% is not reserved for NTFS file system 但需要在纯 cmd 和/或批处理脚本中进行特殊转义;
  • .* - 字符串末尾的任何字符出现零次或多次。

这里有更多关于 SO 的问题的链接,其中包含 Aacini、DBenham、Jeb(以及其他人)的详尽答案。令人兴奋,引人入胜的阅读......

以及由 David Deley 提供的详细信息 How Command Line Parameters Are Parsed,© 2009(2014 年更新)

输出

string to test (hit <Enter> to end)> n<m
0 Invalid n<m [n<m]
string to test (hit <Enter> to end)> n>m
0 Invalid n>m [n>m]
string to test (hit <Enter> to end)> n:m
0 Invalid n:m [n:m]
string to test (hit <Enter> to end)> n/m
0 Invalid n/m [n/m]
string to test (hit <Enter> to end)> n\m
0 Invalid n\m [n\m]
string to test (hit <Enter> to end)> n|m
0 Invalid n|m [n|m]
string to test (hit <Enter> to end)> n?m
0 Invalid n?m [n?m]
string to test (hit <Enter> to end)> n*m
0 Invalid n*m [n*m]
string to test (hit <Enter> to end)> n"m
0 Invalid n"m [n"m]
string to test (hit <Enter> to end)> nm
1 correct nm [nm]
string to test (hit <Enter> to end)> nm.gat
1 correct nm.gat [nm.gat]
string to test (hit <Enter> to end)> n%m.gat
0 Invalid n%m.gat [n%m.gat]
string to test (hit <Enter> to end)>

【讨论】:

  • 非常感谢,您的回答完美而清晰,对我有用。
  • 很好的解决方案和解释。对您的链接How Command Line Parameters Are Parsed 的小评论。关于如何转义命令行参数(对于windows)的描述是错误的,通常它会起作用,但使用三个插入符号仍然不正确。
【解决方案2】:

您可以将 findstr 行更改为

set "param=Test < string"

cmd /v:on /c echo(^^!param^^! | findstr /r "^[^\\/?%%*:|<>\.\"]*$^" > nul

if %errorlevel% equ 0 ( 
echo OK
) ELSE (
  echo Invalid, special character found
)

这行得通,因为参数将在子 cmd.exe 进程中扩展,扩展延迟。
是否启用延迟扩展并不重要,该线路始终有效。
而且我更改了正则表达式,因为搜索报价本身有点讨厌。

【讨论】:

  • 我看不出有什么问题,用set "param=123abcd&lt;"测试过
  • 请使用%(百分号)加倍。否则,a*b 结果为 OK。
  • @jeb 抱歉,这是我的错误,非常感谢,一切正常
猜你喜欢
  • 1970-01-01
  • 2010-12-20
  • 2018-05-05
  • 2016-08-24
  • 1970-01-01
  • 2014-11-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多