Archive for October, 2009

Command line Batch File to Remove Direcories, using a Wild Card!

We have a bunch of web services which log details of requests that come in, in folders named using the current date- in the format YYYY-MM-DDD-HH-MM-SS. I wanted to produce a simple cleanup batch file which we could run every month to bin all logs for transactions which happened 3 months ago- using the dos commands which exist on Windows Server 2003.

The dos “del” command allows you to delete using wild cards – so for example “dev 2009-07-*” would erase any files starting with “2009-07-”; However this doesn’t work with folders- so it fell to the rd (remove directory) command- but this doesn’t support wild cards (I’m assuming for safety- to stop you permanently erasing 100′s of folders and their contents accidently- remember there’s no recycle bin when you delete from the command line!).

To get around this I created a simple Batch file which accepted a wildcard as a parameter, then removes all those folders at the current level (it wont check recursivly). Just in case this is of use to anyone else, here’s the code;

REM - performs a remove directory, with wildcard matching - example ; rd-wildcard 2007-*
dir %1 /b >loglist.txt
setlocal enabledelayedexpansion
for /f %%a in (./loglist.txt) do ( 
	rd /s /q %%a
)
del /q loglist.txt
endlocal

5 Comments

Paged List of Child Nodes in Umbraco

We just started looking at Umbraco- and so I’m delving into Macro’s – these are the packaged up chunks of functionality which make your site actually do things and either take the form of XSLT files which process and spit out the contents of your database, or dot net controls.

I found an excellent example XSLT that allows you to create a paginated list of child nodes over on Tim Geyssens “nibble” blog, and I’ve modified it ever so slightly to stop the numerical index rendering if the pagecount is less than 2- so thought I would re-post it!


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [
  <!ENTITY nbsp "&amp;amp;amp;#x00A0;">
]>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxml="urn:schemas-microsoft-com:xslt"
xmlns:umbraco.library="urn:umbraco.library"
exclude-result-prefixes="msxml umbraco.library">

  <xsl:output method="xml" omit-xml-declaration="yes"/>
  <xsl:param name="currentPage"/>
  <xsl:template match="/">

    <xsl:variable name="recordsPerPage" select="/macro/recordsPerPage"/>
    <xsl:variable name="pageNumber" >
      <xsl:choose>
        <!-- first page -->
        <xsl:when test="umbraco.library:RequestQueryString('page') <= 0 or string(umbraco.library:RequestQueryString('page')) = '' or string(umbraco.library:RequestQueryString('page')) = 'NaN'">0</xsl:when>
        <!-- what was passed in -->
        <xsl:otherwise>
          <xsl:value-of select="umbraco.library:RequestQueryString('page')"/>
        </xsl:otherwise>
      </xsl:choose>

    </xsl:variable> &amp;amp;amp;nbsp;

    <xsl:variable name="numberOfRecords" select="count($currentPage/node)"/>

    <!-- The fun starts here -->
    <ul>
      <xsl:for-each select="$currentPage/node [string(data [@alias='umbracoNaviHide']) != '1']">
        <xsl:if test="position() > $recordsPerPage * number($pageNumber) and
position() <= number($recordsPerPage * number($pageNumber) +
$recordsPerPage )">
          <li>
            <a href="{umbraco.library:NiceUrl(@id)}">
              <xsl:value-of select="@nodeName"/>

            </a>
          </li>
        </xsl:if>
      </xsl:for-each>
    </ul>

    <xsl:if test="$pageNumber > 0">
      <a href="?page={$pageNumber -1}">previous </a>
    </xsl:if>

<xsl:if test="($numberOfRecords > $recordsPerPage)">
    <xsl:call-template name="for.loop">
      <xsl:with-param name="i">1</xsl:with-param>
      <xsl:with-param name="page" select="$pageNumber +1"></xsl:with-param>
      <xsl:with-param name="count" select="ceiling(count($currentPage/node)div $recordsPerPage)"></xsl:with-param>
    </xsl:call-template>
