为什么 FINDSTR 在具有 FOR 循环的多个文件夹上执行时匹配并且不输出任何内容?

Why does FINDSTR match and output nothing on being executed on several folders with a FOR loop?

我正在编写一个批处理脚本来迭代 findstr 不同目录中的文件,但我什么也没得到。我确定该字符串存在于所有文件中。

这是我的脚本:

setlocal

rem Iterate each folder in the current directory.
for /d %%A in (*) do (

    rem Check if orca_input subfolder exist.
    if exist "\orca_input\%%~A" (

        rem Check if dft_opt.log files exist.
        if exist "\orca_input\%%~A\dft_opt.log" (

            rem Change working directory to where the files are temporarily.
            pushd "\orca_input\%%~A" && (

                rem Run the program.
                findstr /C:"Last Energy change" dft_opt.log > energychange.dat
                popd
            )
        )
    )
)

我看不到错误。

编辑:

我按照建议尝试了这个脚本,但是没有创建文件.dat。

setlocal

rem Iterate each folder in the current directory.
for /d %%A in (*) do (

    rem Check if dft_opt.log files exist.
    if exist "\orca_input\%%~A\dft_opt.log" (         

        rem Run the program.
        findstr /C:"Last Energy change" "\orca_input\%%~A\dft_opt.log" >"\orca_input\%%~A\energychange.dat"
    )
)

让我们试试这个:

setlocal

rem Iterate each folder in the current directory.
for /d %%A in (*) do (
   rem Check if dft_opt.log file exists.
   if exist "orca_input\%%~A\dft_opt.log" (
      findstr /C:"Last Energy change" "orca_input\%%~A\dft_opt.log">"orca_input\%%~A\energychange.dat"
   )
)

我觉得我们需要搞清楚文件夹结构。

另一种选择:

setlocal

rem Iterate each folder in the current directory.
for /d %%A in (*) do (
   rem Check if dft_opt.log file exists.
   if exist "%%~A\orca_input\dft_opt.log" (
      findstr /C:"Last Energy change" "%%~A\orca_input\dft_opt.log">"%%~A\orca_input\energychange.dat"
   )
)

第三次尝试:

setlocal

rem Iterate each folder in the current directory.
for /d %%A in (*) do (
   rem Check if dft_opt.log file exists.
   if exist "%%~A\dft_opt.log" (
      findstr /C:"Last Energy change" "%%~A\dft_opt.log">"%%~A\energychange.dat"
   )
)