Sunday, April 09, 2006

[VBScript] When did I log on/off?

The Australian federal government has brought in new laws which seem to require a return to the old days of clocking in at the beginning of the working day and clocking out at the end. As a contractor I don't think I'm going to notice a big difference ... if I want to get paid for the hours I work, then I fill out my timesheet appropriately.

If you aren't in the habit of keeping this kind of information, don't worry too much: Windows keeps a track of your login and logout in one of its Event Logs. Getting that information is fairly easy, if you know what you're looking for. Otherwise its like the proverbial needle in the haystack.

The script outputs into an Excel spreadsheet, the date of each logon in a separate column containing time of logon and logoff. Please note that the script was designed to query someone else's machine, and will likely need modification if you are trying to get your own data out of your own event log.

Here's the code.

As is often the case in my scripts, the preamble loads external code libraries. The former is a the standard library and the latter (see the end of the posting) a symbol table class, based on the Scripting.Dictionary object.
Next comes code to split up the event log's timestamp, followed by the routine to store logon date and logon and logoff times into spreadsheet cells.
Next comes the variable declarations. In retrospect the variables sComputer and sUser could have been Const rather than Dim. For that matter they could have been taken from the command line. Note the double backslash in the uUser variable: this is needed for where it occurs in the WMI call.
This is the core of the script: pointing WMI at sComputer and executing an SQL query against Win32_NTLogEvent. It asks for everything from the 'Security' Logfile where the EventCode matches a logon or a logoff and where the User matches sUser.
Then the script starts to do things with the list of events now stored in cEvents. First a Dictionary is loaded with logons as (zero, comma, TimeSplit of datestamp) and logoffs as (one, comma, TimeSplit of datestamp). The reason for this is that there may be more than one logon and logoff event for a given user during a working day. By storing the logon and logoff times in this way, the first "0" reference can be assumed to be the first logon off the day, and the last "1" can be assumed to be the last logoff of the day.
From the Dictionary wrapper class the keys are extracted. A check is put in here to make sure that there are records to be reported.
Next comes the Excel interactions. The ExcelStart and ExcelNewSheet macros are listed at the bottom of this posting.
What follows is the guts of the Excel reporting. Despite the possibility of incorrect recording, the assumption is that the report will contain the first logon and the last logoff. If a logon occurs after the last logoff, it is discarded. The script is unable to cope with the situation where someone logs on today and logs off tomorrow.

Note that a check is made not to report more dates than MAX_INSTANCE, a limit that has not yet been encountered. AMax and AMin are listed at the bottom of the posting.
Excel is left running with the results of the report. It's up to the user to save it, print it or whatever. To finish, the macros ExcelStart, ExcelNewSheet, AMax and AMin, and the ClassSymTab class as promised.

Tuesday, March 14, 2006

[News] Looking for work ... again

Went and visited three new (to me) employment agencies today. Gryphon, Sinclair Knight Merz and Candle. I also went to the offices of the City of Perth and picked up a busking permit application. Do I have the guts to stand up and sing, a capella, in Murray St. Mall?

Friday, February 24, 2006

[News] Codeaholic/LL

That's it then ... 99.9% of the languages that were listed down the left hand side are now over on the Code-a-holic Language List.

Mate! I'm glad that's over.

Wednesday, February 22, 2006

[News] Today and Tomorrow

Today it was swapping hard drives; sharing receipt printer; finding, downloading, and failing to get working the old touch-screen software; telling all the other machines to use LPT6 when running the POS application, but making sure that the POS machine itself still uses LPT1 (using Clipper 5.3); and getting the floppy drive to work in the SERVER.

Today I celebrated by 45th birthday. One of the gifts I received was a PLUG membership. Eventually the ancient source codes promised but never delivered on Code-a-holic will find their way there.

Tomorrow is the first day (of about 1.5 weeks) at Home Open working on the help desk. More news, and code, as it comes to hand.

Friday, February 10, 2006

[News] End of Contract ... again

