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
FOR /F "delims=" %%i in ('dir /O-D /B "%TEST_RESULTS%\*.trx"')
SET INPUT_PATH=%TEST_RESULTS%\%%~ni
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.
%% 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.
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.
&& 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/…
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With