批处理文件以从1天前开始复制文件 [英] Batch file to copy files from 1 day before and no more

查看:224
本文介绍了批处理文件以从1天前开始复制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象我有2个文件夹:

  • C:\ FolderData (每天随着新文件的增加而增加)
  • C:\ FolderTemp (需要从执行前一天开始仅包含文件,不包括小时数/时间戳)
  • C:\FolderData (will be incremented daily with new files)
  • C:\FolderTemp (Needs to have only files from the day before executing, excluding hours/timestamp)

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

The FolderData will receive filenames that if not for date would be equals, as in:

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

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

If I run the .bat file today I need that only the SYS_PURCHASES_20170613.xls be copied over to FolderTemp.

我尝试使用robocopy,但显然它不能接收到相同的最小值和最大值

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

此外,如果我尝试:

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

它将带来两者 SYS_PURCHASES_20170612.xls SYS_PURCHASES_20170613.xls ,这不是我所需要的.

It will bring both SYS_PURCHASES_20170612.xls and SYS_PURCHASES_20170613.xls, which is not what I need.

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

Other than that, I tried using forfiles but also with no avail.

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"

和其他变量,但它会返回系统无法返回指定文件"乘以文件夹中文件数的行.

And other variables but it returns something along the line "the system can't return the specified file" times the number of files in the folder.

请注意,下面还有涉及其他过程,应该忽略,只是我不知道如何执行上述步骤.

Note that there are other processes involved bellow that should be ignored, It's just that I can't figure out how to do the step above.

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

All steps my batch filed needs to do:

  1. 从Folder2之前1天开始从Folder1文件复制(我需要帮助)
  2. 从folder2的所有文件中删除最后9位数字(将删除日期,使用此解决方案上的循环),因此文件将为SYS_PURCHASES.xls
  3. 使用简单的move /y将文件夹2中的文件移动并替换到文件夹3中
  1. Copy from folder1 files from 1 day prior to folder2 (what I need help)
  2. Remove last 9 digits from all files from folder2 (will remove the date, used the loop on this solution) so the file will be SYS_PURCHASES.xls
  3. Move and replacing the files from folder2 to folder3 (using a simple move /y)

推荐答案

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

Since you want to touch the files from yesterday only, I would use forfiles, because this command only regards the date but not the time, when specifying the /D option. robocopy instead also regards the time, which is probably not what you want.

这是一种可能的方法:

@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

这篇关于批处理文件以从1天前开始复制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