0

Awesome work guys by guys in this post on this site.
But I need modifying this script according to my needs.

I am not hardcore coder but with little help I can get it working.


I have website it has timestamp in HH:MM AM/PM format.
So this is what is on my website.

Last Updated as of 11:14 pm March 11th

I would like to parse 2:14 PM and compare it(subtract it) with current time stamp, and get the minutes. if difference is greater than 30 mins then I want it do something.

Can someone help me with this? Thanks


I am trying best to understand and modify this code.
FYI Google.com is just example, instead of google it would be link to mysite.

@echo off
setLocal EnableDelayedExpansion
set curTimestamp=%date:~7,2% %date:~3,3%_%date:~10,4%_%time:~0,2%_%time:~3,2%
FOR /F "TOKENS=*" %%A IN ('TIME/T') DO SET Now=%%A


for %%F in (q r s t u v w x y z) do if exist %%F del %%F


wget <a href="http://google.com" target="_blank" rel="nofollow">http://google.com</a>

find "Updated" < index.html > q


for /f "tokens=1-4 delims==" %%a in (q) do (
echo %%d >> r
)
for /f "tokens=2-3 delims=>" %%a in (r) do (
echo %%a %%b >> s
)
for /f "tokens=1-2 delims=<" %%a in (s) do (
echo %%a %%b >> t
)
for /f "tokens=1-2 delims=^/" %%a in (t) do (
echo %%a %%b >> u
)
for /f "tokens=1,3 delims= " %%a in (u) do (
echo %%b %%a >> v
)
for /f "tokens=1-4 delims=- " %%a in (v) do (
echo %%c %%b %%a %%d >> w
)
for /f "tokens=* delims= " %%a in (w) do (
set str=%%a
set str=!str:Jan=01!
set str=!str:Feb=02!
set str=!str:Mar=03!
set str=!str:Apr=04!
set str=!str:May=05!
set str=!str:Jun=06!
set str=!str:Jul=07!
set str=!str:Aug=08!
set str=!str:Sep=09!
set str=!str:Oct=10!
set str=!str:Nov=11!
set str=!str:Dec=12!
echo !str! >> x
)
sort < x > y
for /f "tokens=4 delims= " %%a in (y) do (
set AVG=%%a
)
echo wget www.google.com
echo %curTimestamp%
ECHO %Now%
::for %%F in (q r s t u v w x y z) do if exist %%F del %%F
Pause
Mowgli
  • 3,422
  • 21
  • 64
  • 88
  • 1
    Why are you trying to kill yourself using batch 'scripting'?? Can you use *anything* else?? PowerShell? something? – PenguinCoder Mar 12 '13 at 15:19
  • I didn't know about them :), is it similar? I am guess better, fyi I am running Windows 7. – Mowgli Mar 12 '13 at 15:21
  • If you have windows 7, then you can use [powershell](http://technet.microsoft.com/en-us/scriptcenter/powershell.aspx). Though you still would be better off with a simple, none batch, scripting setup. Look into [AutoIt](http://www.autoitscript.com/site/)? – PenguinCoder Mar 12 '13 at 15:25
  • Thanks a lot for info on powershell. Yes I have used AutoHotKey and others also, I currently do this in Excel VBA but I need better option. AHK is not stable. it require installation of extra app(ahk), I want to use built in tools only such as batch or now powershell. Thanks. – Mowgli Mar 12 '13 at 15:30
  • You can build a standalone app with Autoit. I'm not talking AutoHotKey. Check the link above, and [this one](http://www.autoitscript.com/autoit3/docs/intro/compiler.htm). No other installation necessary. Personally, I'd go with a bash/curl/wget command line script for something like this. Don't think you have that option on Windows though. – PenguinCoder Mar 12 '13 at 15:41
  • I don't have Linux system, I would do it, if I had. I am giving PowerShell a chance, and I already have wget on windows 7 added. Hopefully this should work, if not I'll go with AutoiT. – Mowgli Mar 12 '13 at 15:46

2 Answers2

2

Here's a batch script / JScript hybrid using JScript's ability to perform math with dates. Since the web page never specifies, this script assumes the year is this year, so you might get unexpected results running this at the beginning of January if the document was last modified in December. Anyway, here.

@if (@X)==(@Y) @end /* (batch + jscript hybrid script init)

:: *** Batch script *****

@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%I in ('wget "%~1" -O- -q 2^>NUL ^| findstr /i "last.*updated.*as.*of"') do (
    for /f "delims=" %%x in ('cscript /nologo /e:jscript "%~f0" "%%I"') do (

        rem test whether date diff >= 30 minutes
        set /a "thirtyMinutes = 30 * 60 * 1000"
        if %%x GEQ !thirtyMinutes! (
            echo Do that voodoo that you do.
        )

        rem Just to demonstrate, you can do some maths to make further sense of the date difference.
        rem set milliseconds=%%x
        set /a "seconds = %%x / 1000, seconds %%= 60"
        set /a "minutes = %%x / 1000 / 60, minutes %%= 60"
        set /a "hours = %%x / 1000 / 60 / 60, hours %%= 24"
        set /a "days = %%x / 1000 / 60 / 60 / 24"
        echo %~1 last modified !days! days !hours! hours !minutes! minutes ago.
    )

    rem Once the for loop has fired, exit.
    exit /b
)

rem In case the web page does not contain "last updated as of"
exit /b


:: *** JScript script *****/
var args = [];
for (var i=0; i<WScript.arguments.length; i++) { args.push(WScript.arguments(i)) }
var t = args.join(' ').replace(/^\s+|<[^>]+>|\s+$/g,'').replace(/\&nbsp;/g, ' ').split(' ');
var h = t[4].split(':')[0];
if (/pm/i.test(t[5])) h = h * 1 + 12;
var ds = t[6] + ' ' + t[7] + ', ' + new Date().getFullYear() + ' ' + h + ':' + t[4].split(':')[1];
var diff = new Date() - new Date(ds);
WScript.echo(diff);

Example output:

C:\Users\me\Desktop>test.bat http://stackoverflow.com/questions/15364653/
Do that voodoo that you do.
http://stackoverflow.com/questions/15364653/ last modified 0 days 23 hours 18 minutes ago.
rojo
  • 24,000
  • 5
  • 55
  • 101
  • Seriously, I have no words to Thank you enough, what you done is totally brilliant. I never imagined this could be even possible.I love you man. I just have one problem with my weburl. it has space between the html file name. example `www.mysite.com/this folder\index.html` any suggestions? – Mowgli Mar 12 '13 at 18:04
  • Try it with `http://www.mysite.com/this%%20folder/index.html` (replacing the space with `%%20`). Either that or enclose your URL in quotation marks. – rojo Mar 12 '13 at 18:05
  • I tried that it didn't work, I am going to try creating test html page on my deskop and run that. – Mowgli Mar 12 '13 at 18:13
  • I did an edit a few minutes ago adding quotation marks around `"%~1"` in the `wget` line. Does the revision with which you are testing include that? If this isn't helpful, if you don't mind, pastebin some example html source and let me check whether I'm scraping it correctly. – rojo Mar 12 '13 at 18:22
  • I can paste html code in pastbin, but do you think it will work if `\this folder\index.html' is used iframe in ` www.mysite.com\something\somethin2\index.html` ? this one didn't pull data either. – Mowgli Mar 12 '13 at 18:31
  • 1
    @Mowgli - The problem is not that your URL contains a space. The problem is that your HTML has a buttload of non-breaking spaces (` `) instead of spaces spaces. That's why it didn't work. I updated my code to translate ` ` to a space. Give it another shot with my updated code. – rojo Mar 12 '13 at 19:10
  • Worked like a charm now :) Thanks man, now I will notified if my site stops updating. I will end up throwing auto email or something if it has been over 30 mins since last update. You really helped me a lot :) – Mowgli Mar 12 '13 at 19:17
1
@echo off
set timeStamp=Last Updated as of 2:14 pm March 11th

rem Convert time of timeStamp to number of minutes
for /F "tokens=5-7 delims=: " %%a in ("%timeStamp%") do (
   set /A minutes=%%a*60 + 1%%b%%100
   if %%c equ pm set /A minutes+=12*60
)

rem Subtract that number of minutes from current time
for /F "tokens=1-2 delims=:" %%a in ("%time%") do (
   set /A minutes=%%a*60 + 1%%b%%100 - minutes
)

rem If time lapse is negative, timeStamp was taken from previous day
if %minutes% lss 0 (
   set /A minutes+=24*60
)

if %minutes% gtr 30 (
   echo Do something...
)
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Really awesome work!!! I think I can use this code after I am successfully able to get `Last Updated as of 2:14 pm March 11th` from url, – Mowgli Mar 12 '13 at 16:13