Friday, April 13, 2007

File merge with Powershell

Powershell, is a great tool for handy scripting. It is often needed that you need to write sql scripts in isolated files. The is good in terms of finding errors in script, and focusing on one part while developing part of the sytem.But , as the number of script files grows big , the deployment issue becomes a headache for the release manager to handle your scripts.

I used to think that the best solution, for merging files, is to use dos copy /xcopy commend, but this often gets screwed up for really big scripts having evernt large srings within.

Though .Net has rich libray for that,it not aways easy to make a console app which i can share with my collegues, therefore i started tiny a powershell that made my life more easier.

The goal of my power shell, i will give source dirercoty , exetenstion(filetype to be included in merge) and optionally a diliminator(for ex. i want to have "GO" keyword at end of each sql file).


Logic:

Get all files for provided extenstion
Read the whole content, and append it in a string builder
Add the diliminator
Finally Combine them to one file.


Code

Param ($source="c:\Test", $extenstion ="*.sql", $outputFile = "outputFile.sql" , $blockTerminator = "GO" )

[System.IO.DirectoryInfo]$directoryInfo = New-Object System.IO.DirectoryInfo($source);
$rgFiles = $directoryInfo.GetFiles($extenstion);

$builder = New-Object System.Text.StringBuilder;

foreach ($fileInfo in $rgFiles)
{
[System.IO.FileStream]$fReader = $fileInfo.OpenRead();

if (-not ($fileInfo -eq $null))
{

write $fileInfo.Name;
$reader = New-Object System.IO.StreamReader($fReader);
$builder.AppendLine($reader.ReadToEnd());
$builder.AppendLine($blockTerminator);
}
}

[System.IO.FileStream]$fWriter = New-Object System.IO.FileStream($outputFile, [System.IO.FileMode]::OpenOrCreate);
$writer = New-Object System.IO.StreamWriter($fWriter);

$writer.Write($builder.ToString());
$writer.Flush();
$writer.Close();



Terminology

Param - This excepts user input from shell, optionally you set default value
All the status methods in .net are called with double "::".
Classes are declared with fully quilifed name
New-Object : use for class declartion , optionally you can use -comObject to declare com objects.


How to use

Copy the code , paste it in a text file , rename it to .ps1 extenstion.
Get the exeution poiicy of your shell enviroment , by default it is Restricted , you can know that from calling Get-ExecutionPolicy command. In this mode you cant run scriptlets. Use Set-ExecutionPolicy RemoteSinged.
Run the script , do add the ".\" sign before your file name.


Example

PS C:\Users\mehfuz> .\FileMerge.ps1 c:\community\ *.sql c:\output\merged.sql GO

A Reader's Toolbox

For web page designers powershell is a useful tool as this can help in good designs. Many internet marketers focus of design of their website because this is one factor that any search engine looks for ranking. There are few things that you should consider in hosting of your website. Whenever you launch any new website, you should go for webhosting review so that you can make right choice. There are many internet companies providing internet phone for setting up your call center and pc backup support for any kind of troubleshooting.

3 comments:

Loudenvier said...

Do you mind me asking why in heavens you complicated this matter so much? Try the following and check to see if works for you:

Get-Content *.xml | Out-File test.xml

That will do the trick for you...

Unknown said...

How to Prepare for GED Test through Pre GED Test Options That Work
ged book 2011

Unknown said...
This comment has been removed by the author.