简体   繁体   English

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

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

I use the following script to keep only the newest 360 files (a year, daily-made backup) in the directory: 我使用以下脚本将最新的360个文件(一年定期备份)保留在目录中:

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? 之后如何将最新的7个文件复制到另一个目录?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何编写Windows批处理脚本,将最新文件从目录复制到新文件夹? - How do I write a Windows batch script to copy the newest file from a directory into new folder? 批处理文件删除除最新的10个文件以外的所有文件 - batch file remove all but the newest 10 files 如何递归复制目录结构并仅复制底层中的文件(Windows Batch) - How can I recursivly copy a directory structure and only copy the files in the bottom tier (Windows Batch) 将错误处理添加到现有的Windows批处理脚本->以从目录复制最新文件 - Add error handling to existing Windows batch script-> to copy the newest file from a directory Windows Batch 询问文件格式,然后从列表中搜索文件并复制到目录 - Windows Batch ask for fileformat then search files from list and copy to directory 如何复制目录结构但只包含某些文件(使用 Windows 批处理文件) - How to copy a directory structure but only include certain files (using windows batch files) 如何在批处理文件中复制特定文件的目录 - how to copy directory of specific files in batch file 在子目录中搜索特定目录名称并复制文件(Windows批处理) - Search for specific directory names within subdirectories and copy files (Windows batch) Windows Shell批处理脚本复制最后10个修改的文件 - Windows Shell Batch Script to copy the last 10 modified files 如何使用批处理从Windows命令行中的每个子目录返回最新文件 - How to return the newest file from each sub-directory in Windows command line using batch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM