用于从目录获取第一个文件名的批处理脚本

cha*_*nna 6 batch-file

我的函数需要来自特定目录的第一个文件名来处理使用第一个文件的一些测试,在完成测试时从目录中删除第一个文件

我尝试如下

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 ********************************************************
Run Code Online (Sandbox Code Playgroud)

它显示最后一个元素获取第一个元素的任何过程?

Dra*_*kel 20

另一种方法是goto在获得第一个文件后使用(中断FOR循环):

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

并且它可以在某些情况下更快地运行(一点点),因为它不必遍历整个目录.


Ale*_* K. 8

假设您按字母顺序排序时表示第一个文件; 然后假设您遍历列表覆盖最后一个值,就像你说捕获最后一个文件一样,而是控制排序顺序;

for /f "delims=" %%F in ('dir %test_firmware_dir%\*.zip /b /o-n') do set file=%%F
echo 1st alpha file is %file%
Run Code Online (Sandbox Code Playgroud)