</xsl:if>

    <xsl:if test="(($pageNumber +1 ) * $recordsPerPage) < ($numberOfRecords)">
      <a href="?page={$pageNumber +1}">next</a>
    </xsl:if>
  </xsl:template>

  <xsl:template name="for.loop">

    <xsl:param name="i"/>
    <xsl:param name="count"/>
    <xsl:param name="page"/>
    <xsl:if test="$i <= $count - 1">

      <xsl:if test="$page != $i">
        <a href="{umbraco.library:NiceUrl($currentPage/@id)}?page={$i - 1}" >

          <xsl:value-of select="$i" />
        </a>
      </xsl:if>

      <xsl:if test="$page = $i">

        <xsl:value-of select="$i" />

      </xsl:if>
    </xsl:if>
    <xsl:if test="$i <= $count - 1">
      <xsl:call-template name="for.loop">
        <xsl:with-param name="i">
          <xsl:value-of select="$i + 1"/>
        </xsl:with-param>
        <xsl:with-param name="count">
          <xsl:value-of select="$count"/>
        </xsl:with-param>
        <xsl:with-param name="page">
          <xsl:value-of select="$page"/>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

2 Comments

Stop the “web.config” being inherited by child applications in sub folders

We recently had an issue setting a .net microsite live off the back of an existing domain, hosting another .net site.

This was due to the web.config being inherited to the child- we would be hit with errors like this;

Configuration Error

Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.

Parser Error Message: Could not load file or assembly ‘UrlRewritingNet.UrlRewriter’ or one of its dependencies. The system cannot find the file specified. (d:ProjectsVSAspDotNetFaqProjectWebsiteweb.config line 89)

Source Error:

Line 88: <httpModules>
Line 89:   <add name="UrlRewriteModule" type="UrlRewritingNet.Web.UrlRewriteModule, 
      UrlRewritingNet.UrlRewriter"/>
Line 90:   <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, 
          System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
Line 91: </httpModules>

This is because a module was defined in the parent site which doesn’t exist in the child, but because it’s inheriting it still tries to load it.  We would also get errors complaining that modules have already been defined.

I tried initially, manually adding <remove name=”blah” /> to the child web.config to remove the entries in the child web.config it thought were defined twice, but after putting in about 10 of these and it still not working it started to get a bit ridiculous!

The trick, we found on google (How to disable web.config Inheritance for Child Applications in Subfolders in ASP.NET?) is to stop the web.config from inheriting in the top level web.config;

<location path=. inheritInChildApplications=false>

<system.web>

</system.web>
<
/location>

This stops anything being migrated down to the children, allowing you to have your full web.config in the child site, unaffected by the parent! — Make sure you don’t put this around anything else though; just the system.web section!

, ,

No Comments

URL Routing with ASP.NET 4 Web Forms (VS 2010 and .NET 4.0 Series) – ScottGu’s Blog

I was quite excited (what have I become!?) to read about the URL Routing stuff from MVC, which is now available in WebForms as of ASP.net 4.0- specifically the ability to retreive a URL back from the Route engine from the code behind!

URL Routing with ASP.NET 4 Web Forms (VS 2010 and .NET 4.0 Series) – ScottGu’s Blog.

No Comments

Linq Distinct!

I have a List of books- each book has a category – i want to find out all the unique categories involved, given a list of books- i knew i could do this with linq, but not quite sure how!

It turned out to be easy..

List<Category> categories = _books.Select(i => i.Category).Distinct();

There was a minor complication however- Distinct obviously needs to be able to compare different instances of Category- you can supply a delegate to a custom EquityComparer class- otherwise it will simply check if this is the same instance of the object, which will (probably) always come back false rendering your distinct useless. So i created this;

    public class CategoryComparer : IEqualityComparer<Category>
    {
        public bool Equals(Category x, Category y)
        {
            return x.Id == y.Id;
        }

        public int GetHashCode(Category obj)
        {
            return obj.Id.GetHashCode();
        }
    }

then updated my linq to use it..

List<Category> categories = _books.Select(i => i.Category).Distinct(new CategoryComparer());

No Comments

A complete list of .NET Serializers in 3.5

Just saw this pop up in my Google Reader and though I would blog it as it is tres useful!

.NET Serializers – Development With A Dot.

