批处理文件中的Folder1和Folder2日志未正确显示 [英] Folder1 and Folder2 logs not showing correctly in Batch files

查看:134
本文介绍了批处理文件中的Folder1和Folder2日志未正确显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该很简单,但是不能正常工作.

This should be simple but is not working correctly.

FOR /D %%d IN ("D:\Folder*") DO (
    FOR /R "%%d\logs" %%i IN (*) do echo %%i
)
pause

我也尝试过:

FOR /D %%d IN ("D:\Folder*") DO (
    SET fld=%%d
    FOR /R "%fld%\logs" %%i IN (*) do echo %%i
)
pause

我觉得缺少一些基本知识.

I feel that am missing something pretty basic.

谢谢!

推荐答案

第一个代码

FOR /D %%d IN ("D:\Folder*") DO (
    FOR /R "%%d\logs" %%i IN (*) do echo %%i
)

失败,因为您不能在for /r的文件夹中使用for可替换参数.以for解析器的工作方式,在for /d开始执行之前,必须用作/R中使用的起始文件夹的值.

fails because you can not use a for replaceable parameter in the folder of a for /r. In the way the for parser works, the value to use as the starting folder used in the /R must be available before the for /d starts to execute.

第二个代码

FOR /D %%d IN ("D:\Folder*") DO (
    SET fld=%%d
    FOR /R "%fld%\logs" %%i IN (*) do echo %%i
)

失败,因为%fld%不会像您所想的那样被解析.完整的for /d %%d仅被解析为一条命令,并且该命令中的所有%var%引用均被删除,并在开始执行之前用存储在变量中的值替换.您不能在同一命令内更改变量的值并获取更改后的值,因为在执行命令时,没有任何变量扩展包含在命令中,而是仅包含变量在解析命令时所具有的值.

fails because %fld% will not be parsed as you think. The full for /d %%d is parsed as only one command and all the %var% references inside this command are removed, being replaced with the value stored in the variables before starting to execute. You can not change a variable's value and retrieve the changed value inside the same command because while executing there is not any variable expansion included in the command, only the values the variables had when the command was parsed.

这通常是通过启用延迟扩展来处理的,因此您可以将所需的%var%更改为!var!,以向解析器指示变量扩展(值检索)应延迟到执行命令之前.所以你可以写类似

This is usually handled enabling delayed expansion so you can change where needed %var% into !var! to indicate to the parser that the variable expansion (value retrieval) should be delayed until the command is executed. So you could write something like

setlocal enabledelayedexpansion
FOR /D %%d IN ("D:\Folder*") DO (
    SET "fld=%%d"
    FOR /R "!fld!\logs" %%i IN (*) do echo %%i
)

但是,由于第一个代码中指出的相同原因,这也将不起作用.解析时需要在/R中使用的值.

BUT this will also not work, for the same reason indicated in the first code. The value to use in /R is required at parse time.

那么,如何解决问题?

通过首先将当前活动目录更改为for /D %%i迭代的文件夹,可以避免在/R中指示文件夹.

You can avoid having to indicate the folder in the /R by first changing the current active directory to the folder being iterated by for /D %%i

for /d %%d in ("d:\Folder*") do (
    pushd "%%~fd\logs" && (
        for /r %%i in (*) do echo %%i
        popd
    )
)

或者您可以将内部循环放在单独的子例程中

or you can place the inner loop in a separate subroutine

for /d %%d in ("d:\Folder*") do call :process "%%d"
goto :eof


:process folderPath
for /r "%~1\logs" %%i in (*) do echo %%i
goto :eof

或者您可以将内部递归for替换为for /f处理递归dir

or you can replace the inner recursive for with a for /f processing a recursive dir

for /d %%d in ("d:\Folder*") do (
    for /f "delims=" %%i in ('dir "%%~fd\logs" /a-d /b /s`) do echo %%i
)

这篇关于批处理文件中的Folder1和Folder2日志未正确显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