.bat to check how long files have been in directory, then ts

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
boredmatt
Posts: 2
Joined: 14 Feb 2012 11:23

.bat to check how long files have been in directory, then ts

#1 Post by boredmatt » 14 Feb 2012 11:28

First of all, I'm very sorry for signing up and posting a question like this, I know how annoying that can be, but I'm quite desperate (if it helps, I picked this forum above all others :) )

Basically, I need to write a batch file to check how long files have been sitting in a directory for, and if it's longer than, say 30 minutes, they need to be touched/timestamped.

I never normally ask questions of a forum until I've contributed for a bit, but like I said, I'm very desperate!

Any help really would be appreciated. Thanks.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: .bat to check how long files have been in directory, the

#2 Post by foxidrive » 14 Feb 2012 18:30

boredmatt wrote:I need to write a batch file to check how long files have been sitting in a directory for, and if it's longer than, say 30 minutes, they need to be touched/timestamped.



It would be helpful to know the reasoning behind the task, as there could be another way to approach it.

There are utilities that will return dates and time and do time mathematics - Fdate is one older one - but plain batch for this task would be tedious.

Aacini
Expert
Posts: 1930
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: .bat to check how long files have been in directory, the

#3 Post by Aacini » 14 Feb 2012 20:25

Code: Select all

@echo off
set howLong=30
for /F "tokens=1,2 delims=:" %%a in ("%time%") do set timeNow=(%%a*60)+%%b
for %%f in (*.*) do (
   for /F "tokens=2,3" %%a in ("%%~Tf") do (
      for /F "tokens=1,2 delims=:" %%i in ("%%a") do set /A fLapse=(1%%i %% 100)*60+%%j+%howLong%
      if "%%b" == "p.m." set /A fLapse+=12*60
   )
   if !fLapse! lss %timeNow% copy +%%f,,
)

If the file Lapse may pass over midnight, the date must be included in the calculation.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: .bat to check how long files have been in directory, the

#4 Post by foxidrive » 14 Feb 2012 22:22

Aacini wrote:If the file Lapse may pass over midnight, the date must be included in the calculation.


That's my point exactly.

Aacini
Expert
Posts: 1930
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: .bat to check how long files have been in directory, the

#5 Post by Aacini » 15 Feb 2012 02:29

The Batch file below correctly handle the case when the file Lapse pass over midnight:

Code: Select all

@echo off
set howLong=30
for /F "tokens=1,2 delims=:" %%a in ("%time%") do set timeNow=((%%a+24)*60)+%%b
for /F "tokens=2 delims=/" %%a in ("%date%") do set today=%%a
for %%f in (*.*) do (
   for /F "tokens=2-6 delims=/: " %%a in ("%%~Tf") do (
      set /A fLapse=(1%%c %% 100)*60+%%d+%howLong%
      if "%%e" == "p.m." set /A fLapse+=12*60
      if "%%a" == "%today%" set /A fLapse+=24*60
   )
   if !fLapse! lss %timeNow% copy +%%f,,
)
foxidrive wrote:plain batch for this task would be tedious.

I don't think that a dozen lines of Batch code be tedious at all... :wink:

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: .bat to check how long files have been in directory, the

#6 Post by foxidrive » 15 Feb 2012 02:55

Is there a regional settings issue? That's also what is tedious about it.

Code: Select all

*60+%d+30 was unexpected at this time.


Code: Select all

d:\AAA>for %a in (*.*) do @echo %~Ta
15/02/2012 12:11
15/02/2012 12:10
14/02/2012 21:05
15/02/2012 19:48
15/02/2012 19:48
14/02/2012 09:30
14/02/2012 08:47
14/02/2012 21:05
08/02/2012 11:30
08/02/2012 12:49
10/06/2003 00:06
15/02/2012 12:11
15/02/2012 12:11

boredmatt
Posts: 2
Joined: 14 Feb 2012 11:23

Re: .bat to check how long files have been in directory, the

#7 Post by boredmatt » 15 Feb 2012 05:40

Thank you for the responses guys!

Last night, I managed to cobble together a couple of examples that I found around the web, and I now have this:

Code: Select all

@echo off 
cd "C:\exampledir"
setlocal
call :DateToMinutes %date:~-4% %date:~-10,2% %date:~-7,2% %time:~0,2% %time:~3,2% NowMins

set flag=0
for /f "delims=" %%a in ('dir *.mpg /a-d /b') do call :CheckMins "%%a" "%%~ta"

set flag=0 
goto :EOF

:CheckMins
set File=%1
set TimeStamp=%2
call :DateToMinutes %timestamp:~7,4% %timestamp:~1,2% %timestamp:~4,2% %timestamp:~12,2% %timestamp:~15,2%%timestamp:~18,1% FileMins
set /a MinsOld=%NowMins%-%FileMins%
if %MinsOld% geq 1 copy /b %file% +,, > nul

goto :EOF

:DateToMinutes
setlocal
set yy=%1&set mm=%2&set dd=%3&set hh=%4&set nn=%5
if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
set /a dd=100%dd%%%100,mm=100%mm%%%100
set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633
if 1%hh% LSS 20 set hh=0%hh%
if /i {%nn:~2,1%} EQU {p} if "%hh%" NEQ "12" set hh=1%hh%&set/a hh-=88
if /i {%nn:~2,1%} EQU {a} if "%hh%" EQU "12" set hh=00
if /i {%nn:~2,1%} GEQ {a} set nn=%nn:~0,2%
set /a hh=100%hh%%%100,nn=100%nn%%%100,j=j*1440+hh*60+nn
endlocal&set %6=%j%&goto :EOF



This now updates modification time of anything older than 1 minute (or whatever is set), and it seems to work well, however, when running it on my local machine, I can't target a folder on a network share (by either setting it as '\\netshare\folder' or by mapping it to a letter 'X:\'). Is there anything obvious I'm missing?

Thanks.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: .bat to check how long files have been in directory, the

#8 Post by foxidrive » 15 Feb 2012 06:01

Modify these two lines after mapping drive x: to your share - see if the copy command is echoed at all. If not then some more tests are needed.

If it is then try the copy command at a cmd prompt and see if it works on your network drive. If not then you may need a 'touch' utility.

Code: Select all

@echo off 
cd /d "x:\"

...

if %MinsOld% geq 1 echo copy /b %file% +,,

Post Reply