Computer Hope

Microsoft => Microsoft DOS => Topic started by: bobcatalog on July 30, 2008, 12:07:26 PM

Title: Need help with a batch job.
Post by: bobcatalog on July 30, 2008, 12:07:26 PM
Hi,

Find out the date of job.txt.
If the date is yesterdays or today’s date then it needs to tell the file is up to date.
if it is lesser than the yesterdays date it should send a mail to the user that it is not up to date.
 
for /f %a in ('dir /b job.txt') do echo %~ta gives the date of the file.

How to write the above conditions?

Thanks,
-Bob
Title: Re: Need help with a batch job.
Post by: Dias de verano on July 30, 2008, 01:42:33 PM
How do your file dates show up? Using your code I see file dates like this (non-US date format, non-US (24 hour) time format.

30/07/2008 20:37
Title: Re: Need help with a batch job.
Post by: bobcatalog on July 30, 2008, 02:24:02 PM
C:\>echo 07/11/2008 04:16 PM
07/11/2008 04:16 PM

I was just trying with that  :) , you can ignore that.
Title: Re: Need help with a batch job.
Post by: Dias de verano on July 30, 2008, 03:47:06 PM
OK so you use US date format.

The idea is to get yesterday's date, and the filedate, into YYYYMMDD format, i.e. numbers where a later date is always greater than an earlier one, then see if the filedate number is less than the yesterday's date number.


Code: [Select]
@echo off
setlocal

REM Get yesterday's date into yyyymmdd format
call :get_date
REM Strip leading zeros from possible octals and decrement the day
set /a mm=1%mm%-100, dd=1%dd%-101
if %dd% NEQ 0 goto :add_zeros

REM Today is the 1st of the month - decREMent the month
REM and set leap year check (ignoring centuries)
set /a mm-=1,ly=yy%%4
REM If today is 1 Jan, set date to 31st Dec
if %mm% EQU 0 (set /a dd=31, mm=12, yy-=1) else (
  REM Calculate days in last month (by Frank Westlake)
  set /a "dd=5546>>mm&1,dd+=30"
  REM Special case for February
  if %mm% EQU 2 if %ly% EQU 0 (set dd=29) else (set dd=28)
)

:add_zeros
if %dd% LSS 10 set dd=0%dd%
if %mm% LSS 10 set mm=0%mm%

set /a yestdate=%yy%%mm%%dd%

for /f %%a in ('dir /b job.txt') do set fd=%%~ta
REM get filedate into yyyymmdd format
REM This is for US date format i.e. mm/dd/yyyy
set mm=%fd:~0,2%
set dd=%fd:~3,2%
set yy=%fd:~6,4%
set /a filedate=%yy%%mm%%dd%

IF %filedate% LSS %yestdate% del jobs.txt
goto next

REM ------------------------------------------------------------------
:Get_Date
REM ------------------------------------------------------------------
REM Generic date parser
REM Sets %dd% (01-31), %mm% (01-12) & %yy% (4 digit)

if "%date%A" LSS "A" (set toks=1-3) else (set toks=2-4)
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('echo:^|date') do (
  for /f "tokens=%toks% delims=.-/ " %%d in ('date/t') do (
    set %%a=%%d
    set %%b=%%e
    set %%c=%%f
    set toks=
  )
)
if %yy% LSS 100 set yy=20%yy%
goto :eof

:next
Title: Re: Need help with a batch job.
Post by: bobcatalog on July 31, 2008, 06:46:12 AM
Thanks for the script. Will check that and update you.

Looks like the script deletes the file, if its older than y'day ?
But instead i need to send an email to a person saying its an older file than y'day. can this be done ?

Appreciate your help.
Title: Re: Need help with a batch job.
Post by: Dias de verano on July 31, 2008, 06:49:23 AM
Substitute the command you would use to send the email for 'del jobs.txt'

Quote
IF %filedate% LSS %yestdate% del jobs.txt