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

Cmd 为什么FINDSTR在几个带有FOR循环的文件夹上执行时不匹配并输出任何内容?,cmd,foreach,findstr,Cmd,Foreach,Findstr,我正在编写一个批处理脚本,在不同目录中的文件上迭代一个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 i

我正在编写一个批处理脚本,在不同目录中的文件上迭代一个
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迭代当前目录中的每个文件夹。
对于(*)中的/d%%A,请执行以下操作(
rem检查dft_opt.log文件是否存在。
如果存在“orca\u输入\%%~A\dft\u opt.log”(
findstr/C:“上次能量变化”“orca\U输入\%%~A\dft\U选项日志”>“orca\U输入\%%~A\energychange.dat”
)
)
我觉得我们需要弄清楚文件夹的结构

另一种选择:

setlocal
rem迭代当前目录中的每个文件夹。
对于(*)中的/d%%A,请执行以下操作(
rem检查dft_opt.log文件是否存在。
如果存在“%%~A\orca\u input\dft\u opt.log”(
findstr/C:“上次能量变化”“%%~A\orca\u input\dft\u opt.log”>“%%~A\orca\u input\energychange.dat”
)
)
第三次尝试:

setlocal
rem迭代当前目录中的每个文件夹。
对于(*)中的/d%%A,请执行以下操作(
rem检查dft_opt.log文件是否存在。
如果存在“%%~A\dft\u opt.log”(
findstr/C:“上次能量变化”“%%~A\dft_opt.log”>“%%~A\energychange.dat”
)
)

“\orca\u输入\%%~A”
。这将在当前驱动器的根目录下查找文件夹
orca\u input
。也许你想让
“orca\u input\%%~A”
检查当前目录中的
orca\u input
?旁注--
if exist”\orca\u input\%%~A“
是不必要的,因为你有
if exist”\orca\u input\%%~A\dft\u opt.log“
旁注2--我个人的偏好是避免
pushd
popd>。相反,你可以
findstr/C:“上次能量变化”\orca\u input\%%~A\dft\u opt.log“>“\orca\u input\%%~A\energychange.dat”
我在“orca\u input”文件夹中有一些文件夹,所以我想我需要进入其中一个文件夹,进行查找,返回“orca\u input”然后迭代到另一个文件夹。您的文件夹结构是什么?您的原始脚本正在查找
c:\orca\u input\eachfoldername\dft\u opt.log
。但是,
orca\u input
不是'c:`的根。因此,文件夹的位置是错误的。你的脚本在错误的地方寻找它想要的东西。这就是为什么什么也没有归还。我们必须知道东西在哪里(你的文件夹结构或目录树),这样我们才能正确地引用它们。现在我们知道
orca\u input
在哪里。给我一个示例目录,说明
dft_opt.log
文件的位置。然后告诉我你运行这个脚本的文件夹。我在上面的一条评论中写下了这个结构。我认为使用(*)可以在当前文件夹中查找,例如“formic”。我从“orca_输入”运行它有两个问题。第一个是使用前导反斜杠\。它转到
c:
的根目录,而不是当前目录。第二个问题是重复“orca_输入”。如果没有前导反斜杠,您仍然会错误地查找
C:\Users\MiniHp\Documents\Universitáa\Orca\u input\Orca\u input\Formic\dft\u opt.log
——查找嵌套在“Orca\u input”文件夹中的“Orca\u input”文件夹。