How to find and copy the last created file larger than a custom format?


I wrote a batch file which should find and copy the latest build file bigger than a 3.5 GB.

set source=D:\src
set tmp_dir="D:\tmp"

set "file="
for /f "delims=|" %%a in ('
    dir /b /o-d "%source%\*.zip" 2^>nul
    ^| cmd /q /v /c"set /p .=&if defined . (echo(!.!)"
') do set "file=%%a"
echo %file%

robocopy %source% %tmp_dir% %file% /MT:16 /X /MIN:3500000000 /XO /V /TS /FFT /R:3 /W:10 /ETA

My problem is what happening when the latest file is smaller than 3.5 GB. In this case is need to copy the next file larger than 3.5 GB.

You almost have it. Instead of using the dir command to retrieve the initial list of files, we can use robocopy to get a list (/l) of files in the source folder with the required minimum size (/min). This list should not include job header, job footer, nor directory list (/njh, /njf, /ndl). For each file we don't need the class of the file (/nc) or the size of the file (/ns), but we want the time stamp (/ts) of the files to sort the list on this field.

This will leave a sorted list of files with the newer file matching the conditions as the first in the list.

@echo off
    setlocal enableextensions disabledelayedexpansion

    set "source=D:\src"
    set "tmp_dir=D:\tmp"

    set "file="
    for /f "tokens=2,*" %%a in ('
        robocopy "%source%" . *.zip /l /xx /is /njh /njs /ndl /nc /ns /ts /min:350000000000
        ^| sort /r
        ^| cmd /q /v /c"set /p .=&if defined . (echo(!.!)"
    ') do set "file=%%~nxb"

    if defined file (
        robocopy "%source%" "%tmp_dir%" "%file%" /MT:16 /X /XO /V /TS /FFT /R:3 /W:10 /ETA