批处理脚本以从目录获取第一个文件名 [英] Batch script to Get First Filename from a directory

查看:700
本文介绍了批处理脚本以从目录获取第一个文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的函数需要特定目录中的第一个文件名才能使用第一个文件处理某些测试,完成测试后,请从目录中删除第一个文件

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

我尝试如下

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 ??

推荐答案

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

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.

这篇关于批处理脚本以从目录获取第一个文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