The Holy Grail, permanent work, remains elusive. I have good KSAs (Knowledge, Skills, Attitudes) but employers seem only interested in having me around for a few weeks ... maybe it's my deodorant / mouthwash / politics / religion ...

A couple of times it's been suggested I try out writing for a living. What do I live on in the meantime? Others have suggested getting a heavy-rigid licence and signing up to be a bus driver. That's tempting, and it may leave time to write. But it also means stepping into the twilight between an IT career and "something else."

So Monday it's climb back into the suit, catch the bus into town, and go door to door, employment agency to employment agency, saying, "Hey, remember me? I'm looking for a job (again.)"

[Languages] The Left-Hand List

The list of programming languages on the left-hand side is shrinking. That's A to E done. They've all been moved over to the language list on Code-a-holic/LL.
Over the next few weeks I'll be posting the rest of the data I have on those languages (the bare minimum of name, link, and description.) If anyone's interested in the metacard database or having an XML-ish file with all of the extra details (platform, users' group, date of last activity, etc.), please leave a comment.
Leave a comment too, if you can see anything that needs changing / updating.

Thursday, February 02, 2006

[VBScript] CliVE - Command Language Interpreter VBScript Environment

A few years back ZATZ Publishing, in their DominoPower Magazine, published an article entitled Using Python to create a command line interpreter for Notes. Not being that au fait with Python I decided to have a go at doing the same thing in VBScript. CliVE is the result.

CliVE takes the basic CLI mechanism of my previous posting (
[VBScript] CLI Framework) and adds a couple of extra bits: Loading, Saving, and a shorthand for 'WScript.Echo'

First the code supporting the standard header.
Then support for putting script files on the commandline. Files specified on the command line are loaded and interpreted, one after the other.
This is the main interpreter loop.

The aSession array stores each line passed to Evaluate so that a given session's script can be stored.

In the main loop, a prompt is output and input received. The sLine is split into a two element list: the first element being the keyword; the second being all subsequent text.

There are a few keywords starting with a dot. These are peculiar to CliVE:
.quit
.exit
Synonyms for WScript.Quit
.echo
?
Synonyms for WScript.Echo. Unlike other dot commands, the line starting with this command is passed to the Evaluate function.
.save
Saves the contents of the aSession array to SaveClive.clive unless a filename is given as an argument.
.run
.load
Reads the filename given in the argument and executes the code therein by passing it to ExecuteGlobal.
.debug
An empty procedure. A place to put test code.
All other commands are treated as VBScript commands and are passed to the Evaluate subroutine.
Evaluate, Save, LoadRun and Debug.

Note that the code for Evaluate traps errors, and displays an error message. Note also that LoadRun has no such protection. It's something one might want to add ASAP!

Note also the call to AAdd, which adds the evaluated command to the aSession array if the command works without error.
Rather than reproduce the entire StandardLibrary (1500+ lines), here are the referenced routines (AAdd, Exists, ReadFile, WriteFile), plus supporting symbols.

Wednesday, February 01, 2006

Friday, January 27, 2006

[VBScript] Where else have I logged in on this network?

It's not so much a question for me -- I remotely log into many pcs daily as part my job, and try to leave as few tracks as possible. However, today a customer rang saying she'd lost a few important Favourites and had no backup. The best way I could think of helping her, was to locate all the machines she's logged into and see if any of the Favourites could be found.

I did find what she was looking for. Here's the code I used.

First the standard header implementing the synthetic include (derived from
Rube Goldberg Memorial Scripting Page)
StandardLibrary.vbs is getting big. 1500+ lines of code.

Next a little function for a right fill. Having needed this only once, I haven't included it in the StandardLibrary.
Now the main body of the script.

1. Check commandline to see if a username is specified.
1.1 If nothing specified, throw a fit.
2. Get an array of all the computers currently online.
2.1 No error checking here but there ought to be
3. Tell everyone what's going on.
4. Loop through the names of the online machines
4.1 Trim off the leading \\ put on by NET VIEW
4.2 Write out the name, right filled with dots
4.3 Get an array of the login profiles for the computer
4.4 Scan the array to see if contains the the username (case insensitive search)
4.4.1 If found say so with a CRLF
4.4.2 otherwise issue a CR

The calls to BeginsWith are commented out. They apply only to my situation where a company edict ensures that all desktop pc names start with PC and all laptops with LP.
The supporting library rountines are below, First GetOnLineComputers with its supporting routines, GetNetView, CaptureDOS and ReadFile
Next, BeginsWith (included for completeness sake)
Then GetLoginProfiles, plus support routine AAdd
And, finally, StrInArray.
Unfortunately, this does not run quickly. It can take ages to get through the entire list and sometimes hangs inexplicably. Your mileage may vary.

If anyone comes up with a faster way of doing this I'd be very interested.

Monday, January 23, 2006

[Euphoria] Structured Access, part 2

I knew I had it somewhere. Okay, these implement the following: BYTE, BYTES, WORD, DWORD, QWORD, ASCIIZ and PSTRING. You can pull the data one chunk at a time as per the last posting or else pull a whole set all at once.

The file versions don't work and I'm not sure why, and as I said before, I don't have the time to do anything about it.

By the way, this was written before Euphoria 2.5. In fact, possibly before 2.4 so the code may need some tweaking.

mread is like the getmem of the previous posting. memread takes a sequences of reads. I will eventually post an example (if I can find one).

(Since writing part 1, I've been in touch with CChris from
Euphoria Standard Library who has taken over the development of getmem. It's great to be able to give something back to the Euphoria community after all these years (I've been using it since version 1.5).)

