繁体   English   中英

批处理文件以从1天前开始复制文件

[英]Batch file to copy files from 1 day before and no more

假设我有2个文件夹:

  • C:\\ FolderData (每天都会随着新文件而增加)
  • C:\\ FolderTemp (仅需要执行前一天的文件,不包括小时数/时间戳)

FolderData将接收文件名,如果没有日期,则文件名将等于,例如:

  1. SYS_PURCHASES_20170612.xls
  2. SYS_PURCHASES_20170613.xls
  3. SYS_PURCHASES_20170614.xls

如果今天运行.bat文件, 则只需要将SYS_PURCHASES_20170613.xls复制到FolderTemp即可

我尝试使用robocopy但显然无法获得相同的最小值和最大值

robocopy "C:\\FolderData" "C:\\FolderTemp" /minage:1 /maxage:1

另外,如果我尝试:

robocopy "C:\\FolderData" "C:\\FolderTemp" /minage:1 /maxage:2

这会带来两个 SYS_PURCHASES_20170612.xlsSYS_PURCHASES_20170613.xls,不是我所需要的

除此之外,我尝试使用forfiles但也无济于事。

forfiles /p C:\\FolderData /D -1 /C "/C /copy /y @file C:\\FolderTemp"

甚至

forfiles /p C:\\FolderData /D -1 /C "/C /copy /y C:\\FolderData\\@file C:\\FolderTemp"

和其他变量一样,它返回的内容是“系统无法返回指定的文件”乘以文件夹中文件的数量。

请注意,以下涉及的其他过程也应忽略 ,只是我不知道如何执行上述步骤

我的批次归档需要执行的所有步骤:

  1. 从Folder2之前1天开始从folder1复制文件( 需要我的帮助
  2. 从folder2的所有文件中删除最后9位数字(将删除日期,使用此解决方案上的循环),因此该文件为SYS_PURCHASES.xls
  3. 将文件从folder2移动并替换到folder3(使用简单的move /y

由于您只想触摸昨天的文件,因此我将使用forfiles ,因为在指定/D选项时,此命令仅考虑日期而不是时间。 robocopy也会考虑时间,这可能不是您想要的。

这是一种可能的方法:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "_SOURCE=C:\FolderData"
set "_TARGET=C:\FolderTemp"
set "_MASK=?*_????????.*"
set /A "_DAYS_AGO=1"

rem // Check given age and calculate interim value `PREV` needed for `forfiles`:
(if %_DAYS_AGO% LSS 0 set "_DAYS_AGO=0") & set /A "PREV=_DAYS_AGO+1"
rem // Let `forfiles` output the filtered files and capture them by `for /F`:
for /F "delims= eol=|" %%F in ('
    rem/ Use two nested `forfiles` loops to filter for files of specified day: ^
        ^& forfiles /P "%_SOURCE%" /M "%_MASK%" /D -%_DAYS_AGO% ^
        /C "cmd /C if @isdir==FALSE > nul 2>&1 forfiles /M @file /D -%PREV% || echo @file"
') do (
    rem // Store current file name and extension:
    set "FILE=%%~nF" & set "FEXT=%%~xF"
    setlocal EnableDelayedExpansion
    rem // Copy the file and remove the last 9 characters from its name:
    > nul copy /Y "!_SOURCE!\!FILE!!FEXT!" "!_TARGET!\!FILE:~,-9!!FEXT!"
    endlocal
)

endlocal
exit /B

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM