Updated DeletedOld CodeProject Article

After an extremely long time I’ve made an update to the DeleteOld console app and updated the CodeProject article.  The original was .NET 1.1 – this one’s now 2.0 (I’m not on 3 yet!)

It still does the same things it did – deletes files of a specified age, but now also:

  • Allows files to be deleted ‘newer’ than a certain age as well as ‘older’
  • Allows files to be deleted based on an absolute date (overriding the timeframe arguments)
  • Allows the ‘root’ path to be preserved if ‘remove empty folders’ is selected and the path specified is empty.
  • Fixed a bug in Arguments parsing regex as noted by dudik
  • Changed output datetime format to ‘full’ rather than specific dd/mm/yyyy as noted by a.plus.01

I had a need for the absolute date, so thought I may as well cover off a few other things. 

Hope it’s of some use…

You can also see my other CodeProject articles.

Serving SSL from localhost on IIS 5 on Windows XP using Self-Signed certificates

I’ve probably glossed past this a few times, but it turns out getting SSL working on your dev machine is less painful than you’d think…

  1. install iis60rkt.exe from http://www.microsoft.com/downloads/details.aspx?FamilyID=56fc92ee-a71a-4c73-b628-ade629c89499&DisplayLang=en on your local machine. It’s intended for IIS 6.0 but it works
    in IIS 5.1.
  2. Select custom install if you only want to use SelfSSL.
    Otherwise, install all.
    There’s a lot of other useful stuff there.
  3. Select Programs –> IIS Resources –> SelfSSL –> SelfSSL
    command prompt
  4. In the command prompt, you’ll see a list of all possible
    command parameters..
    1. Type ‘selfssl.exe
      /V:60’     This indicates the certificate is valid for 60 days
    2. Type Y to answer the
      question “Do you want to replace the SSL settings for site 1 (default website)
      ?
    3. Type ‘iisreset’ to
      restart IIS.

If your browser prompts something like ‘Certification Authority not recognised’ (in Firefox), just put the url in the exception list or override the warnings when viewing the page.


Thanks to Ricky for getting this info together.

Setting up a Continuous Integration .NET Build Server without Visual Studio

We start with Windows XP SP2 (doesn’t really need to be a ‘server’).  My requirement is to rely on the .NET framework and open source tools – i.e. not requiring a Visual Studio Licence.  We’re using .NET 2.0 so everything will be in reference to that.


I assume here that you’ll be familiar with the actual software below and just want to get a Build Server up and running without having to install Visual Studio.  If you’re not familiar with Continuous Integration then start by looking at Martin Fowler’s Continuous Integration article and then the info on CruiseControl.NET, as that’s the tool that pulls everything together. 


There’s help on each of the sites below for installing and using each of the tools, so I won’t go into detail about each one.  The order of the list isn’t critical, but you’ll probably have some issues unless all are done.



  1. Install IIS from Add/Remove Windows Components (used for CruiseControl.NET) if not already present
  2. Open up a command prompt and run C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -i (to install ASP.NET in IIS)
  3. Install CruiseControl.NET (1.4) from http://confluence.public.thoughtworks.org/display/CCNET/Download.  NOTE:  If you’re using a remote SubVersion repository (as in my case) then you’ll need to run the CruiseControl.NET Service as a domain user that has access to the network location of the repository  rather than LocalSystem.  This is because CruiseControl will be using the SVN client to poll for changes. 
  4. Install Microsoft .NET Framework 2.0 SP1 from http://www.microsoft.com/downloads/details.aspx?familyid=029196ed-04eb-471e-8a99-3c61d19a4c5a&displaylang=en
  5. Install latest SubVersion (1.5) from http://www.collab.net/downloads/subversion/
  6. Install appropriate NUnit from http://nunit.org/index.php?p=download (targeting .NET framework 2.0 – I have 2.4.6)
  7. Install latest FxCop from http://www.microsoft.com/downloads/details.aspx?familyid=3389F7E4-0E55-4A4D-BC74-4AEABB17997B
  8. Install .NET Framework 2.0 SDK from http://www.microsoft.com/downloads/details.aspx?familyid=fe6f2099-b7b4-4f47-a244-c96d69c35dec&displaylang=en (you’ll need this to get around a NAnt bug whereby it can’t resolve an internal property to load the .NET framework)
  9. Download latest NAnt from http://nant.sourceforge.net/
  10. Download latest NAntContrib from http://nantcontrib.sourceforge.net/ (You’ll need this to run MSBuild)
  11. To save getting errors when building Web Projects (and other types) the easiest thing is to copy c:\program files\msbuild from your dev machine to the build server (otherwise you’ll have to alter the path of where the targets are in each project).

