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).