如何将10个最新文件批量复制到Windows中的目录?

时间:2017-06-08 16:22:28

标签: windows batch-file

我使用以下脚本在目录中仅保留最新的360个文件(一年,每日备份):

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

如何将最新的7个文件复制到另一个目录?

1 个答案:

答案 0 :(得分: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
    )