Last User Posted on Forum

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Last User Posted on Forum

#1 Post by abc0502 » 19 Feb 2013 06:15

Edited: 21 Feb 2013

Last User Posted Batch File
It Checks for the last User posted on this forum and report back every 5 minutes

Code: Select all

@Echo OFF
Title DosTips.com - Last User Posted
Mode 70,7

REM ===[ settings ]==========================
SET "Repeat=yes"
SET "DelayTime=300"
SET "OutHTML=%temp%\Page_LUP.html"
SET "IndexPage=http://www.dostips.com/forum/index.php"
REM =========================================

SETLOCAL EnableDelayedExpansion

:Loop
REM ===[ Download Index Page ]===============

CALL :Download "%IndexPage%" "%OutHTML%"

Rem 3rd "topicdetails" where time of last post exist
Rem 4th "topicdetails" where last user posted exist
SET "Found=0"
FOR /F "tokens=1-2* delims=>" %%A IN ('FINDstr "topicdetails" "%OutHTML%"') DO (
   SET "Line=%%A%%B%%C"
   IF "!ErrorLevel!" == "0" SET /A Found += 1
   IF "!Found!" == "3" (
      SET "TMP1=%%B"
      SET "CurrTimeStamp=!TMP1:~0,-3!"
   )
   IF "!Found!" == "4" (
      SET "TMP2=%%C"
      SET "LastUser=!TMP2:~0,-4!"
   )
)


Rem ===[ Check for 1st or after 1st run ]====
IF "!TimeStampOld!" == "" (
   Echo.&Echo.
   Echo  Last User Posted was : !LastUser!    [!CurrTimeStamp!]
   CALL :Read "Last User Posted Was !LastUser!"
   SET "TimeStampOld=!CurrTimeStamp!"
   ) ELSE (
      IF NOT "!TimeStampOld!" == "!CurrTimeStamp!" (
         CLS
         Echo.&echo.
         Echo  Last User Posted Was : !LastUser!    [!CurrTimeStamp!]
         CALL :Read "Last User Posted was !LastUser!"
         SET "TimeStampOld=!CurrTimeStamp!"
      )
)


REM ===[ Delete Files ]======================
DEL /F /Q "%OutHTML%" >NUL


REM ===[ Exit or Loop ]======================
Call :Delay "5"
IF /I "%Repeat%" == "yes" (
   CALL :Delay "%DelayTime%"
   Goto :Loop
   )
Exit /B


:Download <URL> <out_file>
IF NOT EXIST "%temp%\Download.vbs" (
   (For /F "tokens=1*" %%A In ('FINDstr "^:Download: " ^< "%~F0"') DO Echo.%%B)>"%temp%\Download.vbs"
)
CScript //nologo "%temp%\Download.vbs" "%~1" "%~2"
GOTO :EOF
:Download: Set objArgs = WScript.Arguments
:Download: url = objArgs(0)
:Download: pix = objArgs(1)
:Download: With CreateObject("MSXML2.XMLHTTP")
:Download:  .open "GET", url, False
:Download:  .send
:Download:  a = .ResponseBody
:Download:  End With
:Download:  With CreateObject("ADODB.Stream")
:Download:  .Type = 1 'adTypeBinary
:Download:  .Mode = 3 'adModeReadWrite
:Download:  .Open
:Download:  .Write a
:Download:  .SaveToFile pix, 2 'adSaveCreateOverwrite
:Download:  .Close
:Download:  End With


:Read <text>
rem Create the VBScript, if not exist
IF NOT EXIST "%temp%\Voice.vbs" (
   (FOR /F "tokens=1*" %%a IN ('findstr "^:Voice: " ^< "%~F0"') DO Echo.%%b)>"%temp%\Voice.vbs"
)
Cscript //nologo "%temp%\Voice.vbs" "%~1"
GOTO :EOF
:Voice: Dim message, sapi
:Voice: Set sapi=CreateObject("sapi.spvoice")
:Voice: sapi.Speak chr(34) & WScript.Arguments(0) & chr(34)


