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.

Freitag, 20. Februar 2009

ISE Extension basics: splitting text to lines

Finally I got the best way to split text to an array of lines. It is using $text -split "`r`n"



Improved thanks to Jaycul's comment.

Montag, 9. Februar 2009

Do you like ISE-Cream?

ISE-Cream is a CodePlexProject to extend ISE the new editor of PowerShell V2 CTP 3.

You can forget all the prehistoric, not windows like copy & paste keys and convert to uniform ^C ^V .

The countdown for this final transistion is allready running.

No actually that is a completely unrelated event, which shows how easy datetime could be handled in databaseworld, if only if were invented by mathematicians.

The database example shows , that it is important to start with a clear vision, otherwise you will have to fix it later with functions like set datefirst. I use this function every day, so with me it is allways saturday. (I didn't chose sunday - in germany the shops would be closed).

Back to PowerShell reality. I started to design some categories, which might help the project in organizing the contributions:

  • basic function [that means reinventing of the wheel]
  • load - save [with session and history]
  • print - export (to html, rtf, etc.)
  • invoke files - scriptblocks - lines - regions
  • editing tasks
  • comment - identify - beautytifier
  • snippets
  • introspection - expand alias - syntax check - tabcompletion
  • other text transforms e.g. sorting of lines
  • ISE caller - profile management
  • Custumization - Custom memue
Happy extending

Bernd

Samstag, 7. Februar 2009

Bernd_k joined the PowerShell ISE-CREAM project

I joined the ISE-CREAM project here and and checked in an editor extension here to open the Explorer with the folder containing the currently edited file.

I like the cooperation between explorer and ISE, often I open files by drag-and-drop from the explorer.


An ISE bug when running in german locale


This is a bug showing up in CTP 3 and Win 7 (the german beta download), when running in german locale.

Correct would be, that all Ctrl+ were replaced by Strg+.

The english Win 7 Beta download does not show the bug.

For the curious Strg is an abbrivation of the german word Steuerung which is a synonym of the german word Kontrolle, which means just what you can guess without use of a dictionary.

Therefore I spell Strg as Stupid-Translated-Regular-Garbage, but as this was not invented by Microsoft, I will complain here.

At last some good news for us PowerShell users. To create a custum menue items we have to use the Ctrl- for all locales.

The replacement in display is task of the OS.

Example:

$psISE.CustomMenu.Submenus.Add("run region 5", {dir }, 'strg+D')

will work in any system.

You can vote for the bug at https://connect.microsoft.com/feedback/ViewFeedback.aspx?FeedbackID=411556&SiteID=99

-Bernd

Mittwoch, 4. Februar 2009

Schmierzettel 2

Hello again,

today I'm very happy, I just installed Windows 7 as dual boot on my vista notebook.

Defragmentation and repartioning took some time. Was there real IT-Live in advance to external USB-drives?

I wrote a little about it in one of my other blogs.

This christmas I got PowerShell V2 CTP3 and just in time to my birthday Karl Prosser started the ISE-Cream project. That's really great.

And don't miss Jaykul's artikel about it’s by design I really enjoyed it.

Greetings

Bernd