Once you’ve got all of these set up then you’ll be able to add some builds to your ccnet.config file ( most likely c:\program files\CruiseControl.NET\server\ccnet.config)


Test CruiseControl’s happy by going to http://localhost/ccnet It should come up OK but basically show ‘no projects’.


I’m not going to go into detail about setting up the builds here but may cover that in a follow up article, as it requires some more setup with SVN and a ‘standard’ project structure (to get benefit from reuse of build scripts).  If you’re still with me then I’ll share one last thing which might help… 


Some people choose to have NAnt in a standard place and just reference it from the PATH, but I now use the power of svn:externals to drag in NAnt, NUnit and other common external dependencies like MS Enterprise Library from a shared SVN location to each project.  This means you just get latest of a project and it has ‘everything’ needed to build – no installations or assumptions about locations of tools.

Microsoft Guidance Explorer – An alternative to WikidPad?

I tried Guidance Explorer again after a good while (for the purposes of documenting standards and processes), and concluded that it wasn’t going to be up to the job because printing wasn’t supported. 

I’ve been using WikidPad for a while and have been struggling with it’s
usability so I had another google around the printing capability of Guidance
Explorer (my eventual medium is likely to be some other wiki-like product, so
this is likely to just be my ‘input medium’).  I found the (rather
obvious) Guidance
Explorer Overview
on Codeplex that explains that printing is indirectly
supported through the export formats. I tried it and – whoa!! I take it all
back. 

The export to Word format at different levels and different views
is just great.  It gives you an index at the top with links to the actual content.  The formatting’s nice too.  The HTML format’s just the same as the Word doc visually, but spits out the content to separate files which is a bit nicer for deployment.

I still can’t believe how little-known Guidance Explorer is, but much of the Patterns and Practices work is documented, along with many other resources.  I’ll certainly be saying Bye bye to WikidPad.

NAnt Solution Task bug – Error checking whether ‘xxx’ is an enterprise template project.

Just spent a little while looking into this.  I thought I’d be clever and add my build script to the solution it builds so I can get some intellisense from Resharper (although it’s somewhat limited).  This added the file into ‘Solution Items’ which is a virtual folder.

The error appears because NAnt expects to find valid project files in the solution folders.  I had the same error previously with Web Site Projects (as these are also ‘special folders’). 

I’m using NAnt 0.86 (Build 0.86.2898.0; beta1; 8/12/2007), and from the bug report it seems that it’s fixed in 0.86 beta 2 (not yet out) so you’ll have to get a nightly build if you’re desperate.

My workaround for the moment is to use the NAntContrib MSBuild task to build my Web Deployment project (which in turn builds all the dependencies), as follows:

<target name=”build.webdeploy” description=”Compiles a Web Deployment Project using MSBuild”>
  <msbuild project=”${project.dir}\mydeploydir\mydeployproj.wdproj” verbosity=”Detailed” >
     <arg value=”/p:Configuration=${build.configuration}” />
  </msbuild>
</target>


You can fill in the blanks yourself with the parameters.

Who needs Internet Explorer when you can use it in Firefox?