:Delay <sec>
Ping LocalHost -n %~1 >Nul
GOTO :EOF




REM Leave Empty Line After This Line





Few Notes:
>Don't Forget to leave Empty line at the end of this batch.
>if you want it to check only once, change yes [at Repeat variable]to no or any thing else.
>Default time is 300 sec, you can change it in DelayTime Variable.


Next One will check for topics :)



Topic Alarm Batch Script
This One, will help you in keeping track of a specific topics [ In First Forum Page ] you want to follow every 5 minutes.

You enter all the topics names in "TopicList.ini" file and it will inform you if there is any new posts in these topics.
If the topic is no longer in the "1st Forum page", it will report back and will remove that topic name from your topic list file and put it in a txt file "Topics_No_Longer_Available.txt".
The batch keep track of each previous check, so if you closed the batch and re-run it, it will continue and check if there was any posts happened since the last check.

Code: Select all

@Echo OFF
Color 07
Title DosTips.com - Topic Alarm

REM =====================================================[ Settings ]=======================================================
SET "Repeat=Yes"                                 %= Tell batch file to keep looping or not [ yes/no ] =%
SET "DelayTime=300"                                 %= Time to wait between each check [ 5 minutes ] =%
SET "MaxTopic=50"                                 %= Each Page Has 50 Topic Max, Can Set Less But Not More =%
SET "NotAvailableFile=Topics_No_Longer_Available.txt"      %= File Contain Removed Topic Names from Topic List File =%
SET "Record=Record.rec"                              %= This will hold hisory of the topics =%
SET "TopicList=TopicList.ini"                        %= Here you set the topics you want to keep track of =%
SET "TMPTList=TopicList.tmp"                        %= Temporary File for holding available topic names =%
SET "LOG=%temp%\log.csv"                           %= Temporary files that will hold a html page in csv format =%
SET "TMPRecord=record.tmp"                           %= Holds a temporary history of the topics until process finish =%
SET "VBSdownload=%temp%\download.vbs"                  %= Necessary for downloading Index page =%
SET "VBSvoice=%temp%\Voice.vbs"                        %= Necessary for warnning =%
SET "TLpage=%temp%\TopicList_Page.html"                  %= The Index Page =%
SET "URL=http://www.dostips.com/forum/viewforum.php?f=3"   %= Index Page URL =%
REM ========================================================================================================================


REM =====[ Remove Temp File if exist ]===============
IF EXIST "%TMPRecord%" DEL /F /Q "%TMPRecord%" >NUL
IF EXIST "%LOG%" DEL /F /Q "%LOG%" >NUL
IF EXIST "%TLpage%" DEL /F /Q "%TLpage%" >NUL
IF Exist ""%TMPTList%"" DEL /F /Q "%TMPTList%" >NUL


REM Check for TopicList If exist or if has Zero size.
IF NOT EXIST "%TopicList%" rem.>"%TopicList%" & GOTO :TL_Error
For /F "delims=" %%A In ("%TopicList%") Do IF "%%~zA" == "0" GOTO :TL_Error


REM Create Record File if not Exist, Then Process the Log File
IF NOT EXIST "%Record%" rem.>"%Record%"


SETLOCAL EnableDelayedExpansion


:Loop

REM =====[ Downloading Topic Page ]==================
CALL :Download "%URL%" "%TLpage%"

REM Process the html file and output the LOG file
Rem each "topictitle" word represent a topic
Rem and each Topic has 4 "topicdetail" words represent replies, views, last post time and last user posted (in order)
SET "Found=0"
FOR /F "tokens=1-2* delims=>" %%A IN ('FINDstr "topictitle" "%TLpage%"') DO (
   IF "!ErrorLevel!" == "0" (
      IF "!Found!" == "!MaxTopics!" Goto :ExitCommand
      SET /A Found += 1
      SET "TMP=%%B"
      SET "TopicTitle=!TMP:~0,-3!"
      SET "TopicNumber=!Found!"
      Rem This Function will output the LOG file at it's end.
      CALL :GetDetails "!TopicNumber!"
   )   
)

:ExitCommand
Echo.
SET "NotExistFlag=0"

Rem Get Topic Names and check if they exist or not in Log File or in Record file or in both
For /F "delims=" %%A In ('Type "%TopicList%"') Do (
   SET "TopicName=%%A"
   
   Rem Check if Topic Name Exist in Log File
   SET "LogFlag=No"
   For /F "tokens=* delims=" %%a In ('FINDstr /L /C:"!TopicName!" "%LOG%"') Do (
      IF "!Errorlevel!" == "0" (
         SET "LogLine=%%a"
         SET "LogFlag=Yes"
      )
   )
   
   Rem Check if Topic Name Exist in Record File
   SET "RecordFlag=No"
   For /F "tokens=* delims=" %%a In ('FINDstr /L /C:"!TopicName!" "%Record%"') Do (
      IF "!Errorlevel!" == "0" (
         SET "RecordLine=%%a"
         SET "RecordFlag=Yes"
      )
   )
   
   Rem Her we Copmare:
   Rem First Case (yes/yes) exist in both Log and Record File
   IF /I "!LogFlag!" == "Yes" (
      IF /I "!RecordFlag!" == "Yes" (
         Rem Sub-Case 1: Topics has no changes
         IF /I "!LogLine!" == "!RecordLine!" (
            Echo !RecordLine!>>"%TMPRecord%"
         )
         Rem Sub-Case 2: Topics has changes
         IF /I NOT "!LogLine!" == "!RecordLine!" (
            Echo !LogLine!>>"%TMPRecord%"
            For /F "tokens=1-3* delims=_" %%A In ("!LogLine!") Do (
               Echo  [%%C]   Topic: %%A.    [%%D]
               CALL :VisualAlarm "10" "07"
               CALL :Read "Topic: %%A, Has A New Post"
            )
         )
      )
   )
   
   Rem Second Case (yes/no) exist in log file but not in Record file
   IF /I "!LogFlag!" == "Yes" (
      IF /I "!RecordFlag!" == "No" (
         Echo !LogLine!>>"%TMPRecord%"
         For /F "tokens=1-3* delims=_" %%A In ("!LogLine!") Do (
            Echo  [%%C]   Topic: %%A.    [%%D]
            CALL :VisualAlarm "10" "07"
            CALL :Read "Topic: %%A, Has A New Post"
         )
      )
   )
   
   Rem Third Case (no/yes) doesn't exist in log file but exist in record file
   IF /I "!LogFlag!" == "No" (
      IF /I "!RecordFlag!" == "Yes" (
         Echo !RecordLine!>>"%TMPRecord%"
      )
   )
   
   Rem Fourth Case (no/no) doesn't exist in both log and record file
   IF /I "!LogFlag!" == "No" (
      IF /I "!RecordFlag!" == "No" (
         SET "NotExistFlag=1"
         Rem Remove un available topics from topic list
         CALL :CleanTopicList "!TopicName!" "!TopicList!"
         Echo  [  Not Available  ]   Topic: !TopicName!.
         CALL :VisualAlarm "10" "07"
         CALL :Read "Topic: !TopicName!, is No Longer Available"
      )
   )
)

Rem Delete Old Record File and Rename The TMP Record File with it's name
For /F "delims=" %%A In ("%Record%") Do (
   DEL /F /Q "%Record%" >NUL
   REN "%TMPRecord%" "%%~nxA"
)
   
REM =====[ Clean Temp Files ]==================
DEL /F /Q "%TLpage%" "%LOG%" >NUL

REM ===[ Exit or Loop ]======================
Call :Delay "5"
IF /I "%Repeat%" == "yes" (
   CALL :Delay "%DelayTime%"
   Goto :Loop
   )
CALL :Delay "30"
Exit /B

REM ========================================================================================================================
REM ========================================================================================================================

