Mats codemix

Mats codemix

A little c#, a litte .NET and throw in some c++ and you get a nice Spaghetti

Mats codemix RSS Feed
 
 
 
 

Powershell tips - Filter files modified the last n days

I wanted to list files modified the last n days. Here is a filter function i wrote to help me.

 1 function FilterLast([int] $nDays)
 2 {
 3     begin {
 4         $DateToCompare = (Get-date).AddDays(-1 * $nDays)
 5     }
 6 
 7     process
 8     {
 9         %{ if ($_.lastwritetime –gt $DateToCompare) { $_ } }
10     }
11 } 

So now i can write

ls | filterlast -nDays 4


Some good links on SCM - software configuration management

Henrik Kniberg has written some very interesting things on agile version control. He has some links to other interesting sources as well. Go there, read it!

From Perforce I found this writing about High level best practices in SCM.

Is SCM the acronym for:

Both searches on Google seems to to point in the direction that they roughly are equivalent with the “Software Configuration Management” being a little more high level.

Windows Special folders in powershell…

… or where is my wallpaper?

I wanted to edit the imagefile i had as windows desktop background so i wanted to find out the location of it. Excellent material for a powershell lession i thought.

So where are the special folders? Could it be here:

ls ([Environment]::GetFolderPath('MyPictures')

Found help about how to get windows special folders  here and here.

What is [environment+Specialfolder]? Looks like it’s an enum.

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
False    True     SpecialFolder                            System.Enum

So you can list all enum names like this.

A nice way to list all options for special folders is:

[environment+specialfolder]::GetNames([environment+specialfolder])
Desktop
Programs
Personal
MyDocuments
Favorites
Startup
…etc

Eventually i had to use the windows dialog and found out that the wallpaper was in:

 ([Environment]::GetFolderPath('ApplicationData') + "\mozilla\firefox\")

But i learned some new powershell.

Powershelll tips - Filter on container

Say you want to recurse a diretory tree and show just the directories.

Yo can write

ls C:\ -rec | %{if ($_.PSIsContainer) { $_ }} | %{$_.Fullname}

Add a little filter function

1 function FilterContainer()
2 {
3     process
4     {
5         %{if ($_.PSIsContainer) { $_ }}
6     }
7 }

Which reduces the first line to

ls C:\ -rec | FilterContainer | %{$_.Fullname}

Clever SQL

Here are some clever SQL for searching strings via soundex or like, as well as finding gaps in sequences etc.

VB6 and .NET (Visual studio 2008)

Im working on a legacy app written in VB6 and want to write .NET-stuff that integrates with it. I feared that i couldnt have both IDEs installed side by side but it seems to work ok (so far…).  The ideal is to put the VB6 environment in a virtual machine and keep it there.

The installation order was VB6 and then VS2008.

Luckilly our app communicates via the server through a DLL and it will be a (relatively) simple task to replace that with a COM-class written in C#.

SQL-formatter (updated)

Working a lot with SQL at the moment i found a handy tool for formatting SQL-code.

SqlInform automatic SQL code formatter (http://www.sqlinform.com/)

There is an online version but also a free although time-limited JAVA-program to download. The online version handles just 100 lines of code mind you.

Another online formatter without the 100-line limit.  http://www.dpriver.com/pp/sqlformat.htm

Some that i havent tried (yet…)

http://www.orafaq.com/utilities/sqlformatter.htm

http://www.dbainfopower.com/dbaip_free_download_execute.php (free registration )

http://www.wangz.net/cgi-bin/pp/gsqlparser/sqlpp/sqlformat.tpl (Pay)

http://www.ubitsoft.com/products/sqlenlight/sqlenlight.php (pay)

About stored procedures

I found some articles about stored procedures and…

I agree with most of the arguments written in them :-)

My pet peeve about SPs is that its not a good programming language for dealing with logic and control flow. Not at all!

http://statestreetgang.net/post/2008/04/My-Statement-on-Stored-Procedures.aspx

http://weblogs.asp.net/fbouma/archive/2003/11/18/38178.aspx

http://home.comcast.net/~eichler2/misc/AntiSP.htm

http://blog.platinumsolutions.com/node/77

Regex for SQL

Found a regex for getting SQL-code here.

“(/\*[^\*/]*\*/)|(\r\n\s*)|\t|\s{2,}”

Making groups unique - fixing a duplicate key problem in SQL

I ran into a problem where updating a key column caused duplicate key errors.  The table in question had several columns that formed the Primary Key.  One of those columns was userd for a  serial number making groups of keys unique.

Example

Group

GroupSerial

Apple

1

Apple

1

Pear

1

Pear

1

Here we see that each row is not unique.

So how can we fix this?

First we lay up the data like this, having a unique row number:

Row#

Group

GrpSerial

GrpCnt

GrpMax

1

Apple

1

2

Apple

1

3

Pear

1

4

Pear

1

5

Pear

1

Then we count the items in each group and set that value to each member of the group.

Row#

Group

GrpSerial

GrpCnt

GrpMax

1

Apple

1

2

2

Apple

1

2

3

Pear

1

3

4

Pear

1

3

5

Pear

1

3

Then we the the max row# for each group

Row#

Group

GrpSerial

GrpCnt

GrpMax

1

Apple

1

2

2

2

Apple

1

2

2

3

Pear

1

3

5

4

Pear

1

3

5

5

Pear

1

3

5

Now we can calculate the group serial for each row.

grp_serial = row_serial  - grp_max + grp_cnt

Row#

Group

GrpSerial

GrpCnt

GrpMax

1

Apple

1

2

2

2

Apple

2

2

2

3

Pear

1

3

5

4

Pear

2

3

5

5

Pear

3

3

5

Simple as (apple) pie.

And here is an example in SQL:

 1 /*
 2 How to update a group_serial fixing uniqueness
 3 */
 4 
 5 
 6 -- Create a table with a series of groups 
 7 drop TABLE matstest
 8 go
 9 
10 CREATE TABLE matstest
11 (
12     id numeric identity not null
13     ,row_serial int null
14     -- Value columns
15     ,grp int Null
16     ,grp_serial int null
17     -- helper columns
18     ,grp_cnt int null
19     ,grp_max int null
20 )
21 go
22 
23 -- having a unique index on grp,grp_serial would cause an error 
24 truncate table matstest
25 insert into matstest (grp,grp_serial) values (1,1)
26 insert into matstest (grp,grp_serial) values (22,1)
27 insert into matstest (grp,grp_serial) values (22,1)
28 insert into matstest (grp,grp_serial) values (33,1)
29 insert into matstest (grp,grp_serial) values (33,1)
30 insert into matstest (grp,grp_serial) values (33,1)
31 
32 -- Fake a high row_serial just to prove that low numbers 
33 update matstest set row_serial = id +4311
34 
35 -- Show the starting point
36 select * from matstest
37 
38 --
39 -- It is important to include all the keys that make up the group! In this example there is just one column (grp)
40 -- Find count per group (grp_count)
41 --
42 select grp, count(*) as grp_cnt
43 into #tmp_grp_cnt
44 from matstest
45 group by grp
46 having count(*) > 1 -- Skip this to have count also on the groups with one row
47 
48 update matstest
49 set grp_cnt = ga.grp_cnt
50 from matstest m
51 join #tmp_grp_cnt ga on ga.grp = m.grp
52 
53 --
54 -- Find max row row_serial number per group  (grp_max)
55 --
56 select grp, max(row_serial) as grp_max
57 into #tmp_grp_max
58 from matstest
59 group by grp
60 having count(*) > 1
61 
62 update matstest
63 set grp_max = ga.grp_max
64 from matstest m
65 join #tmp_grp_max ga on ga.grp = m.grp
66 
67 --
68 -- Now we can calculate the serial number per group
69 -- Serial number per group = row_serial - (grp_max) + (grp_cnt)
70 --
71 update matstest set grp_serial = row_serial - (grp_max) + (grp_cnt) where grp_cnt is not null
72 
73 
74 -- show the final result
75 select * from matstest
76 
77 /*
78 
79 -- Starting point
80        id    row        grp grps cnt max
81 1    1    4312    1    1    [NULL]    [NULL]
82 2    2    4313    22    1    [NULL]    [NULL]
83 3    3    4314    22    1    [NULL]    [NULL]
84 4    4    4315    33    1    [NULL]    [NULL]
85 5    5    4316    33    1    [NULL]    [NULL]
86 6    6    4317    33    1    [NULL]    [NULL]
87 
88 
89 -- Final result
90        id    row        grp grps cnt max
91 1    1    4312    1    1    [NULL]    [NULL]
92 2    2    4313    22    1    2    4314
93 3    3    4314    22    2    2    4314
94 4    4    4315    33    1    3    4317
95 5    5    4316    33    2    3    4317
96 6    6    4317    33    3    3    4317
97 */