Thursday 4 March 2010

Command line script to zip and unzip files

In Windows XP, and Vista (possibly others) you have the ability to right-click a folder and compress it using some sort of inbuilt zip mechanism.  But how do you access the same functionality from the command line?

One way is to use scripts.  Here is the code for two files which I call zip.vbs:

FolderToZip = Wscript.Arguments(0)
zipFile = Wscript.Arguments(1)
Set sa = CreateObject("Shell.Application")
Set zip= sa.NameSpace(zipFile)
Set Fol=sa.NameSpace(FolderToZip)
zip.CopyHere Fol.Items, 16
Do Until zip.Items.Count = Fol.Items.Count
  WScript.Sleep 1000
Loop
 
and unzip.vbs:
 
pathToZipFile=Wscript.Arguments(0)
extractTo=Wscript.Arguments(1)
set sa = CreateObject("Shell.Application")
set filesInzip=sa.NameSpace(pathToZipFile).items
sa.NameSpace(extractTo).CopyHere(filesInzip)
 
To execute them the syntax is as follows:
 
cscript zip.vbs <folder_to_zip> <zip_file>
cscript unzip.vbs <zip_file> <folder_to_extract_to>
 
E.g:
 
cscript zip.vbs C:\Users\Richard\stuff C:\stuff.zip
cscript unzip.vbs C:\stuff.zip C:\Users\folder

Thursday 18 February 2010

Remove padding from TextBox in WPF

If you add the attribute

Padding="0, 0, 0, 0"

to most WPF elements e.g. <Label></Label>, the text is placed in the top left hand corner with no space between the left and top sides.  However if you try it with a TextBox element it appears to have no effect.

Fortunately as a workaround you can set the padding to have negative values.  So e.g. if I want a standard TextBox element, with standard fontsize, with no space above the text, doing something like:

Padding="0, -2, 0, 0"

Will have the desired effect.

Wednesday 3 February 2010

Select elements by innerHTML with jQuery

Sometimes it is necessary to use jQuery to select html elements with a certain innerHTML value.  Consider the following 3 labels:

<label>apples</label>
<label>oranges</label>

<label>opal fruits</label>

Apparently the following are all equivalent ways of matching a label based on its innerHTML value:

$("label[innerHTML=opal fruits]") - no quotes
$("label[innerHTML='opal fruits']") - single quotes within double quotes
$("label[innerHTML=\"opal fruits\"]") - escaped double quotes within double quotes


However innerHTML must be a strange one, as this form of matching only works for strings of multiple words and without the quotes.  As soon as you try and match a single word, or put quotes around the string, no matches are found.  Even more confusing is that if you try the following, which should match any element with innerHTML, you get no matches:

$("[innerHTML]")

Fortunately, jQuery, as you might expect, has a better way of doing it using the :contains(text) selector.  So now I can use the following:

$("label:contains(opal fruits)")

Thursday 28 January 2010

NHibernate.HibernateException: Incorrect syntax for definition of the 'TABLE' constraint.

While using Spring.NET and NHibernate on a ASP.NET web application I came across the following error:

System.Data.SqlClient.SqlException: Incorrect syntax for definition of the 'TABLE' constraint.
NHibernate.HibernateException: Incorrect syntax for definition of the 'TABLE' constraint.

The problem was that I was trying to name a column in a SQL Server database as "References", which must be a protected name because it didn't like it. When I changed my code to call the column "Refs" instead the problem went away.

[Property(Name = "Refs", Column = "References", Length = 4001)]
//Length 4001 is equivalent to nvarchar(MAX)
public virtual string Refs { get; protected internal set; }