Script package to get the first file name from a directory

My function needs the first file name from a specific directory to handle some tests using the first file, when the tests end, delete the first file from the directory

I tried as below

FOR /R \\<My Folder> %F in (*.zip*) do echo ~nF >%test_list% set /p firstline=%test_list% echo %firstline% FOR /F "tokens=*" %%A IN ('dir %test_firmware_dir% /b/s ^| find /c ".zip"') DO SET count=%%A echo %count% :test_execution if %count% NEQ 0 ( echo ON dir /b %test_firmware_dir%\*.zip >%test_firmware_list% set /p firstline=<%test_firmware_list% set /a filename=%firstline:~0,-4% Echo %filename% Echo ********************************************************* Echo "Now running batch queue tests with %filename%" Echo ******************************************************** 

it shows the last element of any procedure to get the first element

+4
source share
2 answers

An alternative is to use goto (breaks the FOR loop) after you have the first file:

 FOR %%F IN (%test_firmware_dir%\*.zip) DO ( set filename=%%F goto tests ) :tests echo "%filename%" 

And in some cases, it can work (slightly) faster, since it does not need to go through the entire directory.

+14
source

Assuming you mean the first file when sorting alphabetically; then, given that you are traversing a list overwriting the last value that you will, as you say, capture the last file, instead follow the sort order;

 for /f "delims=" %%F in ('dir %test_firmware_dir%\*.zip /b /o-n') do set file=%%F echo 1st alpha file is %file% 
+5
source

All Articles