如何在FOR循环中批量获取有关当前路径%CD%的信息? [英] How to get information about current path %CD% in batch in a FOR loop?

查看:53
本文介绍了如何在FOR循环中批量获取有关当前路径%CD%的信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用几个逗号分隔的目录路径设置了一个环境变量,并使用 FOR 循环将当前目录设置为变量中的下一个目录路径.而且我尝试获取当前路径,但是结果看起来很奇怪.

I set an environment variable with several comma separated directory paths and use a FOR loop to set current directory to next directory path in variable. And I try to get current path, but it looks strange about result.

我尝试使用以下代码:

set a=C:\test\A,C:\test\B,C:\test\C

for %%i in (%a%) do (
    echo %%i
    echo 1 %CD%
    cd %%i
    echo 2 %CD%
    echo ""
)

结果是:

 C:\test>TEST.BAT
 C:\test\A
 1 C:\test
 2 C:\test
 ""
 C:\test\B
 1 C:\test
 2 C:\test
 ""
 C:\test\C
 1 C:\test
 2 C:\test
 ""

 C:\test\C>

%CD%应该在 cd %% i 之后更改.但是所有 echo%CD%都会输出批处理文件路径 C:\ test 作为启动批处理文件时的当前目录.

%CD% should be changed after cd %%i. But all echo %CD% output the batch file path C:\test being the current directory on starting the batch file.

如何获取正确的当前路径?

How can I get correct current path?

推荐答案

在所有以(开头并以匹配结尾的命令块中使用%variable%的所有环境变量引用)被Windows命令解释器替换为所引用的环境变量的当前值,然后再分别执行命令块到命令块开始处剩下的命令.

All environment variable references using %variable% in a command block starting with ( and ending with matching ) are replaced by Windows command interpreter by current value of the referenced environment variable before the command block respectively to command left to begin of command block is executed at all.

这意味着对于发布的批处理文件中的 FOR 循环,Windows命令解释器在预处理状态期间将代码修改为:

This means for the FOR loop in posted batch file that Windows command interpreter modifies the code during preprocessing state to:

for %i in (C:\test\A C:\test\B C:\test\C) do (
echo %i
 echo 1 C:\test
 cd %i
 echo 2 C:\test
 echo ""
)

在命令提示符窗口中运行批处理文件时,可以看到与发布时完全相同的输出,而不是双击批处理文件(顶部已删除 @echo 或将其注释掉或修改为 @echo on .

This output exactly as posted can be seen on running the batch file from within a command prompt window instead of double clicking on batch file with @echo off at top removed or commented out or modified to @echo on.

请注意,目录列表中的每个逗号都用空格替换,%CD%都用 C:\ test 和修改的缩进代替.

Please note the replacements of each comma by space in list of directories, both %CD% being replaced by C:\test and modified indentations.

然后,使用此预处理的命令块执行 FOR 命令.

Then the FOR command is executed with this preprocessed command block.

批处理文件中的命令 cd %% i 可以工作,但是环境变量引用在执行前已被变量 CD 的当前值(此处为 C:\ test .

The commands cd %%i in the batch file work, but the environment variable references are replaced already before execution by current value of variable CD which is here C:\test.

在执行 FOR 循环期间,需要延迟的环境变量扩展才能查看当前目录.

Delayed environment variable expansion is needed to see the current directory during execution of the FOR loop.

@echo off
setlocal EnableDelayedExpansion
set "DirectoryList=C:\test\A C:\test\B C:\test\C"

for %%I in (%DirectoryList%) do (
    echo %%I
    echo 1 !CD!
    cd /D %%I
    echo 2 !CD!
    echo/
)

rem Other commands in batch file perhaps using DirectoryList.
endlocal

但是请注意,将目录/文件名中的感叹号解释为启用延迟的环境变量扩展作为变量引用的开始/结尾,这可能会导致执行意外行为.

But please take into account that an exclamation mark inside a directory/file name is interpreted on having delayed environment variable expansion enabled as begin/end of a variable reference which could result in unexpected behavior on execution.

要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • cd/?
  • echo/?
  • endlocal/?
  • 用于/?
  • rem/?
  • set/? ...还解释了在 IF FOR 示例上延迟的环境变量扩展.
  • setlocal/?
  • cd /?
  • echo /?
  • endlocal /?
  • for /?
  • rem /?
  • set /? ... explains also delayed environment variable expansion on an IF and a FOR example.
  • setlocal /?

另请参阅文章:

  • How to set environment variables with spaces?
  • This answer for details about the commands SETLOCAL and ENDLOCAL.
  • DosTips forum topic ECHO. FAILS to give text or blank line - Instead use ECHO/ why it is better to use echo/ instead of echo. (or echo "") to output an empty line.

这篇关于如何在FOR循环中批量获取有关当前路径%CD%的信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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