:GetDetails <Topic_Number>
REM Each topic has 4 "topicdetails",
Rem   1st: replies number,
Rem   2nd: views number,
Rem   3rd: last post time,
Rem   4th: last user posted

Rem Input
SET "TopicNumber=%~1"

Rem Calculate detail numbers for current topic
SET /A DetailNumber_replies = TopicNumber * 4 - 3
SET /A DetailNumber_viewNum = TopicNumber * 4 - 2
SET /A DetailNumber_postime = TopicNumber * 4 - 1
SET /A DetailNumber_usrname = TopicNumber * 4

Rem extract reply, views, time and user that belong to each topic
SET "TDnumber=0"
For /F "tokens=1-2* delims=>" %%A In ('FINDstr /I "topicdetails" "%TLpage%"') Do (
   SET /A TDnumber += 1
   REM Replies Number
   IF "!TDnumber!" == "!DetailNumber_replies!" (
      SET "TMP1=%%C"
      SET "Reply=!TMP1:~0,-9!"   )
   REM Views Number
   IF "!TDnumber!" == "!DetailNumber_viewNum!" (
      SET "TMP2=%%C"
      SET "Views=!TMP2:~0,-9!"   )   
   REM Post Time
   IF "!TDnumber!" == "!DetailNumber_postime!" (
      SET "TMP3=%%B"
      SET "Time=!TMP3:~0,-3!"   )
   REM User Name
   IF "!TDnumber!" == "!DetailNumber_usrname!" (
      SET "TMP4=%%C"
      SET "User=!TMP4:~0,-4!"   )
)
Rem Create LOG file
Echo !topicTitle!_!Reply!_!Time!_!User!>>"%LOG%"
Goto :EOF



:CleanTopicList <Word> <TopicList_File>
SET "Word=%~1"
SET "File=%~2"
Rem Get Un available topic name line number
For /F "skip=2 tokens=1 delims=[]" %%A In ('FIND /N "%Word%" "%File%"') Do SET "Topic_LineNum=%%A"

Rem Remove un-available topiclist name from TopicList file
SET "Line=0"
For /F "delims=" %%A In ('TYPE "%File%"') Do (
   SET /A Line += 1
   IF "!Line!" == "%Topic_LineNum%" Echo %%A>>"%NotAvailableFile%"
   IF NOT "!Line!" == "%Topic_LineNum%" Echo %%A>>"%TMPTList%"
   )
REM Remove old TopicList and rename Tmp Topic List File to TopicList file name
For /F "delims=" %%A In ("%File%") Do (
   DEL /F /Q "%%A" >NUL
   Ren "%TMPTList%" "%%~nxA" >NUL
   )
GOTO :EOF



:Download <URL> <out_file>
IF NOT EXIST "%VBSdownload%" (
   (For /F "tokens=1*" %%A In ('FINDstr "^:Download: " ^< "%~F0"') DO Echo.%%B)>"%VBSdownload%"
)
CScript //nologo "%VBSdownload%" "%~1" "%~2"
GOTO :EOF
:Download: Set objArgs = WScript.Arguments
:Download: url = objArgs(0)
:Download: pix = objArgs(1)
:Download: With CreateObject("MSXML2.XMLHTTP")
:Download:  .open "GET", url, False
:Download:  .send
:Download:  a = .ResponseBody
:Download:  End With
:Download:  With CreateObject("ADODB.Stream")
:Download:  .Type = 1 'adTypeBinary
:Download:  .Mode = 3 'adModeReadWrite
:Download:  .Open
:Download:  .Write a
:Download:  .SaveToFile pix, 2 'adSaveCreateOverwrite
:Download:  .Close
:Download:  End With



:Read <text>
rem Create the VBScript, if not exist
IF NOT EXIST "%VBSvoice%" (
   (FOR /F "tokens=1*" %%a IN ('findstr "^:Voice: " ^< "%~F0"') DO Echo.%%b)>"%VBSvoice%"
)
Cscript //nologo "%VBSvoice%" "%~1"
GOTO :EOF
:Voice: Dim message, sapi
:Voice: Set sapi=CreateObject("sapi.spvoice")
:Voice: sapi.Speak chr(34) & WScript.Arguments(0) & chr(34)



