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
6
  • 1
    Why are you trying to kill yourself using batch 'scripting'?? Can you use anything else?? PowerShell? something? Commented Mar 12, 2013 at 15:19
  • I didn't know about them :), is it similar? I am guess better, fyi I am running Windows 7. Commented Mar 12, 2013 at 15:21
  • If you have windows 7, then you can use powershell. Though you still would be better off with a simple, none batch, scripting setup. Look into AutoIt? Commented Mar 12, 2013 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. Commented Mar 12, 2013 at 15:30
  • You can build a standalone app with Autoit. I'm not talking AutoHotKey. Check the link above, and this one. 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. Commented Mar 12, 2013 at 15:41

2 Answers 2

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.
Sign up to request clarification or add additional context in comments.

7 Comments

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?
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.
I tried that it didn't work, I am going to try creating test html page on my deskop and run that.
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.
@Mowgli - The problem is not that your URL contains a space. The problem is that your HTML has a buttload of non-breaking spaces (&nbsp;) instead of spaces spaces. That's why it didn't work. I updated my code to translate &nbsp; to a space. Give it another shot with my updated code.
|
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...
)

1 Comment

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,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.