How to batch-copy the 10 newest files to a directory in Windows?

戏子无情 提交于 2020-01-10 06:12:07

问题


I use the following script to keep only the newest 360 files (a year, daily-made backup) in the directory:

for /f "skip=360 eol=: delims=" %%F in ('dir /b /o-d /a-d *.*') do @del "%%F"

How to afterwards copy the newest 7 files to another directory?


回答1:


@echo off
    setlocal enableextensions disabledelayedexpansion

    rem Three alternatives

    rem Pure arithmetics
    set "numFiles=7"
    for /f "delims=" %%a in ('dir /b /o-d /a-d') do (
        2>nul set /a "1/numFiles", "numFiles-=!!numFiles" && (
            echo copy "%%~fa" x:\somewehere
        )
    )

    rem Pure arithmetics 2 - No negation operator
    set "numFiles=7"
    for /f "delims=" %%a in ('dir /b /o-d /a-d') do (
        2>nul set /a "1/numFiles", "numFiles-=1" && (
            echo copy "%%~fa" x:\somewehere
        )
    )

    rem Number list of files
    set "numFiles=7"
    for /f "tokens=1,* delims=:" %%a in ('
        dir /b /o-d /a-d
        ^| findstr /n "^"
    ') do if %%a leq %numFiles% (
        echo copy "%%~fb" x:\somewehere
    )


来源:https://stackoverflow.com/questions/44440967/how-to-batch-copy-the-10-newest-files-to-a-directory-in-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!