:Delay <sec>
Ping LocalHost -n %~1 >Nul
GOTO :EOF



:VisualAlarm <blink_number> <base_Color>
For /L %%@ in (1 1 %~1) Do (
   color 0C
   ping localhost -n 1 >nul
   color %~2
   ping localhost -n 1 >nul
)
Goto :EOF



:TL_Error    [Topic List File Errors]
Echo.&Echo.
Color 80
Echo       Please Edit The TopicList.ini File and Add Topic Names to Follow.
Call :Read "Please Edit The TopicList File . and Added Topic Names to Follow."
Start "" "%TopicList%"
Pause
CLS
GOTO :EOF

REM Always Leave Empty Line After This One.




Important Notes:
> Put the batch in a folder as it will create extra files ( 2 will always exist and other will be temporary and will be deleted ).
> In the Topic List File don't leave empty lines between topic names and each topic in a separate line. (it causes a delay in discovering unavailable topic names ).
> On the first run it will create "topiclist.ini" file and open it for you to enter topic names, Also will create Record.rec file.
> On the first check it will inform you that there is a new posts (in your Topic lists) even if there wasn't ( just on first time )
> Don't Forget to leave empty line at the end of the batch ( or the batch will hang )
> Avoid any extra leading or trialling spaces in topic names when you put them in the "TopicList.ini" file.
> Finally, Errors will happen when a topic names is 1 to 3 characters long, or contain underscore sgin _ ( _ sign is used to separate data in the log.csv file )


I hope you like it :D


If This will cause any problems to the website, The administrators can remove this posts any time, but let us know to stop using it.
Last edited by abc0502 on 21 Feb 2013 08:17, edited 3 times in total.

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

Re: Last User Posted on Forum

#2 Post by foxidrive » 19 Feb 2013 06:24

Nice work. :)

Ocalabob
Posts: 79
Joined: 24 Dec 2010 12:16
Location: Micanopy Florida

Re: Last User Posted on Forum

#3 Post by Ocalabob » 19 Feb 2013 07:38

Greetings abc0502,
Works great for me, good job. I tested it using WIN 7 and XP3.

Thank you!

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Last User Posted on Forum

#4 Post by abc0502 » 19 Feb 2013 12:09

I'm glade you liked it, i will post a new one soon to check for specific topics the user specify and report back if any new posts was made.
but it was too easy when i was thinking about it :D

mfm4aa
Posts: 70
Joined: 13 Feb 2013 14:02
Location: Europe

Re: Last User Posted on Forum

#5 Post by mfm4aa » 19 Feb 2013 12:43

You should put

Code: Select all

SETLOCAL EnableDelayedExpansion
outside the loop. I got a stack overflow.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Last User Posted on Forum

#6 Post by abc0502 » 19 Feb 2013 12:56

I never had this before but it's a good idea :)
thanks

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

Re: Last User Posted on Forum

#7 Post by foxidrive » 20 Feb 2013 12:19

Just confirming, with the setlocal inside the loop I eventually got this message repeated:

Maximum setlocal recursion level reached.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Last User Posted on Forum

#8 Post by Ed Dyreen » 20 Feb 2013 14:57

Hi abc,

This is just for fun right ? I mean already u use VBS and there are other languages that support the IE DOM model.
But it's nice to see how u use several externalities to get it done in batch.

See ya :wink:

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Last User Posted on Forum

#9 Post by abc0502 » 21 Feb 2013 06:32

@Ed Dyreen
Thanks, i made it because when i 'm waiting for an answer or a reply from a user and busy or doing other things on my computer it warn me when a post come and i don't have to keep reloading the page every while.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Last User Posted on Forum

#10 Post by abc0502 » 21 Feb 2013 08:18

Posted The Topic Alarm Batch File, up in my first post.
Hope you like it :)

Post Reply