FOR /F "delims=" %%i IN ('DIR "Test*.xlsx" /B /OD /A-D') DO set "file_name=%%i"
or
FOR /F "delims=" %%i IN ('DIR "Test*.xlsx" /B /O-D /A-D') DO set "file_name=%%i"&goto nextstep
:nextstep
The "delims=" removes delimiters which default to include Space - this is why the name is terminated at the space - Space is a delimiter and thedefault for tokens is 1 so only the first token is assigned, delimited by the default Space
The dir switch /-d makes no sense; it reacts the same way as /d which lays out the directory list in columnar form and is overridden by the /b.
/a-d suppresses directorynames from the list.
/od shows the directorylist in date-order and /o-d in reverse-date order. Hence, the first snippet assigns each filename in turn to the variable and after the for block, the variable contains the last name assigned which will be the filename with the latest date.
The second block forcibly jumps out of the for loop when the first filename is assigned, so if the sorting is accomplished with /o-d then the latest is listed and hence assigned first.
The advantage of the second form is that it is a lot faster, especially if the directory contains a great number of files.