VFP: Fetch XML feed

Interpreting XML Feed in Visual Foxpro

Looking for ways to interpret XML files I stumbled on this article at the foxite.com forum. This was still way too difficult in my opinion. So I used the methods and created some lean code that does what I want in far less code.

This piece of program retrieves a feed from the Dutch public-services network and lists all items:

lcFeed = [http://feeds.livep2000.nl/]
lcFile = UrlToFile(lcFeed)

oXML = CREATEOBJECT("MSXML2.DOMDocument")
oXML.load(lcFile)
oItems = oXML.getElementsByTagName([item])
FOR EACH oItem IN oItems
 FOR EACH oLine IN oItem.childNodes
  ? oLine.nodeName+[: ]+oLine.text
 ENDFOR 
ENDFOR

VFP: Progress bar

VFP Progress bar using foundation class

Using the foundation class you can have a progress bar in no time. Hardest part is to locate the FFC folder, but most of the times it’s just beneath the vfp install program folder found with SYS(2004).

Half this code is for slowing down the demo:

* Progressbar with VFP Foundation Classes
* Source: https://degraafonline.com
* Free after Carl Warner's article at http://www.vfug.org/Newsletters/ThermometerBar.htm 

 lnPause = 2 && seconds between steps
 lcLibraryLocation = SYS(2004)+[FFC\]
 loTherm = NewObject([_thermometer],lcLibraryLocation+[_therm],[],[Progress Example])
 loTherm.Show
 =INKEY(lnPause,[HM])
 loTherm.Update(10) && just set the progress bar
 =INKEY(lnPause,[HM])
 loTherm.Update(20,[80 percent to go]) && set progress bar and show a subtitle
 =INKEY(lnPause,[HM])
 loTherm.Update(50,[half way])
 =INKEY(lnPause,[HM])
 loTherm.Complete([Ready])
 =INKEY(lnPause,[HM])
 loTherm.Release

VFP: Find IP by name

VfpNameLookup

For my network checks I want to ping some computers to see if they’re still active on my network. But before that, I have to lookup the IP numbers for the network names. Found this TekTips article on NSlookup from VFP6 and built my own function around the example.

* Lookup an IP number connected to a netwerk name
* (C)2011 Jasper de Graaf for J.A. Software
* Source: https://degraafonline.com 
* Inspiration from http://www.tek-tips.com/viewthread.cfm?qid=891380&page=338 

LPARAMETERS tcLookup && name to lookup

* No parameter, wrong type or empty 
IF PCOUNT()=0 OR !VARTYPE(tcLookup)=[C] OR EMPTY(tcLookup)
 ? [nslookup needs character parameter lookup network name or ip number]
 RETURN []
ENDIF 

* create scripting shell object and test
loShell = CreateObject("WScript.Shell")
IF VARTYPE(loShell)<>[O]
 ? [did not succeed in creating Wscript.Shell object]
 RETURN []
ENDIF 

* perform lookup and get results
loScriptExec = loShell.Exec("nslookup -debug "+tcLookup)
lcNsLookup = loScriptExec.StdOut.ReadAll()

IF AT([ANSWERS],lcNsLookup,2)>0 && we got a valid answer!
 lnIPpos = AT([Address:],lcNsLookup,2)+8
 lcNsIp = ALLTRIM(SUBSTR(lcNsLookup,lnIPpos))
ELSE
 lcNsIp = [] && could not resolve network name
ENDIF 

loScriptExec = null
loShell = null 
RETURN lcNsIp

VFP: Ping

Visual Foxpro: Ping an IP number and return weither it has responded. Uses Windows scripting shell.

* Ping function
* (C)2011 Jasper de Graaf for J.A. Software
* Source: https://degraafonline.com
LPARAMETERS tcIpNumber && IP number to ping

* No parameter, wrong type or empty 
IF PCOUNT()=0 OR !VARTYPE(tcIpNumber)=[C] OR EMPTY(tcIpNumber)
 ? [ping needs character parameter ip number (ie: "192.168.1.254")]
 RETURN []
ENDIF 

* create scripting shell object and test
loShell = CreateObject("WScript.Shell")
IF VARTYPE(loShell)<>[O]
 ? [did not succeed in creating Wscript.Shell object]
 RETURN []
ENDIF 

* perform short single ping and get results
loScriptExec = loShell.Exec("ping -n 1 -w 2 "+tcIpNumber)
lcPing = loScriptExec.StdOut.ReadAll()
llPing = ![time-out]$LOWER(lcPing)

loScriptExec = null
loShell = null 
RETURN llPing