Sonntag, 22. März 2009

Using PowerShell to scan through Visual Basic Code

Perhaps you have some old VB6 Projects containing some SQL-queries in SQL-Server Syntax, which you have to check for ORACLE compatibility or some of the old Sybase outer-joins *= or =*.

What you want is a list containing the filename, the name of the function or subroutine and the interesting line of code.

With PowerShell it is rather easy. You start with my Get-VBProject which extracts the files, which contain program code.

VB 6 uses _ at the end of a lines to continue logical lines. I use Get-VBlines to split a VB source code file into logical lines.

Next I transform the text into a stream of objects, representing either a declaration, a property get/set, a function or a modue, with attributes for name of object, filename, position within the file and the coressponding lines of code.

With Select-VBCode_String I can filter objects whoses codelines match artificial patterns.

Adding a little Select-Object and Format-List or Format-Table I get all the answers I need.


It is a nice feature of PowerShell that the streams of objects combine seamless.
Scaning a single file is almost the same as scanning a whole project.


And now the Code:


download | new post

Sonntag, 15. März 2009

Some thoughts about find and search on Windows (part I).

In unix world there are the commands find and grep which help to locate files in the file system. Not trivial, but they do an honest job.

In Windows world search capabilities changed from version to version. In my opinion to the worse.

I still have a running Windows 2000 Box at hand and searching for files is one of rare tasks I still use it.

In Windows XP I first have to kick away some insulting dog.

And Vista is spending a lot of its time indexing the filesystem and nearly never gives the answers I want.

Windows 7 wastes less time indexing, but doesn't answer my questions either.


Perhaps to express my wishes to todays software designers, I have to describe a clear set of working cases. Though that they have to solve my problem or have to admit that they can't handle it.


Work case 1:
I know that I worked on a PowerShell Script some days ago,
it must be located in some dedicated directory or in one of my working directories.
I'm not sure whether it was on a local drive or on a network share.


Using Windows 2000 or Windows XP the OS search function helps.


Using Vista or Windows 7 windows search never found the files I was looking for.




Note 1: PowerShell uses other extension but the simple .ps1 to e.g. .psm1 and .ps1xml


Note 2: Selecting a single root to start the search is not sufficient


Note 3: Not all pathes may be accessiable all the time


Let me show you my PowerShell solution.

Note that I don't like having my files neither on a system partition nor within my profile but on different local drive or net-share.


# list of directories, which contain the places where I use to store my PowerShell scripts
# some are local and some are not wllways connected
$ps_roots = (
'I:\bin\Scripts',
'I:\codeplex\ise-cream',
'I:\PoshCode',
'U:\scripts',
'U:\scripts'
)

# show the pathes that are currently accessible
$ps_roots |? { (Test-path $_) }

# usual PowerShell File Extensions
$ps_extensions = '*.ps1', '*.psm1', '*.ps1xml'

# now show files with any of these extensions in my pathes
$ps_roots |? { (Test-path $_) } | gci -rec -include $ps_extensions


# now show files with any of these extensions in my pathes changed within the last 10 days
$ps_roots |? { (Test-path $_) } | gci -rec -include $ps_extensions| Where-object {((Get-date) - $_.LastWriteTime).days -lt 10 }



Next time I'm going to to show some refinements based on file contents.