windows 批处理文件将超过 30 分钟的文件从一个文件夹复制到另一个文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3185489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:46:36  来源:igfitidea点击:

batch file to copy files older than 30 minutes from one folder to another

windowsbatch-filecmd

提问by shashikant

How can I copy files which are older than 30 minutes from current time from one folder to another one?

如何将当前时间超过 30 分钟的文件从一个文件夹复制到另一个文件夹?

回答by mousio

There are several Windows ports for the *nix findcommand available, offering switches like -mminand -mtimethat would be useful here, allowing the problem to be solved with a one-liner…
Note that Powershell certainly is a viable alternative to achieve this goal.

有几个Windows端口* nix中find可用的命令,提供开关一样-mmin,并-mtime会在这里很有用,从而可以使问题有一个班轮...来解决
。注意,PowerShell的肯定是一个可行的替代方案,以实现这一目标

In plain DOS, here is a rather limited batch script, but it suffices as a base to solve your problem:

在普通 DOS 中,这是一个相当有限的批处理脚本,但它足以作为解决您问题的基础:

@echo off
setlocal enabledelayedexpansion

call :GetRefTimestamp -30
for %%f in (*) do (
    call :GetFileTimestamp "%%~tf"
    if "!filetimestamp!" LSS "!reftimestamp!" echo -- %%f is older than 30 minutes
    if NOT "!filetimestamp!" LSS "!reftimestamp!" echo ++ %%f is NOT older than 30 minutes
)

endlocal
goto :EOF

:GetRefTimestamp
::get current date/time
for /f "usebackq tokens=1-5 delims=/:, " %%f in (`echo %DATE:~-10% %TIME: =0%`) do set reftimestamp=%%h%%g%%fT%%i%%j
::apply delta (format [-]HHMM) on time part - not handling over/underflow
set /a timedelta=%~1
set timedeltasign=
if %timedelta% LSS 0 set timedeltasign=-
set timeHHMM=%timestamp:~-4%
set /a timeHHMM+=timedelta
set /a timeMM=timeHHMM %% 100
if %timeMM% GEQ 60 set /a timeHHMM+=%timedeltasign%40
set timeHHMM=000%timeHHMM%
set reftimestamp=%reftimestamp:~0,-4%%timeHHMM:~-4%
goto :EOF

:GetFileTimestamp
::get file date/time
for /f "usebackq tokens=1-5 delims=/:, " %%f in (`echo %~1`) do set filetimestamp=%%h%%g%%fT%%i%%j
goto :EOF

Just use common sense for the delta (knowing the limitations) and refrain from using leading zeroes :]

只需对 delta 使用常识(了解限制)并避免使用前导零:]

回答by npocmaka

The easiest way is to use robocopy (or forfiles)

最简单的方法是使用 robocopy(或 forfiles)

robocopy is part of win2003 rtk and is installed on vista and windows 7 by default -> http://www.microsoft.com/downloads/details.aspx?FamilyID=9d467a69-57ff-4ae7-96ee-b18c4790cffd&displaylang=en

robocopy 是 win2003 rtk 的一部分,默认安装在 vista 和 windows 7 上 -> http://www.microsoft.com/downloads/details.aspx?FamilyID=9d467a69-57ff-4ae7-96ee-b18c4790cffd&displaylang=en

FORFILES is part of Windows 2000/NT resource kit but works fine on XP/Vista/7 -> (I think it is not available on microsoft site anymore)

FORFILES 是 Windows 2000/NT 资源工具包的一部分,但在 XP/Vista/7 上运行良好 ->(我认为它不再在 microsoft 站点上可用)

h_ttp://www.petri.co.il/download_free_reskit_tools.htm

h_ttp://www.petri.co.il/download_free_reskit_tools.htm