我有一个相当有趣的delima。我正在尝试在目录中找到最新的7个文件。如果我只是从命令提示符运行dir /o-d /a-d /b|find /n /v ""
,它会返回顶部的最新7个文件,但如果我在批处理文件中的for循环中运行它,则不会。他们出了故障。我正在使用的代码是:
@echo off
setlocal
cd /d %~dp0
for /f "tokens=*" %%a in ('dir /o-n /b /a-d^|find /n /v ""' ) do (
echo %%a
)
我打算使用带有for /l
循环的数字来获取最新的7个文件但我不知道为什么会发生这种情况。以下是显示差异的文件。
F:\Scripts\Batch\Test\Check7>dir /o-d /a-d |find /n /v ""
[1] Volume in drive F is home
[2] Volume Serial Number is 0101-23B6
[3]
[4] Directory of F:\Scripts\Batch\Test\Check7
[5]
[6]08/26/2016 09:58 AM 256 Check7.cmd
[7]08/25/2016 05:48 PM 544 RexxTable.txt
[8]08/25/2016 05:48 PM 115,668 FCSBalanceFile.txt
[9]08/25/2016 05:48 PM 33,051 FCSFlatFile.txt
[10]08/25/2016 05:48 PM 1,475,304 ITF_MT_Deloitte_CRR.txt
[11]08/25/2016 05:48 PM 92 ITF_LOB_Strike.txt
[12]08/25/2016 05:48 PM 387,673 ITF_LOB_Daily.txt
[13]08/25/2016 05:48 PM 20,944 DDAFlatFile.txt
[14]08/24/2016 05:46 PM 115,668 FCSBalanceFile-08252016.txt
[15]08/24/2016 05:46 PM 22,894 FCSFlatFile-08252016.txt
[16]08/24/2016 05:46 PM 19,448 DDAFlatFile-08252016.txt
[17]08/23/2016 05:49 PM 115,668 FCSBalanceFile-08242016.txt
[18]08/23/2016 05:49 PM 31,730 FCSFlatFile-08242016.txt
[19]08/23/2016 05:48 PM 22,440 DDAFlatFile-08242016.txt
[20]08/22/2016 05:50 PM 115,560 FCSBalanceFile-08232016.txt
[21]08/22/2016 05:50 PM 38,748 FCSFlatFile-08232016.txt
[22]08/22/2016 05:49 PM 31,042 DDAFlatFile-08232016.txt
[23]08/19/2016 05:48 PM 115,560 FCSBalanceFile-08222016.txt
[24]08/19/2016 05:48 PM 22,242 FCSFlatFile-08222016.txt
[25]08/19/2016 05:48 PM 18,326 DDAFlatFile-08222016.txt
[26]08/18/2016 05:49 PM 115,560 FCSBalanceFile-08192016.txt
[27]08/18/2016 05:49 PM 34,588 FCSFlatFile-08192016.txt
[28]08/18/2016 05:48 PM 20,196 DDAFlatFile-08192016.txt
[29]08/17/2016 05:49 PM 115,560 FCSBalanceFile-08182016.txt
[30]08/17/2016 05:49 PM 22,507 FCSFlatFile-08182016.txt
[31]08/17/2016 05:49 PM 19,448 DDAFlatFile-08182016.txt
[32]08/16/2016 05:49 PM 115,452 FCSBalanceFile-08172016.txt
[33]08/16/2016 05:49 PM 30,712 FCSFlatFile-08172016.txt
[34]08/16/2016 05:49 PM 26,180 DDAFlatFile-08172016.txt
F:\Scripts\Batch\Test\Check7>Check7.cmd
[1] Volume in drive F is home
[2] Volume Serial Number is 0101-23B6
[3]
[4] Directory of F:\Scripts\Batch\Test\Check7
[5]
[6]08/25/2016 05:48 PM 544 RexxTable.txt
[7]08/25/2016 05:48 PM 1,475,304 ITF_MT_Deloitte_CRR.txt
[8]08/25/2016 05:48 PM 92 ITF_LOB_Strike.txt
[9]08/25/2016 05:48 PM 387,673 ITF_LOB_Daily.txt
[10]08/24/2016 05:46 PM 22,894 FCSFlatFile-08252016.txt
[11]08/23/2016 05:49 PM 31,730 FCSFlatFile-08242016.txt
[12]08/22/2016 05:50 PM 38,748 FCSFlatFile-08232016.txt
[13]08/19/2016 05:48 PM 22,242 FCSFlatFile-08222016.txt
[14]08/18/2016 05:49 PM 34,588 FCSFlatFile-08192016.txt
[15]08/17/2016 05:49 PM 22,507 FCSFlatFile-08182016.txt
[16]08/16/2016 05:49 PM 30,712 FCSFlatFile-08172016.txt
[17]08/25/2016 05:48 PM 33,051 FCSFlatFile.txt
[18]08/24/2016 05:46 PM 115,668 FCSBalanceFile-08252016.txt
[19]08/23/2016 05:49 PM 115,668 FCSBalanceFile-08242016.txt
[20]08/22/2016 05:50 PM 115,560 FCSBalanceFile-08232016.txt
[21]08/19/2016 05:48 PM 115,560 FCSBalanceFile-08222016.txt
[22]08/18/2016 05:49 PM 115,560 FCSBalanceFile-08192016.txt
[23]08/17/2016 05:49 PM 115,560 FCSBalanceFile-08182016.txt
[24]08/16/2016 05:49 PM 115,452 FCSBalanceFile-08172016.txt
[25]08/25/2016 05:48 PM 115,668 FCSBalanceFile.txt
[26]08/24/2016 05:46 PM 19,448 DDAFlatFile-08252016.txt
[27]08/23/2016 05:48 PM 22,440 DDAFlatFile-08242016.txt
[28]08/22/2016 05:49 PM 31,042 DDAFlatFile-08232016.txt
[29]08/19/2016 05:48 PM 18,326 DDAFlatFile-08222016.txt
[30]08/18/2016 05:48 PM 20,196 DDAFlatFile-08192016.txt
[31]08/17/2016 05:49 PM 19,448 DDAFlatFile-08182016.txt
[32]08/16/2016 05:49 PM 26,180 DDAFlatFile-08172016.txt
[33]08/25/2016 05:48 PM 20,944 DDAFlatFile.txt
[34]08/26/2016 09:58 AM 256 Check7.cmd
答案 0 :(得分:1)
这是完成的批处理文件。
for /f "tokens=1,2 delims=[]" %%a in (
'dir /o-d /a-d /b^|find /n /v ""' ) do (
echo %%~nxtb>>%~n0.txt
if %%a geq 7 goto :out
)
:out