I love Firefox and its addons, although I still habitually use Internet Explorer.  IE6 at work (which always provides some nice CSS challenges!) and IE7 at home.


I’m a bit slow off the mark with using firefox, but the latest addon that someone sent me yesterday may have tipped the balance.


The IE Tab addon actually uses the IE rendering engine to render in a new tab in Firefox.  It’s quite amusing when you right-click and get the IE context menu.


The really great thing about this is that you can then use the power of the other Firefox addons to develop for IE.  Here’s my list of ‘must haves’.



  • Firebug.  This is quite simply the best addon.  Hit F12 and you can instantly see (and manipulate) all sorts of things in the comfort of your own browser.
  • YSlow.  Great Javascript profiler
  • FireCookie. See and change your cookies (for development purposes of course! – Good to test the security of your apps)
  • Web Developer.  Bit of overlap with Firebug but some cool features.

The IE Developer Toolbar is still a pretty good tool, but Firebug just gives you more info when you’re debugging CSS (which is inevitable with IE).


 

Search SQL Server Stored Procedures for any text string


Here’s the SQL script I probably use most when I’m trying to work out dependencies and scope of development changes.  Query Analyzer allows you to search objects (hit F4 – it’s cool!) and find objects by ‘name’, but there’s no obvious way to search stored procs for a particular string.


SQL Server hides Stored Procedure code in the sysComments table (in each database), and so it’s possible to search.  This is another script I found from not sure where (so can’t really take credit).  It does a great job of scanning and even gives you line numbers.  It would be great to be able to just shove this into the master database, but you need to be in the context of the database you’re searching in (or it would have to be amended somewhat to run some more ‘dynamic’ SQL). 


I have previously front-ended this for development databases with an ASP.NET page that simply runs the query substituting a parameter for the search text and connecting to the correct database.  The results show nicely in a grid.


Here’s the code – just paste into a new query in your chosen database, change @SearchTerm and away you go…


 


SET NOCOUNT ON
GO

declare 
@dbname sysname
,@SearchTerm    varchar(100)
,@BlankSpaceAdded   
int
,@BasePos       int
,@CurrentPos    int
,@TextLength    int
,@LineId        int
,@AddOnLen      int
,@LFCR          int –lengths of line feed carriage return
,@DefinedLength int
,@obj_name    varchar(255)
,@prev_obj_name 
varchar(255)


/* NOTE: Length of @SyscomText is 4000 to replace the length of
** text column in syscomments.
** lengths on @Line, #CommentText Text column and
** value for @DefinedLength are all 255. These need to all have
** the same values. 255 was selected in order for the max length
** display using down level clients
*/
,@SyscomText    nvarchar(4000)
,@Line          
nvarchar(255)

set @SearchTerm ‘MyTable’
set @dbname db_name()


set @SearchTerm ‘%’ + @SearchTerm + ‘%’

Select @DefinedLength 255
SELECT @BlankSpaceAdded /*Keeps track of blank spaces at end of lines. Note Len function ignores
                             trailing blank spaces*/
