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
  • 32,738
  • 21
  • 92
  • 115

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,151
  • 12
  • 47
  • 72
Magoo
  • 72,034
  • 7
  • 58
  • 78
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
  • 32,738
  • 21
  • 92
  • 115