Search code examples
for-loopdos

Sort directory in DOS and grab first n files


I need to sort a directory alphabetically and then grab the first n files in the folder. This is all in DOS.

I was using this as a start:

for %a in (*.tif) do (

bla bla

)

NOTE: all files in directory are .tif

BUT this will grab all the files, I don't know how to specify only to grab first n files (n=13). Any suggestions??? THANK YOU I really can't figure this one out!


Solution

  • Here's one way.

    @echo off
    set "target=d:\tif files"
    md "%target%" 2>nul
    for %%a in (*.tif) do (
    dir "%target%\*.tif" /b |find /c /v ""|findstr "^13$">nul && goto :done
    move "%%a" "%target%">nul
    )
    :done