0

Related: How to get the most recent file using a batch script in windows

I want to copy the latest 2 files from a directory using Windows batch script.

Community
  • 1
  • 1
ATOzTOA
  • 34,814
  • 22
  • 96
  • 117

2 Answers2

2
@ECHO OFF
SETLOCAL
SET transfer=xx
FOR /f "delims=" %%i IN ('dir/b/a-d/o-d *.*') DO IF DEFINED transfer CALL SET transfer=%%transfer:~1%%&ECHO %%i

Just set TRANSFER to a length of #transfers to execute; obviously replace echo %%i with an appropriate COPY command

madth3
  • 7,275
  • 12
  • 50
  • 74
Magoo
  • 77,302
  • 8
  • 62
  • 84
0

My version based on Peter Wright's answer...

@ECHO OFF
setlocal EnableDelayedExpansion
set j=0

FOR /f "delims=" %%i IN ('dir /b /a-d /o-d *.*') DO (
    echo %%i
    set /A j=j+1
    if !j! geq 2 (
        goto :end
    )
)
:end
Community
  • 1
  • 1
ATOzTOA
  • 34,814
  • 22
  • 96
  • 117