temp.txt 得到跟随输出 index.html: FINDSTR: Cannot ope-6ren">
gpt4 book ai didi

batch-file - findstr 输出到文件中的问题

转载 作者:行者123 更新时间:2023-12-02 17:03:36 24 4
gpt4 key购买 nike

我正在尝试执行命令

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

它没有将输出保存到 temp.txt其他命令,如

dir * >list.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" 
^.............^ ^..........^

关于batch-file - findstr 输出到文件中的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52828691/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com