Tuesday, January 17, 2006

[Euphoria] Structured Access

Late last century, in a developing part of the world, I tried writing a diskette cataloguer. It worked okay, once I'd figured out how to do all the gymnastics necessary for BIOS calls. In the process I wrote a function to access memory in a structured way. Brilliant as Euphoria sequences are, they are just that: sequences. There's little in the way of structure, unless you apply some of your own.

I've reproduced the function in its entirety below. Essentially, the code takes data from memory in a structure way, that is, in terms of BYTE, BYTES, WORD, DWORD or ASCIIZ.

An attempt was made to make the functions applicable to files. Those routines don't work, and I haven't taken the time to find out why. Good as Euphoria is, there's no Mac OS X version (so I can't use it at home) and there's no opportunity to use it at work -- Euphoria's not in the SOE. If you're interested in looking at the code, write.
As an example of how to use getmen, below is the code for GetDriveParameterTable
As an example of the BYTES parameter:
The ASCIIZ parameter is like the BYTES except it keeps working its way through memory until it encounters a binary zero and then returns everything up to that point.

From memory I remember writing a version of getmem() which received a sequence of BYTE, WORD, etc. specifications and returned the resulting sequence of results. When I find it I'll post it.

Enjoy.

Wednesday, January 11, 2006

[VBScript] A CLI framework

Here's a simple framework for building your own CLI (Command Language Interface) Keywords at this point are: quit, exit, bye, eval, echo, set and run. Below is a sample session. Now my own version adds a little to this framework by including a .vbs file full of extra symbols, procedures and classes. I've reproduced it below just so that you can see how to do a synthetic include in VBScript. Interestingly, the technique also works in HTAs. Enjoy.

[VBScript] CSV to variables using Execute

Nico wrote, on microsoft.public.scripting.vbscript:
I need ASP to read the CSV file to read lines 2 on-wards and display it back to the user for verification. Each line has aprx 8 "," that control user properties.

I am looking for each line to be displayed back to the user without the "," so they can verify and write to an access db.

All i am looking for is a simple way to read the text display to the user for acceptance before writing it back to the db.
I wrote a response but thought a fuller treatment was in order, thus the following:

