Tuesday 21 July 2015

A PowerShell script to copy files listed in a M3U playlist to a destination folder whilst preserving directory structure

Some background to this script:


  • I have a large music collection, but only limited space on my phone, so I only want to copy a subset of the files
  • I use Logitech Media Server (a.k.a. Squeezebox Server), along with Erland's Trackstat, Playlist Generator and SQL Query plugins, which automatically generates me M3U format playlist files of things like the most recently added tracks, most played, highest rated etc.
  • I wanted to use some of these generated playlists to specify which files to copy to a destination folder, and so I wrote the script below with a lot of help from sources on Google!
  • Challenges included preserving the directory structure when copying each file, and handling directory names and file names with characters such as square brackets, which apparently have special meaning in PowerShell.





$sourcePlayList="X:\playlists\My Music - Last 500 Added.m3u"
$outputPath="M:\S6\music\"
$regExKeep="volume1\/music\/.*"
$regExExclude="#EXTURL:.*"
$rawPlayListValues = Get-Content $sourcePlayList



foreach ($item in $rawPlayListValues) {
  #First extract the path
  if($item -match $regExExclude){
  }
  elseif($item -match $regExKeep){
   $item = $matches[0] -replace "volume1\/music\/",""
   
   $sourceitem = "X:\$item"
   $sourceitem = Get-Item -Literalpath $sourceitem
   
   $dir = $sourceitem.DirectoryName.Replace("X:\",$outputPath)
   
   $target = $sourceitem.FullName.Replace("X:\",$outputPath)

            if (!(test-path -LiteralPath $dir)) 
   {
    mkdir $dir
   }
   
   if (!(test-path -LiteralPath $target))
   {
    Copy-Item -Literalpath $sourceitem.FullName -Destination $target -Recurse -Force
   }

   Write-Host $item
  } 
}

Sunday 25 January 2015

Rendering images showing the weather forecast from an RSS Feed

I created this bash shell script to generate images showing the weather forecast for my region for the next 2 days, using the BBC Weather RSS feed as a source.

The script is scheduled hourly using crond on one of my Raspberry Pi devices, and outputs 2 images to the local Apache instance web root directory. The images are composed by first parsing the XML from the RSS feed, extracting, transforming and formatting the required values, then generating the resultant images using the ImageMagick convert tool.

I then point my SqueezeBox Radio Image Viewer screensaver at an .lst file containing the URLs of the 2 images, and now each day I wake up to a handy 2 day forecast to help me plan my day!

This script is far from optimised, but it does the job - feel free to try it yourself or suggest modifications and additions to improve it.


NOTES

  • The BBC Weather RSS feed only covers the UK, and you will also need to find out your 'area code' and change it in the supplied URL to get the forecast for your specific region.
  • The script heavily relies on the layout and format of the feed always being the same - if the BBC decide to change it I'll have to adapt the script accordingly.
  • The images are created at a fixed size (320 x 240) to allow display on the SqueezeBox radio screen - you can create them at any size required though.


SAMPLE OUTPUT





THE SCRIPT

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/bin/bash

#CREATE IMAGE FOR TODAY
today_text=`curl -s --location --header "Accept: application/rdf+xml" \
http://open.live.bbc.co.uk/weather/feeds/en/2643123/3dayforecast.rss | xmllint --format --xpath 'string((//channel/item/title)[1])' -`

#text=`echo $text | sed -r 's/, /\\\n/g'`

weekday_today=`echo $today_text | sed -r 's/day:.*/day/g' | colrm 4 | \
sed -e 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/'`

today_text=`echo $today_text | sed -r 's/Maximum Temperature:/\\\nMax Temp;/g'`
today_text=`echo $today_text | sed -r 's/Minimum Temperature:/\\\nMin Temp;/g'`
today_text=`echo $today_text | sed -r 's/.*://g'`
today_text=`echo $today_text | sed -r 's/;/:/g'`
today_text=`echo $today_text | sed -r 's/,/\\\n/g'`

convert -background black -gravity center -fill white -font Helvetica -size 320x240 -pointsize 22 -interline-spacing 10 \
label:"\n$today_text" /var/www/weather_forecast_today.jpg
convert /var/www/weather_forecast_today.jpg -font Helvetica -pointsize 80 -fill rgba\(173,216,230,0.8\) -gravity northWest \
-annotate 0 "$weekday_today" /var/www/weather_forecast_today.jpg



#CREATE FORECAST IMAGE FOR TOMORROW
tomorrow_text=`curl -s --location --header "Accept: application/rdf+xml" \
http://open.live.bbc.co.uk/weather/feeds/en/2643123/3dayforecast.rss | xmllint --format --xpath 'string((//channel/item/title)[2])' -`

weekday_tomorrow=`echo $tomorrow_text | sed -r 's/day:.*/day/g' | colrm 4 | \
sed -e 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/'`

tomorrow_text=`echo $tomorrow_text | sed -r 's/Maximum Temperature:/\\\nMax Temp;/g'`
tomorrow_text=`echo $tomorrow_text | sed -r 's/Minimum Temperature:/\\\nMin Temp;/g'`
tomorrow_text=`echo $tomorrow_text | sed -r 's/.*://g'`
tomorrow_text=`echo $tomorrow_text | sed -r 's/;/:/g'`
tomorrow_text=`echo $tomorrow_text | sed -r 's/,/\\\n/g'`

convert -background black -gravity center -fill white -font Helvetica -size 320x240 -pointsize 22 -interline-spacing 10 \
label:"\n$tomorrow_text" /var/www/weather_forecast_tomorrow.jpg
convert /var/www/weather_forecast_tomorrow.jpg -font Helvetica -pointsize 80 -fill rgba\(173,216,230,0.8\) -gravity northWest \
-annotate 0 "$weekday_tomorrow" /var/www/weather_forecast_tomorrow.jpg

Friday 20 July 2012

How to display Windows OS information from the command line

To display Windows OS information from the command line:

systeminfo | findstr /C:"OS"

Thursday 23 February 2012

How to pair an HTC Wildfire smartphone with a Ford Fiesta car radio via Bluetooth

This one took me over a year to finally crack!

Every time you try and pair an HTC Wildfire with a Ford Fiesta via Bluetooth, the mobile device will prompt you to enter the PIN code shown on the car display, but nothing is shown on the car display until AFTER you enter a PIN code. Unfortunately this PIN code is always a randomly generated 4 digit number, not the default 0000 or 1234 suggested by the phone. As it only appears AFTER you've already entered a code, there is only a 1 in 10000 chance you'll be able to guess what it'll be...

This is now I got round the problem:


2. Ensure Bluetooth is turned on in the car radio settings.

3. Search for available Bluetooth devices from the HTC Wildfire.

4. Once found, select 'Ford Audio'.

5. Enter any 4 digit number on the Wildfire and submit.

6. The actual pairing code will then appear on the screen of the car.

7. Rescan for Bluetooth devices on the Wildfire.

8. Select 'Ford Audio' again.

9. Enter the PIN code still shown on the car screen and submit.

10. The Wildfire will then show a message like 'Paired but Not Connected', but will then set up and connect the various services such as Bluetooth Audio, handsfree, phonebook sharing etc.

I'm sure I tried this process before without success, however that was before I applied the software update to the car radio, so that might have made a difference.

Wednesday 8 February 2012

How to list the ASP.NET framework of all web sites in IIS

If you want to list the ASP.NET framework version for all IIS web sites on a server, use the command below.

C:\WINDOWS\microsoft.net\Framework\v4.0.30319>aspnet_regiis.exe -lk

From the help text: "This will list all the path of all IIS metabase keys where ASP.NET is scriptmapped, together with the version. Keys that inherit ASP.NET scriptmaps from a parent key will not be displayed."

Saturday 7 January 2012

How to change the number of rings before voicemail on a Vodafone Blackberry

This is how to change the number of rings before a call is diverted to voicemail on a Vodafone Blackberry:

1. Enter **61*121*11* into your phone as if you're making a call. Do not press dial.

2. Next enter a time in seconds (any number between 5 and 30 which is a multiple of 5). Do not press dial.

3. Finally, press # and then the dial button.

4. Ring your mobile from another phone to confirm the change is successful.

Friday 6 January 2012

Adding a UNC path containing an IP address as a Trusted Location in Office 2007/2010

If you try and add a network share/UNC path to the 'Trusted Locations' in Office 2007/2010, it will give you an error ("The path you have entered cannot be used as a Trusted Location for security reasons.") if the path you are adding contains an IP address instead of a hostname, e.g. \\10.10.10.1\share. You receive this error even if you have checked the "Allow Trusted Locations on my network (not recommended)" box. Apparently you are only able to add UNC paths, e.g. \\hostname\share.

There appears to be a very simple workaround to this - simply open up Internet Options and add the IP address to your 'Trusted Sites', (file:IP address). Sites listed in your Trusted Sites seem to also be treated as Trusted Locations in Office.

UPDATE 17.06.2014
As mentioned in the comments, this is how to get to the Trusted Sites tab in Internet Options:

Open Internet Explorer --> Settings --> Internet Options --> Security --> Zone: Trusted Sites --> Sites 
or
Open Control Panel --> Internet Options --> Security --> Zone: Trusted Sites --> Sites

Tuesday 12 July 2011

Wiring a BT phone line into a Cat5e home network

This is my 60 second guide to wiring a BT (UK) phone line into a home network using Cat5e cabling, based on my own recent experience. This should allow you to plug your phone into any available RJ45 socket, then at your patch panel connect it into your phone line directly from the corresponding port.

1. BT Cable to Master Socket

The BT cable comes into the house from somewhere outside and is terminated on a 'master' socket. The cable contains 4 - 6 wires, but usually just 4. Only 3 off these are officially used, and only 2 are actually required. The 3rd (Orange/White?) wire is the bell wire, only used to make older phones ring, but not required by modern phones and can cause interference to ADSL if connected. I think you are still supposed to connect the bell wire from the BT cable to the master socket, but then you don't have to extend it to any extensions (I just left it out completely).

The two remaining wires are the 'A' (White, 0V, Pin 2) wire and the 'B' (Orange, -50V to Earth, Pin 5) wire. Terminate these on the master socket on the corresponding contacts.


2. Extension Socket

I used a length of Cat5e cable (better for carrying ADSL as less interference due to twisted pairs) to run a single slave extension from the master socket to a box near my patch panel. NB. There are only two wires required to carry the phone signal, extending the two wires from the BT master socket.

At the master socket end, I terminated the cable onto the back of the removable faceplate.

Blue -> Pin 2
Blue with White Stripes -> Pin 5

At the patch panel end, I terminated the cable onto an ADSL face plate - this basically has an ADSL filter built in, removing the need for untidy dongles hanging off the socket front. The face plate provides two sockets - a BT plug socket (filtered) and an RJ11 (unfiltered) socket for ADSL to connect to the router.

As previously, terminate the wires as follows:

Blue -> Pin 2
Blue with White Stripes -> Pin 5

Test this works for ADSL and your phone line before proceeding any further!


3. Make your patch cables!

BT -> RJ45: To connect your extension socket to a port in your patch panel. I don't have the expensive crimping tool for BT plugs, so I simply used an old BT cable, snipped off one end and attatched an RJ45 plug instead. Quality of cable not really an issue as only analog voice signal is being carried on this cable - ADSL has already been separated out.

A BT plug has 4 pins - only the outer two pins (1 and 4) are required. The color of the wires inside the cable may vary, but I used the red and white wires. The important thing is that Pins 1 and 4 on the BT plug are connected to Pins 3 and 6 on the RJ45 plug (if in doubt use a multimeter with continuity testing to test and confirm after you've made the cable).


BT Plug end:

White -> Pin 1
Red -> Pin 4


RJ45 end:

Red -> Pin 3 (to line up with a narrower RJ11 plug's pins 1 or 2)
White -> Pin 6 (to line up with a narrower RJ11 plug's pins 4 or 5)



RJ45 - RJ11: To connect your telephone to an RJ45 wall socket. NB. You can actually put an RJ11 plug into an RJ45 socket (they are the same height and depth just different widths), so you could also get away with an RJ11 -> RJ11 cable. I used a length of Cat5e for the actual cable to minimise any impact on the ADSL signal.


RJ11 end: Assuming a '4 pin' RJ11 plug (they can have up to six):

Blue -> Pin 1 (Pin 2 on a 6 pin RJ11 plug)
Blue with White Stripes -> Pin 4 (Pin 5 on a 6 pin RJ11 plug)


RJ45 end:

Blue -> Pin 3 (to line up with a narrower RJ11 plug's pins 1 or 2)
Blue with White Stripes -> Pin 6 (to line up with a narrower RJ11 plug's pins 4 or 5)


RJ11 - RJ11: To connect the ADSL socket of the extension face plate to the ADSL router. I used a length of Cat5e for the actual cable to minimise any impact on the ADSL signal.

Assuming a '4 pin' RJ11 plug, for both plugs:

Blue -> Pin 1
Blue with White Stripes -> Pin 4


NB. If using an existing RJ11 to RJ11 cable, make sure it is 'straight through', i.e. that each pin on each plug is connected to the same pin on the plug at the opposite end. At a minimum, pins 1 and 4 should be connected this way.


Good luck!

Tuesday 10 May 2011

How to Extract the Contents of an MSI file

To extract the contents of an MSI installer file, you can us ethe Windows msiexec command line utility.

msiexec /a "path to MSI file/msi filename"

A wizard will run and you'll be prompted where to save the extracted files.

Tuesday 15 March 2011

Debugging MSI installer errors

Some notes on debugging MSI installer errors.

Run the MSI using this command:
msiexec /i "" /l*vx "C:\logname.log"

Run the MSI until the error occurs, then browse through the resultant log. Some common errors include:


DEBUG: Error 2826: Control Line1 on dialog FatalErrorForm extends beyond the boundaries of the dialog to the right by x pixels

You can ignore this one. It's simply telling you that a control on the dialog exceeds the boundaries of that dialog box by x pixels.


From what I've read, the trick to reading installer logs is to search from the top for the text string "Return value 3", then look at the lines immediately preceeding the occurence.

Entries with errors begin with the string "ERROR :" in the logs.