[英]Batch script to move file from scource to destination folder for files older than 30 day
[英]batch file to copy files older than 30 minutes from one folder to another
如何將當前時間超過30分鍾的文件從一個文件夾復制到另一個文件夾?
有幾個Windows端口可供* nix find
命令使用,提供-mmin
和-mtime
等開關,這在這里很有用,可以通過單行解決問題...
請注意,Powershell當然是實現這一目標的可行替代方案。
在普通的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
只需使用delta的常識(知道限制)並避免使用前導零:]
最簡單的方法是使用robocopy(或forfiles)
robocopy是win2003 rtk的一部分,默認安裝在vista和Windows 7上 - > http://www.microsoft.com/downloads/details.aspx?FamilyID=9d467a69-57ff-4ae7-96ee-b18c4790cffd&displaylang=en
FORFILES是Windows 2000 / NT資源工具包的一部分,但在XP / Vista / 7上工作正常 - >(我認為它不再在微軟網站上提供)
h_ttp://www.petri.co.il/download_free_reskit_tools.htm
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.