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

前端 未结 1 499
时光取名叫无心
时光取名叫无心 2020-12-22 13:22

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         


        
1条回答
  •  独厮守ぢ
    2020-12-22 13:36

    @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
        )
    

    0 讨论(0)
提交回复
热议问题