2012-08-09 109 views
4

我的功能所需要的第一个文件名从一个特定的目录中的第一个文件来处理一些测试,在完成测试从目录
批处理脚本从目录中获取第一个文件名

我试过如下

先删除文件
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 ******************************************************** 

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

回答

13

另一种方法是使用goto(减免环路)你有你的第一个文件后:

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

而且它可以运行(一点)快在某些情况下,因为它不必须经过整个目录。

5

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

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