【问题标题】:Copy all files except the last modified file to a new directory将除最后修改的文件之外的所有文件复制到新目录
【发布时间】:2014-04-24 17:32:10
【问题描述】:

我需要将文件从 c:\prueba1 移动到 c:\prueba99 但我不知道如何在源目录 (c:\prueba99) 中的所有文件之间进行比较以移动目录中的所有文件目录中最后修改的文件除外。我知道有一个带有 get InstallDate、LastModified 的 wmic 命令,但我不知道 ms-dos 语法来分配一个变量并比较它以知道读取的一个文件是最后一次修改的文件

我找到了一个例子:

for /f "delims=" %%A in ('wmic datafile where "drive = 'c:' and path='\\windows\\'"
   get LastModified^,Name /format:table^|find ":"^|sort /r') do @echo %%A

并试图修改它但没有结果,因为它似乎只列出了数据文件名而不是文件本身。

这是我的修改版:

for /f "skip=1 delims=" %%A  in ('wmic datafile where "drive = 'c:' and path='\\prueba1\\'"
    get LastModified^,Name /format:table^|find ":"^| sort/r') do move (%%A) c:\prueba99

【问题讨论】:

    标签: windows batch-file last-modified


    【解决方案1】:
    for /f "skip=1 delims=" %%a in ('dir /b /tw /o-d /a-d c:\prueba1\*.*'
    ) do move "c:\prueba1\%%a" "c:\prueba99"
    

    dir 命令按创建日期降序获取文件,所以第一个是最新的。 for 命令遍历此列表,跳过第一个,将文件移动到目标文件夹。

    【讨论】:

    • dir /b /tw /o-d /a-d 是什么意思?
    • @MethodistMX,仅文件名(/b),使用修改日期进行日期排序(/tw),按日期降序排列(/o-d),不包括目录(@987654327 @)
    【解决方案2】:

    获取最后修改文件的方法:

    @echo off
    set $path=c:\prueba99
    
    for /f %%a in ('dir/b/a-d/o-d "%$path%"') do (
    set $Last=%%a
    goto:next)
    
    :next
    echo Last modified : [ %$Last% ]
    

    【讨论】:

      【解决方案3】:

      这应该对您有用,它还允许您丢弃任意数量的 NEWEST 文件(对于您的情况,我采用了 1 个):

      @echo off
      setlocal
      
      :: change the next two statements to match what you want
      set srcdir=C:\prueba1
      set tgtdir=C:\prueba99
      if not exist %tgtdir%\*.* md %tgtdir%
      set ctr=0
      for /f "tokens=*" %%a in ('dir "%srcdir%" /o-d /b') do call :maybemove "%%a"
      set /a ctr-=3
      echo %~n0: %ctr% files were moved from %srcdir% to %tgtdir%
      endlocal
      goto :eof
      
      ::--------------------
      
      :maybemove
      :: increment counter and bypass the ONE newest file
      set /a ctr+=1
      if %ctr% leq 1 goto :eof
      :: remove the double-quotes from the front and back of the filename
      set fn=%~1
      :: perform the move
      move /y "%srcdir%\%fn%" "%tgtdir%"
      goto :eof
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-25
        • 1970-01-01
        • 2017-07-01
        • 2019-02-08
        相关资源
        最近更新 更多