WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Samples from Users
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Read Local LAN News of the Day Sample Script

Keywords: 	  read news

This script was donated by Paul Howe at Northwest Airlines.

It's a newsreader for news on their local LAN, and looks in their registry under a key they custom create under HKCU\Software\NWA (stands for NW Airlines).

Maybe it'll prove useful to someone else along the way...

; News of the Day program
; Paul Howe, Northwest Airlines, Inc.

; Display "News of the Day" message the current user has not yet seen.
; Store registry key to hold the date stamp of the last news item seen.

; Program has two modes:
;	News of the Day - run from login script and displays any news items
;                     not yet seen the by the current user.
;   News History    - allows the user to browse past news items
;                     they can view or print the news items

; Constants

NewsPath="C:\temp\"		; defaults, set in ParseParams
NewsFiles="*.txt"

cAppTitle="NWA LAN - News of the Day v1.0"
cNewsOfTheDay=1
cNewsHistory=2

NewsKey="software\nwa\LanNews"						; our key
NewsReadValue="software\nwa\LanNews[LastRead]"		; our value
OldDateStamp="1990:01:01:00:00:01"					; a very old date

Gosub ParseParams									; read command line, setup defaults, set viewing mode

If NewsMode == cNewsOfTheDay then
	Gosub NewsOfTheDay								; new news only
Else
	GoSub NewsHistory								; let the user pick news items to view/print
Endif
Exit

; ******************************************************************

:NewsOfTheDay

; Display all new news items.

; Is the registry setup?
If !RegExistValue(@RegCurrent,NewsReadValue) then
	Ok=RegCreateKey(@RegCurrent,NewsKey)
	Ok=RegSetValue(@RegCurrent,NewsReadValue,OldDateStamp)
endif

Gosub CreateNewsList

;	Date/Time in YmdHms of last news item read
NewsLastRead=RegQueryValue(@RegCurrent,NewsReadValue)

ONCANCEL="NewsOfTheDaySkip"

For i= 1 to ItemCount(FileTimeList,@Tab)
	FileTimeItem=ItemExtract(i,FileTimeList,@tab)
	Timestamp=ItemExtract(1,FileTimeItem," ")		; the TimeStamp
	Offset=ItemExtract(2,FileTimeItem," ")			; the offset in original list

	; Is the file newer than the last news item read?

	If TimeDiffSecs(TimeStamp,NewsLastRead) > 0 then
		; yes, display the file...
		NewsFile=StrCat(NewsPath,ItemExtract(Offset,NewsFileList,@tab))
		; set AskFileText window a little smaller than display - nearly full screen
		IntControl(63, 10,10,900,900)
		a=AskFileText(Strcat(cAppTitle," ",NewsFile," ",TimeStamp), NewsFile, @UnSorted, @Single)
		IntControl(63,0,0,0,0)
		; update registry with new timestamp
		Ok=RegSetValue(@RegCurrent,NewsReadValue,TimeStamp)
	Endif
Next I

:NewsOfTheDaySkip
Return

; *******************************************************************************************************

:NewsHistory

; Let the user select news items to be viewed or printed

cExit=2		; exit dialog
cDisplay=1	; display button
cPrint=3	; print button

Gosub CreateNewsList
;
MasterList = "       Date     Index                        Description%@Tab%"
For i= 1 to ItemCount(FileTimeList,@Tab)
	FileTimeItem=ItemExtract(i,FileTimeList,@tab)
	Timestamp=ItemExtract(1,FileTimeItem," ")		; the TimeStamp
	Timestamp=StrReplace(StrSub(Timestamp,1,10),":","/")
	Offset=ItemExtract(2,FileTimeItem," ")			; the offset in original list
	FileName=ItemExtract(Offset,NewsFileList,@Tab)
	
	; Get first line in file as title text
	Fptr=FileOpen(StrCat(NewsPath,FileName),"Read")
	FirstLine=FileRead(Fptr)					   
	FileClose(Fptr)

	sOffSet=StrFix(Offset," ",4)
	MasterList = StrCat(MasterList,TimeStamp," ",sOffSet,"   ",StrSub(FirstLine,1,50),@Tab)
Next I

NewsFormat=`WWWDLGED,5.0`

; Setup static dialog box items
NewsCaption=cAppTitle
NewsX=103
NewsY=44
NewsWidth=268
NewsHeight=200
NewsNumControls=7

News01=`4,4,60,DEFAULT,STATICTEXT,DEFAULT,"Historical News Display"`
News02=`8,178,38,DEFAULT,PUSHBUTTON,DEFAULT,"&Display",1`
News03=`210,178,38,DEFAULT,PUSHBUTTON,DEFAULT,"E&xit",2`
News04=`52,178,38,DEFAULT,PUSHBUTTON,DEFAULT,"&Print",3`
News05=`8,20,116,DEFAULT,STATICTEXT,DEFAULT,"Please select the news items you what to read."`
News06=`8,30,116,DEFAULT,STATICTEXT,DEFAULT,"You may select more than one item."`

Done=@False
ONCANCEL="HistNewsSkip"

While !Done
	; need to recreate the LIST variable after each call to Dialog
	; as LIST will return the (smaller) list of items selected.
	List=MasterList
	News07=`6,44,252,132,ITEMBOX,List,DEFAULT`
	ButtonPushed=Dialog("News")
	
	Select ButtonPushed
		Case cPrint
			NewsOutput=cPrint
			Gosub DisplayPrintNews
			Break
		Case cDisplay
			NewsOutput=cDisplay
			Gosub DisplayPrintNews
			Break
		Case cExit
			Done=@True
			Break
	EndSelect