Data, stored in DEMO.CSV
Then the code which does nothing other than read the file, store the data in variables for each line of data, and for effect, echoes the first and last names And here's the results of the run, taken from SciTE's output window A possible extra step would be to read the header line, strips the spaces, build the "Dim" statements and execute them as well. Or maybe, create an instance of "Scripting.Dictionary" and use that. Or ... well, there must be more than two ways to do it, even in VBScript!

Tuesday, January 10, 2006

[VBScript] Threads ... thin ones

The following is an attempt at threads in VBScript. The code owes a lot to Greg Chapman of MouseTrax Computing Solutions. There are two scripts, the first calling the second (in many instances). The means for communicating between the two is via the Volatile environment. When the script runs, Outlook flickers (don't know why).

There are comments, but the basic idea is to keep a watch over how many instances of cscript.exe there are running and while there's only so many, add more. Use the Volatile environment for semaphores and make sure there's enough delay in there to get stuff to and from Volatile (which is actually in Registry and not blazingly fast).

First the controller.
Then the controlled. There are quite a few extra useful routines in all that code, including an AADD(), and assorted WMI related routines.

I'm in the process of putting the thread functionality inside a Class so that I can change how things are done without causing drastic rewrites of the original VBScripts. I'm not entirely pleased with the Volatile environment and may end up playing with something else that can be used as a shared storage area between many programs ... like subdirectory structures.


Monday, December 19, 2005

[VBScript] Deriving user name from computer name

I found a brilliant bit of code for deriving the username from the computer name. It's found inside Matt Williams's "ManagePC v1.31" HTA. His description of the routine is as follows
This bit of code is "cheating" slightly in that all it's doing is getting the owner of the explorer.exe process. Seeing as explorer.exe only runs if a user is logged on it is a reliable (but sneaky) way of getting the logged-on user. It does, however, mean that even if a user is logged on locally or the account is a generic account, it will retrieve the correct values (WMI cannot do this).
-- Matthew Williams, ManagePC
Below is my routine, combining his code with some extra housekeeping. Of course, if no one is logged in, you get nothing. My own take on Matt's routine adds in a check to see if the computer is actually running, using ResolveIP() below:

[VBScript] Plugins

I had written a plugin architecture for one of my HTA applications. Now I discover that I can have plugins using almost any scripting language available. I found this bit of brilliance in a google search: I can now pass whatever information I like to and fro from application to plugin and back again. And if another scripting languages does something better than VBScript I can use it (provided it gives access to the volatile store).

Wednesday, December 07, 2005

[Working Again]

After a short stint on the support queue at iiNet (part-time), I'm now on contract at Department of Land Information. L1 and L2 support with a bit of scripting on the side. VBScript again, sadly.

Friday, November 04, 2005

[Pharmacy Software]

One of my clients has suggested I write pharmacy software. He has a home-grown Clipper app that does everything from Dispensing to Cash Register. I give him a hand with it from time to time. He reckons there's a market for a low-cost package which would fit the Western Australian situation better than the off-the-shelf ones now available.

So comes then the ever-present question: Closed Source, or Open Source.

No knowing a lot about how Open Source works as an income generator, I fear that there is the risk that my non-Open Source competitors may steal my ideas and woo my customers away because they have the resources to bring them (the ideas, not the customers) to the market faster than I can. Is that a valid concern?

Closed Source isn't without its problems either.

And then comes the other questions: programming language / IDE / RAD. I wonder if anyone's come up with a template for software projects in the style of Word / Excel / PowerPoint templates.

[Visual Studio 2005 Team System Beta 2]

I went to a Microsoft do yesterday and that's what they gave me (amongst other things.) Now to find a machine to install it on (only Linux, MacOSX and Win98 at home).

Eventually I want to start bidding on jobs at Get A Coder and Get a Freelancer.

Until then ... Khuda janta he.

Thursday, October 20, 2005

[VBScript] Shell and Heap Sorts

The final versions of the Shell sort and Heap sort classes. First the ShellSorter:
and then the HeapSorter


By the way, I'm unemployed again. Things might be a bit quiet around here for a while.