BAT / POWERSHELL复制(仅)昨天的文件

时间:2015-04-08 08:31:35

标签: powershell batch-file copy timestamp

我正在尝试编写一些脚本来复制昨天创建的所有文件(仅限!)

xcopy的/ p> / D参数表示复制文件在指定日期或之后更改,因此它不是我正在搜索的内容。有任何想法吗? ; /

3 个答案:

答案 0 :(得分:2)

我建议你使用powershell。您可以使用Get-ChildItemWhere-Object获取前一天创建的文件列表

$yesterdayFiles = Get-ChildItem | Where-Object {$_.CreationTime.Date -eq ((Get-Date).AddDays(-1).Date)}

然后,您可以使用$yesterdayFiles cmdlet复制Copy-Item变量中存储的文件

答案 1 :(得分:2)

我尝试使用 robocopy / minage:1 / maxage:1 ,但似乎无效。但是当我设置当前日期时工作。这是脚本(你需要设置你的来源和目的地):

@echo off

set "source=C:\folder1"
set "dest=C:\folder2"

pushd "%temp%"
::get cirrent date
makecab /D RptFileName=~.rpt /D InfFileName=nul /f nul >nul
for /f "tokens=3-7" %%a in ('find /i "makecab"^<~.rpt') do (
   set "year=%%e"
   set "mon=%%b"
   set "day=%%c"

)
del ~.*
popd
:: convert month to numeric string
for %%a in (
    "Jan-01" "Feb-02" "Mar-03" "Apr-04" "May-05" "Jun-06" "Jul-07" "Aug-08" "Sep-09" "Oct-10" "Nov-11" "Dec-12"
) do (
    for /f "tokens=1,2 delims=-" %%x in ("%%~a") do (
        if "%mon%" equ "%%x" (
            set "mon=%%y"
            goto :skip
        )
    )
)
:skip
set "c_date=%year%%mon%%day%"
::echo %c_date%

:: is switch is for force overwriting
robocopy "%source%" "%dest%" * /maxage:1 /minage:%c_date% /is
自Vista之后,每个窗口都内置了robocopy。如果你在XP或Vista下运行,你需要从微软网站下载它。

答案 2 :(得分:1)

此批处理文件选择在今天之前创建的所有文件。如果您确定昨天创建了文件,那么它可以解决您的问题。

@echo off
setlocal EnableDelayedExpansion

set "yesterday="
for /F "skip=5 tokens=1,4*" %%a in ('dir /TC /O-D /A-D') do (
   if "%%a" neq "%date%" (
      if not defined yesterday set "yesterday=%%a"
      if "%%a" equ "!yesterday!" (
         echo Created yesterday: %%a "%%c"
      ) else (
         goto break
      )
   )
)
:break