As of version 3.5 SP1, .NET comes along with the following serializers:

  • System.Runtime.Serialization.Formatters.Binary.BinaryFormatter: binary serializer used in .NET Remoting, type information is included, understands System.Runtime.Serialization.ISerializable;
  • System.Runtime.Serialization.Formatters.Soap.SoapFormatter: SOAP (XML) serializer used in .NET Remoting, includes type information, understands System.Runtime.Serialization.ISerializable;
  • System.Runtime.Serialization.DataContractSerializer: XML serializer used in WCF, does not include type information;
  • System.Runtime.Serialization.Json.DataContractJsonSerializer: JSON serializer used in WCF, does not include type information;
  • System.Runtime.Serialization.NetDataContractSerializer: binary serializer used in WCF, includes type information;
  • System.Xml.Serialization.XmlSerializer: XML serializer used in ASP.NET Web Services (ASMX), type information is not included, understands System.Xml.Serialization.IXmlSerializable;
  • System.Web.UI.LosFormatter: uses ObjectStateFormatter internally, does not account by itself;
  • System.Web.UI.ObjectStateFormatter: binary serializer used in ASP.NET for storing items in view state, control state and session, type information is included, understands System.Runtime.Serialization.ISerializable.

There isn’t, however, a common interface for all of these serializers, and, in fact, they have quite different requirements.

The base interfaces are:

  • System.Runtime.Serialization.IFormatter: for BinaryFormatter, SoapFormatter, NetDataContractSerializer and ObjectStateFormatter;
  • System.Runtime.Serialization.XmlObjectSerializer: for DataContractSerializer, DataContractJsonSerializer and NetDataContractSerializer;
  • XmlSerializer doesn’t have any interface that specifies its behavior.

The interfaces that can be used to control the serialization process are:

  • System.Runtime.Serialization.ISerializable: for all the IFormatter serializers;
  • System.Xml.Serialization.IXmlSerializable: for XmlSerializer.

Also, there are a couple of attribute classes that can be used to control the serialization process:

  • System.Xml.Serialization.SoapAttributeAttribute (XmlSerializer)
  • System.Xml.Serialization.SoapElementAttribute (XmlSerializer)
  • System.Xml.Serialization.SoapEnumAttribute (XmlSerializer)
  • System.Xml.Serialization.SoapIgnoreAttribute (XmlSerializer)
  • System.Xml.Serialization.SoapIncludeAttribute (XmlSerializer)
  • System.Xml.Serialization.SoapTypeAttribute (XmlSerializer)
  • System.Xml.Serialization.XmlAnyAttribute (XmlSerializer)
  • System.Xml.Serialization.XmlAnyElementAttribute (XmlSerializer)
  • System.Xml.Serialization.XmlArrayAttribute (XmlSerializer)
  • System.Xml.Serialization.XmlArrayItemAttribute (XmlSerializer)
  • System.Xml.Serialization.XmlAttributeAttribute (XmlSerializer)
  • System.Xml.Serialization.XmlChoiceIdentifierAttribute (XmlSerializer)
  • System.Xml.Serialization.XmlElementAttribute (XmlSerializer)
  • System.Xml.Serialization.XmlEnumAttribute (XmlSerializer)
  • System.Xml.Serialization.XmlIgnoreAttribute (XmlSerializer)
  • System.Xml.Serialization.XmlIncludeAttribute (XmlSerializer)
  • System.Xml.Serialization.XmlRootAttribute (XmlSerializer)
  • System.Xml.Serialization.XmlTypeAttribute (XmlSerializer)
  • System.Runtime.Serialization.CollectionDataContractAttribute (XmlObjectSerializer)
  • System.Runtime.Serialization.DataContractAttribute (XmlObjectSerializer)
  • System.Runtime.Serialization.DataMemberAttribute (XmlObjectSerializer)
  • System.Runtime.Serialization.EnumMemberAttribute (XmlObjectSerializer)
  • System.Runtime.Serialization.IgnoreDataMemberAttribute (XmlObjectSerializer)

Note that the Soap* attributes are only considered if the SoapBindingStyle.Document is set for the Style property of the binding, in ASP.NET Web Services, otherwise, its the Xml* attributes.

No Comments