Passing the first file in a DIR command to a variable

I want to transfer the very first file returned

dir *.png /B 

to the variable% firstFile%

+4
source share
4 answers

Why do you need dir ?

 for %%x in (*.png) do if not defined firstFile set "firstFile=%%x" 
+5
source

why do you have to sort through the whole list?

 for %%x in (*.*) do ( set "firstFile=%%x" goto :done ) :done 
+3
source

must not be:

 DIR *.png /B > "%TEMP%\dir" && SET firstFile= < "%TEMP%\dir" 
+2
source
 DIR *.png /B > "%TEMP%\dir" && < "%TEMP%\dir" SET firstFile= 
0
source

Source: https://habr.com/ru/post/1400449/


All Articles