Reputation: 38180
This works except when there is space
for /f %%i in ('dir /b/a-d/od/t:c') do set LAST=%%i
Is there a way if there is space ?
Upvotes: 0
Views: 268
Reputation: 38589
You need to deal with the delimiters, because the default delimiters are the and the TAB. My suggestion therefore is either use all tokens, *
, or preferably no delimiters. It should also be noted that the default beginning of line, EOL
, is the semicolon, ;
, so a robust command would prevent names beginning with that character from being omitted from the results too.
Using all tokens, and setting the beginning of line to a character which cannot be included as the first in a file or directory name:
For /F "EOL=? Tokens=*" %%I In ('Dir /B /A:-D /O:D /T:C') Do Set "LAST=%%I"
The very small, but possible, issue with this method is that filenames could begin with one or more space characters, and, as those are the default delimiters, they would be consumed and removed in the returned results.
Using no delimiters and setting the beginning of line to a character which cannot be included as the first in a file or directory name:
For /F "EOL=| Delims=" %%I In ('Dir /B /A:-D /O:D /T:C') Do Set "LAST=%%I"
The above code examples will define the variable named LAST
, with the name of each file, until all of the files have been iterated, set
ting each and overwriting the previous. If you have a very large number of files in the, (current in this case), directory, you could alternatively reverse your sort method, (instead of /O:D
use /O:-D
), define the first value, then break out of the loop:
For /F "EOL=| Delims=" %%I In ('Dir /B /A:-D /O:-D /T:C') Do (
Set "LAST=%%I"
GoTo Done
)
:Done
Upvotes: 2