CREATE TABLE #CommentText
(obj_name 
varchar(255)
 ,LineId    
int
 
,Text  nvarchar(255collate database_default)

/*
**  Make sure the @objname is local to the current database.
*/
if @dbname is not null and @dbname <> db_name()
        
begin
                raiserror
(15250,-1,-1)
        
end

    begin

        DECLARE 
ms_crs_syscom  CURSOR LOCAL
        FOR SELECT 
so.name, sc.text FROM syscomments sc
    
inner join sysobjects so
    
on so.id = sc.id
    where 
so.type ‘P’
    
AND sc.encrypted 0
                
ORDER BY number, colid

        
FOR READ ONLY
    
end

/*
**  Get the text.
*/
SELECT @LFCR 2
SELECT @LineId 1


OPEN ms_crs_syscom

FETCH NEXT FROM ms_crs_syscom into @obj_name, @SyscomText

WHILE @@fetch_status >0
BEGIN

    SELECT  
@BasePos    1
    
SELECT  @CurrentPos 1
    
SELECT  @TextLength = LEN(@SyscomText)

    
IF @prev_obj_name <> @obj_name
        
–start again
        
SELECT @LineId 1
    
    
WHILE @CurrentPos  !0
    
BEGIN
        
–Looking for end of line followed by carriage return
        
SELECT @CurrentPos =   CHARINDEX(char(13)+char(10), @SyscomText, @BasePos)

        
–If carriage return found
        
IF @CurrentPos !0
        
BEGIN
            
/*If new value for @Lines length will be > then the
            **set length then insert current contents of @line
            **and proceed.
            */
            
While (isnull(LEN(@Line),0) + @BlankSpaceAdded + @CurrentPos-@BasePos + @LFCR) > @DefinedLength
            
BEGIN
                SELECT 
@AddOnLen @DefinedLength-(isnull(LEN(@Line),0) + @BlankSpaceAdded)
                
INSERT #CommentText VALUES
                
( @obj_name,
          @LineId,
                  
isnull(@Line, N”) + isnull(SUBSTRING(@SyscomText, @BasePos, @AddOnLen), N”))
                
SELECT @Line = NULL, @LineId @LineId + 1,
                       @BasePos 
@BasePos + @AddOnLen, @BlankSpaceAdded 0
            
END
            SELECT 
@Line    = isnull(@Line, N”) + isnull(SUBSTRING(@SyscomText, @BasePos, @CurrentPos-@BasePos + @LFCR), N”)
            
SELECT @BasePos @CurrentPos+2
            
INSERT #CommentText VALUES( @obj_name, @LineId, @Line )
            
SELECT @LineId @LineId + 1
            
SELECT @Line = NULL
        END
        ELSE
        
–else carriage return not found
        
BEGIN
            IF 
@BasePos <@TextLength
            
BEGIN
                
/*If new value for @Lines length will be > then the
                **defined length
                */
                
While (isnull(LEN(@Line),0) + @BlankSpaceAdded + @TextLength-@BasePos+1 ) > @DefinedLength
                
BEGIN
                    SELECT 
@AddOnLen @DefinedLength – (isnull(LEN(@Line),0) + @BlankSpaceAdded)
                    
INSERT #CommentText VALUES
                    
( @obj_name,
              @LineId,
                      
isnull(@Line, N”) + isnull(SUBSTRING(@SyscomText, @BasePos, @AddOnLen), N”))
                    
SELECT @Line = NULL, @LineId @LineId + 1,
                        @BasePos 
@BasePos + @AddOnLen, @BlankSpaceAdded 0
                
END
                SELECT 
@Line = isnull(@Line, N”) + isnull(SUBSTRING(@SyscomText, @BasePos, @TextLength-@BasePos+1 ), N”)
                
if LEN(@Line) < @DefinedLength and charindex(‘ ‘, @SyscomText, @TextLength+1 ) > 0
                
BEGIN
                    SELECT 
@Line @Line + ‘ ‘, @BlankSpaceAdded 1
                
END
            END
        END
    END
    
–Save previous name
    
SET @prev_obj_name @obj_name
    
FETCH NEXT FROM ms_crs_syscom into @obj_name, @SyscomText
END

IF 
@Line is NOT NULL
    INSERT #
CommentText VALUES( @obj_name, @LineId, @Line )

select from #CommentText 
where  patindex(@SearchTerm, text) <> 0

CLOSE  ms_crs_syscom
DEALLOCATE     ms_crs_syscom

DROP TABLE     #CommentText




Colorized by: CarlosAg.CodeColorizer

Generate SQL Inserts from existing data

This is one from the archives that obviously got away.  I had need for this today so thought I’d post so I don’t have to look so hard next time!


It’s pretty much a case of create the SP in your chosen database and run with a tablename as the parameter.  I originally found this on one of the SQL sites (probably SQLServerCentral), and amended it slightly to add some basic filtering in the @where parameter.


There’s a few limitations from memory – with blob, image and text fields, but for your average tables this will probably do the trick just fine.


I’ve also used a different method that basically builds up a single INSERT with SELECT ‘literal values’ UNION SELECT ‘literal values’.  That works fine and is potentially easier to control transaction-wise as everthing’s in a single statement.  Anyway here’s the code….

if exists (select from dbo.sysobjects where id = object_id(N'[dbo].[usp_InsertGenerator]’and OBJECTPROPERTY(idN’IsProcedure’1)
drop procedure [dbo].[usp_InsertGenerator]
GO

CREATE PROC dbo.usp_InsertGenerator
(@tableName 
varchar(100), @where varchar(1000= NULLas
 
–Declare a cursor to retrieve column specific information for the specified table
DECLARE cursCol CURSOR FAST_FORWARD FOR 
SELECT 
column_name,data_type FROM information_schema.columns WHERE table_name @tableName
OPEN cursCol
DECLARE @string nvarchar(3000–for storing the first half of INSERT statement
DECLARE @stringData nvarchar(3000–for storing the data (VALUES) related statement
DECLARE @dataType nvarchar(1000–data types returned for respective columns
SET @string=‘INSERT ‘+@tableName+‘(‘
SET @stringData=
 
DECLARE @colName nvarchar(50)
 
FETCH NEXT FROM cursCol INTO @colName,@dataType
 
IF @@fetch_status<>0
 
begin
 print 
‘Table ‘+@tableName+‘ not found, processing skipped.’
 
close curscol
 
deallocate curscol
 
return
END
 
WHILE 
@@FETCH_STATUS=0
BEGIN
IF 
@dataType in (‘varchar’,‘char’,‘nchar’,‘nvarchar’)
BEGIN
 SET 
@stringData=@stringData+””+”’+isnull(””’+””’+’+@colName+‘+””’+””’,”NULL”)+”,”+’
END
ELSE
if 
@dataType in (‘text’,‘ntext’–if the datatype is text or something else 
BEGIN
 SET 
@stringData=@stringData+””””’+isnull(cast(‘+@colName+‘ as varchar(2000)),””)+”””,”+’
END
ELSE
IF 
@dataType ‘money’ –because money doesn’t get converted from varchar implicitly
BEGIN
 SET 
@stringData=@stringData+”’convert(money,”””+isnull(cast(‘+@colName+‘ as varchar(200)),”0.0000”)+”””),”+’
END
ELSE 
IF 
@dataType=‘datetime’
BEGIN
 SET 
@stringData=@stringData+”’convert(datetime,’+”’+isnull(””’+””’+convert(varchar(200),’+@colName+‘,121)+””’+””’,”NULL”)+”,121),”+’
END
ELSE 
IF 
@dataType=‘image’ 
BEGIN
 SET 
@stringData=@stringData+””””’+isnull(cast(convert(varbinary,’+@colName+‘) as varchar(6)),”0”)+”””,”+’
END
ELSE 
–presuming the data type is int,bit,numeric,decimal 
BEGIN
 SET 
@stringData=@stringData+””+”’+isnull(””’+””’+convert(varchar(200),’+@colName+‘)+””’+””’,”NULL”)+”,”+’
END
 
SET 
@string=@string+@colName+‘,’
 
FETCH NEXT FROM cursCol INTO @colName,@dataType
END
DECLARE 
@Query nvarchar(4000)
 
SET @query =‘SELECT ”’+substring(@string,0,len(@string)) + ‘) VALUES(”+ ‘ substring(@stringData,0,len(@stringData)-2)+”’+”)” FROM ‘+@tableName
IF @where IS NOT NULL
 SET 
@query @query + ‘ where ‘ + @where
 
exec sp_executesql @query
 
CLOSE cursCol
DEALLOCATE cursCol
GO


Colorized by: CarlosAg.CodeColorizer