Reputation: 35950
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.
Upvotes: 0
Views: 1774
Reputation: 35950
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
Upvotes: 0
Reputation: 79982
@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
Upvotes: 2