EndWhile

:HistNewsSkip
Return

; *******************************************************************************************************

:DisplayPrintNews

; display or print the news items selected in NewsHistory routine
ONCANCEL="DisplayPrintNewsSkip"

For i = 1 to ItemCount(List,@tab)
	CurNewsItem=ItemExtract(i,List,@tab)
	Offset=ItemExtract(2,CurNewsItem," ")
	If IsNumber(Offset) then
		NewsFileName=ItemExtract(Offset,NewsFileList,@Tab)
		NewsFileNamePath=StrCat(NewsPath,NewsFileName)
		If NewsOutput == cDisplay then
			IntControl(63, 10,10,900,900)
			IntControl (49, 2, 1, 0, 0)
			a=AskFileText(Strcat(cAppTitle," ",NewsFileName," ",TimeStamp), NewsFileNamePath, @UnSorted, @Single)
			IntControl(63, 0,0,0,0)
		Endif
		If NewsOutput == cPrint then
			; print the file using the default print program for the file extension
			a=Print(NewsFileNamePath,"",@Hidden,@NoWait)
		Endif
			
	Endif
Next I

:DisplayPrintNewsSkip
Return
; *******************************************************************************************************

:Cancel
Goto %onCancel%


; *******************************************************************************************************
:CreateNewsList
;	Get a list of news files, sort them by file date/time
;	Two lists are created,
;		NewsFileList contains the file information
;		FileTimeList list contains the file date/time and an offset to the NewsFileList
;			         we can then sort FileTimeList, and use the offset to find the original filename

FileTimeList=""
NewsFileList = FileItemize(StrCat(NewsPath,NewsFiles))

; for each filename get its date/time stamp. Build a list.
; since we will sort this list on date/time, we need a pointer back
; to the origrinal file's offset in the list.  The FileTimeList
; is formated
;     timestamp  offset  @tab
;     timestamp2 offset2 @tab

; we can then use ItemExtract with a space delimiter to get the original offset.

For i = 1 to ItemCount(NewsFileList,@tab)
	Ftime=FileYmdHms(StrCat(NewsPath,ItemExtract(i,NewsFileList,@Tab)))
	FileTimelist=StrCat(fTime," ",i,@tab,FileTimeList)
Next i

;	Sort the list by Date/Time

FileTimeList=ItemSort(FileTimeList,@tab)

Return

; *******************************************************************************************************

:ParseParams

; Decide what command line parameters were passed

;	Calling format:  News.exe /? /hist /news /path:path_to_news
;		/?    - display help screen
;		/Hist - History mode
;		/News - News of the day mode (default)
;       /Path - path to news items, (default=Program path)
;
ParamError = @False
; default options are
;	/News  
;	/Path:curdirectory

NewsMode=cNewsOfTheDay
NewsPath=DirGet ( )

If Param0 != 0 then
	; parse parameters
	Msg="The following errors were found on the command line:%@crlf%%@Crlf%"
	For i = 1 to Param0
		CurSwitch=StrUpper(StrSub(Param%i%,1,5))
		If CurSwitch == "/HIST" then
			NewsMode=cNewsHistory
		endif
		If CurSwitch == "/NEWS" then
			NewsMode=cNewsOfTheDay
		Endif
		If CurSwitch == "/PATH" then
			n = StrIndex(Param%i%,":",1,@FwdScan)
			NewsPath=StrSub(Param%i%,n+1,StrLen(Param%i%)-n)
			If StrLen(NewsPath) == 0 then
				Msg=StrCat(Msg,"Missing news file path on /PATH:path_to_news option",@crlf)
				ParamError=@True
			Else
				IF !DirExist(NewsPath) then
					Msg=StrCat(Msg,"Can not locate news path specified in ",Param%i%," option",@crlf)
					ParamError=@True
				Endif
				If StrSub(NewsPath,StrLen(NewsPath),1) != "\" then
					NewsPath=StrCat(NewsPath,"\")
				Endif
			Endif
		Endif
		If Param%i% == "/?" || Param%i% == "-?" then
			Msg="News of the Day v1.0%@crlf%%@crlf%"
			Msg=StrCat(Msg,"This is a News of the day program used to display news items",@Crlf)
			Msg=StrCat(Msg,"to LAN users.  News items consist of ASCII text files.",@crlf,@crlf)
			Msg=StrCat(Msg,"Command line options:",@crlf,@crlf)
			Msg=StrCat(Msg,"  /?    - this help screen",@crlf,@crlf)
			Msg=StrCat(Msg,"  /HIST - history mode - display list of past and future news items",@crlf)
			Msg=StrCat(Msg,"          for the user to read.",@crlf,@crlf)
			Msg=StrCat(Msg,"  /News - News mode - display only those news items the user has not",@crlf)
			Msg=StrCat(Msg,"          yet read.  Last read date is stored in the Current users",@crlf)
			Msg=StrCat(Msg,"          registry at HKCU\SOFTWARE\NWA\LANNews.",@crlf,@crlf)
			Msg=StrCat(Msg,"  /Path:path_to_news - path to directory containing news text files.",@crlf)
			Msg=StrCat(Msg,"          This allows custom news folders for differnt user groups.",@crlf)
			Msg=StrCat(Msg,"          Default path is current application directory.",@crlf)
			ParamError=@True
		endif
	Next i
	If ParamError then
		Message(cAppTitle,Msg)
		Exit
	Endif
Endif
Return



Article ID:   W14958
File Created: 2001:11:08:12:41:08
Last Updated: 2001:11:08:12:41:08