Here's a batch file which will return the last item output by find
:
@echo off
ls | find ".txt" > %temp%\temp.txt
for /f %%i in (%temp%\temp.txt) do set file=%%i
del %temp%\temp.txt
echo %file%
for
has a syntax for parsing command output, for /f "usebackq"
, but it cannot handle pipes in the command, so I've redirected output to a temporary location.
I strongly recommend, given that you have access to ls
, that you consider using a better batch language, such as bash
or even an scripting language like python
or ruby
. Even bash
would be a 20x improvement over cmd scripting.