mak*_*rni 3 batch-file findstr
我正在尝试执行以下命令
findstr /RC:"h25v06.*hdf\"" "index.html" >temp.txt
Run Code Online (Sandbox Code Playgroud)
跟踪输出
index.html:<img src="/icons/unknown.gif" alt="[ ]"> <a
href="MOD13Q1.A2018257.h25v06.006.2018282132046.hdf">
FINDSTR: Cannot open >temp.txt
Run Code Online (Sandbox Code Playgroud)
它不会将输出保存到 temp.txt 其他命令,例如
dir * >list.txt
Run Code Online (Sandbox Code Playgroud)
工作正常
cmd您发现了一个问题,该问题是由解析器和可执行程序参数解析器之间的引号处理差异引起的。
虽然这看起来是正确的
findstr /RC:"h25v06.*hdf\"" "index.html" >temp.txt
^^ escaped quote to findstr
^.............^ ^..........^ arguments to findstr
^ redirection operator
Run Code Online (Sandbox Code Playgroud)
您的问题是,当cmd尝试解析该行(创建命令的内部表示并确定是否需要重定向)时,对于cmd双引号是“转义”(关闭并再次打开)引号,引号看到的是
findstr /RC:"h25v06.*hdf\"" "index.html" >temp.txt
^^ escaped quote
^ open ^close ^open
Run Code Online (Sandbox Code Playgroud)
这意味着一切都被视为论据findstr
findstr /RC:"h25v06.*hdf\"" "index.html" >temp.txt
^.....^ command
^........................................^ argument
Run Code Online (Sandbox Code Playgroud)
转义引号隐藏了将cmd所有内容传递给 的重定向运算符findstr。
内部findstr参数处理是不同的,它看到
findstr /RC:"h25v06.*hdf\"" "index.html" >temp.txt
^.............^ ^..........^ ^.......^ arguments to findstr
Run Code Online (Sandbox Code Playgroud)
这意味着预期的重定向现在被视为要搜索的文件。
一种简单的解决方案是简单地更改重定向的位置
>temp.txt findstr /RC:"h25v06.*hdf\"" "index.html"
Run Code Online (Sandbox Code Playgroud)
但这留下了另一个问题。由于它被引用,如果正在处理的文件名findstr包含空格或特殊字符,则该命令将失败,因为它们超出了引用区域。
因此,我们需要一种方法来分隔两个引号,不在表达式中包含不需要的字符findstr,但正确关闭每个引号区域
findstr /RC:"h25v06.*hdf\"^" "index.html" >temp.txt
Run Code Online (Sandbox Code Playgroud)
^"被视为cmd引用区域之外的真实转义引用(由前面的引用封闭),因此^不会传递给findstr. 现在cmd引用的区域是
findstr /RC:"h25v06.*hdf\"^" "index.html" >temp.txt
^............^ ^..........^
Run Code Online (Sandbox Code Playgroud)
有问题的引用是一个转义序列,它被作为另一个字符处理并findstr接收预期的参数
findstr /RC:"h25v06.*hdf\"" "index.html"
^.............^ ^..........^
Run Code Online (Sandbox Code Playgroud)