My function needs the first filename from a particular directory to process some tests using the first file, on completing tests delete first file from 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 is showing last element any procedure to get first element ??
An alternative is to use goto
(breaks FOR loops) after you have your first file:
FOR %%F IN (%test_firmware_dir%\*.zip) DO (
set filename=%%F
goto tests
)
:tests
echo "%filename%"
And it could run (a little bit) faster in some cases as it doesn't have to go through the whole directory.
Assuming you mean the 1st file when sorted alphabetically; then given that you traverse the list overwriting the last value you will as you say capture the last file, instead control 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%
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With