findstr 输出到文件中的问题

problems in findstr output to file

我正在尝试遵循命令

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

工作正常

You have found one problem caused by the difference in quote handling between cmd parser and executable programs argument parsers.

While this is what seems correct

findstr /RC:"h25v06.*hdf\"" "index.html" >temp.txt
                        ^^                           escaped quote to findstr
            ^.............^ ^..........^             arguments to findstr
                                         ^           redirection operator

your problem is that when cmd tries to parse the line (to create the internal representation of the command and determine if a redirection is needed or not), as for cmd a double quote is a "escaped" (close and open again) quote, the quotes seen are

findstr /RC:"h25v06.*hdf\"" "index.html" >temp.txt
                         ^^ escaped quote
            ^ open          ^close     ^open

This implies everyting is seen as arguments to findstr

findstr /RC:"h25v06.*hdf\"" "index.html" >temp.txt
^.....^                                               command
        ^........................................^    argument

The escaped quote hides the redirection operator to cmd that passes evething to findstr.

Inside findstr the argument handling is different and it sees

findstr /RC:"h25v06.*hdf\"" "index.html" >temp.txt
            ^.............^ ^..........^ ^.......^    arguments to findstr

That means that the intended redirection is now seen as a file where to search.

One simple solution is to simply change the position of the redirection

>temp.txt findstr /RC:"h25v06.*hdf\"" "index.html" 

BUT this leaves another problem. As it is quoted if the file name being processed by findstr includes spaces or special characters the command will fail because they are out of a quoted area.

So, we need a way to separate the two quotes, without including a non desired character in the findstr expression but properly closing each quoted area

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

^" is seen by cmd as a real escaped quote out of a quoted area (closed by the preceding quote) so the ^ will not be passed to findstr. Now for cmd the quoted areas are

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

The problematic quote is a escaped sequence that is handled as another character and findstr receives the intended arguments

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