windows 批处理文件中的 for 循环和 delims
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18316538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
For loop and delims in batch files
提问by arunsun
Can someone please help me understand command file syntax
有人可以帮我理解命令文件的语法吗
IF "%INPUT_PATH%"=="" (
echo Searching for latest test results in: %TEST_RESULTS%
FOR /F "delims=" %%i in ('dir /O-D /B "%TEST_RESULTS%\*.trx"') DO (
SET INPUT_PATH=%TEST_RESULTS%\%%~ni
GOTO :DoneInputPath
) )
I get that it first checks if INPUT_PATH variable is empty and if it is empty then enters into an inner for loop, I am lost otherwise
我知道它首先检查 INPUT_PATH 变量是否为空,如果为空则进入内部 for 循环,否则我会迷路
specifically
具体来说
FOR /F "delims=" %%i in ('dir /O-D /B "%TEST_RESULTS%\*.trx"')
SET INPUT_PATH=%TEST_RESULTS%\%%~ni
FOR /F "delims=" %%i in ('dir /O-D /B "%TEST_RESULTS%\*.trx"')
SET INPUT_PATH=%TEST_RESULTS%\%%~ni
回答by dbenham
Most of the information you need is available in the built-in help, though it can be daunting if you are new to batch programming. For example, type HELP FOR
or FOR /?
from the command prompt to get help on the FOR command.
您需要的大部分信息都可以在内置帮助中找到,但如果您不熟悉批处理编程,这可能会令人生畏。例如,在命令提示符下键入HELP FOR
或FOR /?
以获取有关 FOR 命令的帮助。
Explanation:
解释:
FOR /F "delims=" %%i in ('dir /O-D /B "%TEST_RESULTS%\*.trx"') ...
The DIR command lists all of the *.TRX
files within the %TEST_RESULTS%
path. The /B
option gives the brief format (file names only). The /O-D
option sorts the files by last modified date descending (newest first).
DIR 命令列出路径中的所有*.TRX
文件%TEST_RESULTS%
。该/B
选项给出了简要格式(仅文件名)。该/O-D
选项按上次修改日期降序(最新的在前)对文件进行排序。
The FOR /F command has three modes, depending on the format of the IN() clause. The fact that the IN() clause is enclosed in single quotes means that FOR /F treats the contents as a command, and processes the output of the command, one line at a time. The "delims="
option means do not parse into tokens (preserve each entire line). So each line is iteratively loaded into the %%i
variable. The %%i
variable only exists within the context of the FOR command.
FOR /F 命令具有三种模式,具体取决于 IN() 子句的格式。IN() 子句括在单引号中这一事实意味着 FOR /F 将内容视为命令,并处理命令的输出,一次一行。该"delims="
选项意味着不解析为标记(保留每一整行)。所以每一行都被迭代地加载到%%i
变量中。该%%i
变量仅存在于 FOR 命令的上下文中。
SET INPUT_PATH=%TEST_RESULTS%\%%~ni
I think you know what most of this command does. The only "unusual" aspect is the %%~ni
syntax. That syntax expands the value of %%i
into the base file name only, without any extension.
我想你知道这个命令的大部分内容。唯一的“不寻常”方面是%%~ni
语法。该语法仅将 的值扩展%%i
到基本文件名中,没有任何扩展名。
GOTO :DoneInputPath
The GOTO causes the FOR loop to abort after the first iteration. This means that INPUT_PATH will be set to the name of the most recently modified *.trx
file, since it sorted to the top.
GOTO 导致 FOR 循环在第一次迭代后中止。这意味着 INPUT_PATH 将被设置为最近修改的*.trx
文件的名称,因为它排在最前面。
If the GOTO were not there, then the end result would be the oldest *.trx
file instead.
如果 GOTO 不存在,那么最终结果将是最旧的*.trx
文件。
回答by Endoro
try this, explanation is in the comment:
试试这个,解释在评论中:
IF NOT DEFINED INPUT_PATH (
echo Searching for latest test results in: %TEST_RESULTS%
REM dir /OD means older files first and the youngest last, the last remains in INPUT_PATH; use "%%~nxi" for file name + file extension
FOR /F "delims=" %%i in ('dir /OD /B "%TEST_RESULTS%\*.trx"') DO SET "INPUT_PATH=%TEST_RESULTS%\%%~ni"
)