Peform tasks based upon matching files last modified stamp being greater than x minutes

81 Views Asked by At

The following batch program, when I execute it, the files are moved correctly, and the .dat files are correctly deleted.

@echo off
setlocal enabledelayedexpansion

set sourceFolder=C:\test_S
set destinationFolder=C:\test_D

for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set datetime=%%a

set currentDate=%date:~6,4%%date:~3,2%%date:~0,2%
set currentTime=%time:~0,2%%time:~3,2%

for %%F in (%sourceFolder%\*.zip) do (
    set fileModifiedDate=

    for %%G in (%%F) do set fileModifiedDate=%%~tG

    set fileDate=!fileModifiedDate:~6,4!!fileModifiedDate:~3,2!!fileModifiedDate:~0,2!
    set fileTime=!fileModifiedDate:~11,2!!fileModifiedDate:~14,2!

    set /a elapsedMinutes=currentDate*1440+currentTime/100-fileDate*1440-fileTime/100

    if !elapsedMinutes! geq 2 (
        set baseFileName=%%~nF
        set tmpFileName=!baseFileName!.tmp
        set zipFileName=!tmpFileName!.zip

        ren %%F !tmpFileName!
        move %sourceFolder%\!tmpFileName! %destinationFolder%
        ren %destinationFolder%\!tmpFileName! !zipFileName!
    
        set datFilePath=%sourceFolder%\!baseFileName!.dat
        if exist !datFilePath! (
            del !datFilePath!
        )
    )
)

endlocal

However, I want the files to be moved only if it has a last modified time of greater than 2 minutes ago.

To clarify, if a zip file's last modified date is greater that two minutes, move it, and delete the adjacent dat file with the same basename.

Could someone help me with that?

I would prefer it to be in batch because a part of my script is already in that language.

0

There are 0 best solutions below