Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For loop and delims in batch files

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

specifically

  1. FOR /F "delims=" %%i in ('dir /O-D /B "%TEST_RESULTS%\*.trx"')
  2. SET INPUT_PATH=%TEST_RESULTS%\%%~ni
like image 832
Gr-Disarray Avatar asked Aug 19 '13 14:08

Gr-Disarray


People also ask

What is Delims in batch file?

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.

Why is %% used in batch file?

%% in batch acted like \\ in bash. Where one would need to cancel the meaning of the previous percent-sign in a batch file; because variables in batch look like %var% . So because percent had a special meaning you needed to use %%var%% so a variable was still usable in a batch file.

Can I use for loop in CMD?

Related commands:FOR - Loop through a set of files in one folder. FOR /R - Loop through files (recurse subfolders) . FOR /D - Loop through several folders. FOR /L - Loop through a range of numbers.

What does && do in batch file?

&& runs the second command on the line when the first command comes back successfully (i.e. errorlevel == 0 ). The opposite of && is || , which runs the second command when the first command is unsuccessful (i.e. errorlevel != 0 ). @Spandy: web.archive.org/web/20060412075633/https://www.microsoft.com/…


1 Answers

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.

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).

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.

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.

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.

If the GOTO were not there, then the end result would be the oldest *.trx file instead.

like image 109
dbenham Avatar answered Oct 11 '22 18:10

dbenham