2013-05-30

Get Internet Explorer version using WMI StdRegProv

In Windows XP, you could get the version of Internet Explorer on a computer using the "\Applications\MicrosoftIE" namespace in this fragment of VBA: (To use this code in this article in your VBA project, include a reference to Microsoft WMI Scripting 1.2 Library.)
    Dim objSWbemLocator As SWbemLocator
    Dim objSWbemServices As SWbemServices, colSWbemObjectSet As SWbemObjectSet, objSWbemObject As SWbemObject, varValue As Variant

    Set objSWbemServices = objSWbemLocator.ConnectServer(strServer:=".", strNamespace:="\Applications\MicrosoftIE")
    Set colSWbemObjectSet = objSWbemServices.ExecQuery("Select * from MicrosoftIE_Summary")
    For objSWbemObject in colSWbemObjectSet
      Debug.Print objSWbemObject.Version
    Next objSWbemObject
The "\Applications\MicrosoftIE" namespace is not available after the introduction of Vista (and Windows 7), so the alternative is to examine the registry:
Const HKEY_LOCAL_MACHINE = &H80000002
...
    Dim objSWbemLocator As SWbemLocator
    Dim objSWbemServices As SWbemServices, objSWbemObject As SWbemObject, varValue As Variant

    Set objSWbemServices = objSWbemLocator.ConnectServer(strServer:=".", strNamespace:="\root\default")
    Set objSWbemObject = objSWbemServices.Get("StdRegProv")
    objSWbemObject.GetStringValue HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Internet Explorer\", "Version", varValue
    Debug.Print = varValue

2013-02-16

Google Play Music "Couldn't play the track you requested"

I made some MP3 files and uploaded to my Samsung Nexus. Google Play Music (GPMu) would find the files but when I tried to play any of them, it displayed "Couldn't play the track you requested". If I select the files individually using a file browser, I could play them with GPMu, so the file format was recognised. Clearing GPMu's cache and data did not fix the problem.

My workaround was to attach an image to each file using a media library manager then reload the files. Now GPMu can play the files.

2013-01-31

tr: extra operand

Kept getting this error when trying to delete Ctrl+Z (octal 032) from a file:

tr -d \032 file.txt
tr: extra operand `file.txt'
Only one string may be given when deleting without squeezing repeats.
Try `tr --help' for more information.

I keep forgetting that tr is expecting data from a stream not a file so the solution is to redirect the input: tr -d \032 < file.txt.

2012-11-10

Quick Play with Pattern and Hold Spaces in Sed

I had some rows of data that required two changes and both changes had to appear on separate lines. For example, if the input was ...

123
456
789

... and the requirement was to find '456', replace '5' with 'a' on one line and 'b' on another line so that the output would look like this:

123
4a6
4b6
789

The sed script below applies the first change, prints it, applies the second change and let sed print the pattern space at the end of the function.

/456/ {
  s/5/a/p --> pattern space = 4a6, print pattern space.
  s/a/b/  --> pattern space = 4b6
}         --> print pattern space, 4b6

In the script above, notice that the input string was first modified from '456' to '4a6' so the second statement has to replace 'a' with 'b'.

Another approach is to create two lines first (i.e. 456\n456) then replace the first '5' with 'a' and replace the second '5' with 'b'.

/456/ {
  h      --> hold space = pattern space, 456
  H      --> hold space = 456\n456 (456 + \n + pattern space)
  g      --> pattern space = hold space, 456\n456
  s/5/a/ --> pattern space = 4a6\n456
  s/5/b/ --> pattern space = 4a6\n4b6
}        --> print pattern space, '4a6\n4b6'

The script above uses sed's hold space to construct the two lines first. I don't know if the second script is clearer than the first but it was a simple problem that I could use to play with sed's pattern and hold spaces.