Batch file findstr输出到文件中的问题

Batch file findstr输出到文件中的问题,batch-file,findstr,Batch File,Findstr,我正试着听从命令 findstr /RC:"h25v06.*hdf\"" "index.html" >temp.txt 得到 跟踪输出 index.html:<img src="/icons/unknown.gif" alt="[ ]"> <a href="MOD13Q1.A2018257.h25v06.006.2018282132046.hdf"> FINDSTR: Cannot open >temp.txt 工作正常您发现了一个问题,原因是cm

我正试着听从命令

findstr /RC:"h25v06.*hdf\"" "index.html" >temp.txt
得到 跟踪输出

index.html:<img src="/icons/unknown.gif" alt="[   ]"> <a 
 href="MOD13Q1.A2018257.h25v06.006.2018282132046.hdf">
FINDSTR: Cannot open >temp.txt

工作正常

您发现了一个问题,原因是
cmd
解析器和可执行程序参数解析器之间的引号处理不同

这似乎是正确的

findstr /RC:"h25v06.*hdf\"" "index.html" >temp.txt
                        ^^                           escaped quote to findstr
            ^.............^ ^..........^             arguments to findstr
                                         ^           redirection operator
您的问题是,当
cmd
尝试解析该行(以创建命令的内部表示并确定是否需要重定向)时,对于
cmd
双引号是“转义”(再次关闭和打开)引号,所看到的引号是

findstr /RC:"h25v06.*hdf\"" "index.html" >temp.txt
                         ^^ escaped quote
            ^ open          ^close     ^open
这意味着一切都被视为
findstr

findstr /RC:"h25v06.*hdf\"" "index.html" >temp.txt
^.....^                                               command
        ^........................................^    argument
转义引号将重定向操作符隐藏到
cmd
,该操作符将所有内容传递到
findstr

findstr
内部,参数处理是不同的,它可以看到

findstr /RC:"h25v06.*hdf\"" "index.html" >temp.txt
            ^.............^ ^..........^ ^.......^    arguments to findstr
这意味着预期的重定向现在被视为要搜索的文件

一个简单的解决方案是简单地更改重定向的位置

>temp.txt findstr /RC:"h25v06.*hdf\"" "index.html" 
但这留下了另一个问题。如果
findstr
处理的文件名包含空格或特殊字符,则该命令将被引用,因为它们超出了引用区域

因此,我们需要一种分离两个引号的方法,在
findstr
表达式中不包含不需要的字符,而是正确地关闭每个引号区域

findstr /RC:"h25v06.*hdf\"^" "index.html" >temp.txt
^“
cmd
视为引用区域外的实际转义引号(由前面的引号关闭),因此
^
将不会传递给
findstr
。现在对于
cmd
而言,引用区域是

findstr /RC:"h25v06.*hdf\"^" "index.html" >temp.txt
            ^............^   ^..........^
有问题的引号是作为另一个字符处理的转义序列,
findstr
接收预期参数

findstr /RC:"h25v06.*hdf\"" "index.html" 
            ^.............^ ^..........^

您发现了一个问题,它是由
cmd
解析器和可执行程序参数解析器之间的引号处理差异引起的

这似乎是正确的

findstr /RC:"h25v06.*hdf\"" "index.html" >temp.txt
                        ^^                           escaped quote to findstr
            ^.............^ ^..........^             arguments to findstr
                                         ^           redirection operator
您的问题是,当
cmd
尝试解析该行(以创建命令的内部表示并确定是否需要重定向)时,对于
cmd
双引号是“转义”(再次关闭和打开)引号,所看到的引号是

findstr /RC:"h25v06.*hdf\"" "index.html" >temp.txt
                         ^^ escaped quote
            ^ open          ^close     ^open
这意味着一切都被视为
findstr

findstr /RC:"h25v06.*hdf\"" "index.html" >temp.txt
^.....^                                               command
        ^........................................^    argument
转义引号将重定向操作符隐藏到
cmd
,该操作符将所有内容传递到
findstr

findstr
内部,参数处理是不同的,它可以看到

findstr /RC:"h25v06.*hdf\"" "index.html" >temp.txt
            ^.............^ ^..........^ ^.......^    arguments to findstr
这意味着预期的重定向现在被视为要搜索的文件

一个简单的解决方案是简单地更改重定向的位置

>temp.txt findstr /RC:"h25v06.*hdf\"" "index.html" 
但是这留下了另一个问题。如果
findstr
处理的文件名包含空格或特殊字符,则该命令将失败,因为它们不在引号内

因此,我们需要一种分离两个引号的方法,在
findstr
表达式中不包含不需要的字符,而是正确地关闭每个引号区域

findstr /RC:"h25v06.*hdf\"^" "index.html" >temp.txt
^“
cmd
视为引用区域外的实际转义引号(由前面的引号关闭),因此
^
将不会传递给
findstr
。现在对于
cmd
引用的区域是

findstr /RC:"h25v06.*hdf\"^" "index.html" >temp.txt
            ^............^   ^..........^
有问题的引号是作为另一个字符处理的转义序列,
findstr
接收预期参数

findstr /RC:"h25v06.*hdf\"" "index.html" 
            ^.............^ ^..........^

bingo>temp.txt findstr/RC:“h25v06.*hdf\”“index.html”起作用了,谢谢,你的解释很详细我觉得findstr/RC:“h25v06.*hdf\”“^”“index.html”>temp.txt是更好的方法bingo>temp.txt findstr/RC:“h25v06.*hdf\”“index.html”起作用了,谢谢,你的解释很详细我觉得findstr/RC:“h25v06.*hdf\“^”index.html“>temp.txt是更好的方法