简体   繁体   中英

Copy latest 2 files using Windows batch script

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.

@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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM