5

I am using dir abc*.*/o:-d/b >> "testfile1.txt" to get the output in descending order. Is there a way to get only 5 / 10 files as output. Actually I want to store the latest 5 (sorted by Date Modified) files in testfile1.txt.

Appreciate your response

3 Answers 3

8
@echo off
setlocal
set /a "n=0, limit=5"
>"testfile1.txt" (
  for /f "eol=: delims=" %%F in ('dir /o-d /b abc*.*') do (
    echo %%F
    2>nul set /a "n+=1, 1/(limit-n)"||goto :break
  )
)
:break

I intentionally divide by 0 to detect when the limit has been reached. I could simply use an IF statement instead, but that would require delayed expansion, and delayed expansion would corrupt a file name that contains !. A proper solution with delayed expansion must toggle delayed expansion on and off within the loop.

0
3

I know I'm a little late on this one, but how about this instead:

@echo off
for /f "tokens=1* delims=:" %%a in ('dir abc*.*/o:-d/b ^| findstr /n .') do if %%a leq 5 echo.%%b>>"testfile1.txt"

Here's what I did to make this work:

  • Piped your dir statement into findstr to number each line (dir abc*.*/o:-d/b | findstr /n .)
  • Ran it through a for loop to separate line #'s from content (using "tokens=1* delims=:")
  • Used an if statement to verify the current line # is less than or equal to "5" (if %%a leq 5...)
  • Exported all lines that match to the file you specified (echo.%%b>>"testfile1.txt")

Edit: changed 'findstr /n .*' to 'findstr /n .', as there are no empty lines too watch out for (see comments below).

4
  • +1; You can simplify your FINDSTR regex to .. The asterisk is not needed :-)
    – dbenham
    Commented Oct 26, 2012 at 11:24
  • Thanks @dbenham. However, when I pipe a file into findstr, the blank lines are skipped with just .. I believe I need the star to modify that wildcard to be 'zero or more' results. I may have to tweak the above method anyway as it will choke on lines that start with colons (@Jeb pointed it out in another thread). Looking into a solution for that now... Commented Oct 26, 2012 at 13:07
  • Yes, but in this case we do not have to worry about empty lines. The data is coming from DIR /B, not a file. So the * is not needed. Also, you do not need to worry about lines starting with the EOL character (; by default) because you know that each line will start with a number.
    – dbenham
    Commented Oct 26, 2012 at 16:02
  • Ah, excellent point (no empty lines sent from dir). On the EOL character... what I meant was colon delimiter I'm using to separate the line numbers from the content (added by findstr). Because the for loop collapses consecutive delimiters into one, lines that start with a colon will have the colons stripped off by my code above. Your point is still valid here though: none of the lines from a dir /b output should start with (or even contain) colons. Commented Oct 26, 2012 at 21:00
0

Does this have to be a batch file? This would be trivial in PowerShell.

To do it with plain old batch files, you'll need something similar to the head utility, to grab only the first few lines of output.

You can adapt the solution here to suit your needs (note the section in the answer about the little "winhead.cmd" script): Batch command to take only first line from input

Not the answer you're looking for? Browse other questions tagged or ask your own question.