Tuesday, October 12, 2010

JQuery Template Plugin

JQuery officially released new plugins. Please check here . http://blog.jquery.com/2010/10/04/new-official-jquery-plugins-provide-templating-data-linking-and-globalization/

Example.
I would describe only main steps.

Step 1. Include file
<script src="<%= Url.Content("~/Scripts/jquery.tmpl.js") %>" type="text/javascript"></script>
Step - 2.  Template Structure :
<
<table width="100%">
<tr id=rec${UserName}>
<td width="20%">${FirstName}</td>
<td width="20%">${LastName}</td>
<td width="20%">${UserName}</td>
<td width="20%">${Email}</td>
<td align="center" width="5%" title="Edit"><img src="<%
<td align="center" width="5%" title="Delete"><img src="<%
</tr>
</table>
script id="userTemplate" type="text/x-jquery-tmpl"> =Url.Content("~/Images/IconEditRecord.png") %>" alt="Edit" onmouseover="this.style.cursor='pointer';" onclick=showdialog('${UserName}'); /></td>=Url.Content("~/Images/IconXRed.png") %>" alt="Delete" onmouseover="this.style.cursor='pointer';" onclick="deleteUser(this,'${UserName}');" /></td></

Step 3 - Where to render
<table id="UserList">

Step 4 - Ajax Call and Append Data
 



Monday, October 11, 2010

Powershell script that uses 7-zip to compress all files in a parameterizable folder

Powershell script that uses 7-zip to compress all files in a parameterizable folder that match the file pattern provided by the user.Any file that has not been modified in more than a particular time will be compressed. The compressed archive should be verified using the command-line compression tool. Once the compressed archive is validated, then the original file should be deleted. All arguments should be passed to the script at the command-line.
(Please not that this script has not been fully tested and can really be harmful for the system, try it at your own risk :D)

Param
($SourcePath = $(Read-host "Please enter the source directory path e.g. d:\data\")),
($TargetPath = $(Read-host "Please enter the target path e.g. d:\all\archive.zip")),
($Days = $(Read-Host "Files older than days?")),
($Pattern = $(Read-Host "Enter file pattern, e.g. *.txt"))
$CurrentDate = Get-Date
$LimitDate = $CurrentDate.AddDays(-$Days)
$Files  =  get-childitem $SourcePath  -include $Pattern -recurse | where {$_.LastWriteTime -le $LimitDate}
$FileNames = ""
foreach ($File in $Files)
{
write-host "File Name : $File " $File.LastWriteTime
$FileNames = $FileNames + " " + $File 
}
write-host "Files to compress : " $FileNames
if($FileNames.Trim() -ne "")
{
powershell  "&'c:\program files\7-zip\7z'" a $TargetPath   $SourcePath+$Pattern
#powershell.exe  "&'c:\program files\7-zip\7z'" t $TargetPath *.* -r
        foreach ($File in $Files)
 {
 write-host "Deleting file :"$File
 remove-item $File
 }
}
else
{
write-host "No files to compress"
}

write-host "Operation successful!"

Test 7zip archive

7z t archive.zip *.doc -r
Test the integrity of .doc files in the archive. where -r is recursive (subdirectories) switch.

7z t archive.zip *.* -r
Test the integrity of all files.

Friday, October 8, 2010

Powershell script to compress files

I have used 7zip as a command line compression tool.  http://www.7-zip.org/
Following script compresses all files that match specific file pattern e.g. *.txt, *.doc, *.png etc.
It should also work with Universal Naming Convention(UNC) Paths. e.g.
file://10.10.10.63/SharedDocs/archive.zip  etc.

$Path = $(Read-Host "Enter Path and file pattern e.g. c:\zip\*.txt")
$Files  =  get-childitem $Path  -include $Pattern -recurse
$FileNames = ""
foreach ($File in $Files)
{
write-host "File Name : $File " $File.LastWriteTime
$FileNames = $FileNames + " " + $File 
}
write-host "Files to compress : " $FileNames
if($FileNames.Trim() -ne "")
{
powershell.exe -noexit "&'c:\program files\7-zip\7z'" a c:\target\archive.zip $FileNames
}
else
{
write-host "No files to compress :D"
}

Thursday, October 7, 2010

7-Zip command line

Download and install 7-Zip.
http://www.7-zip.org/

7z.exe a c:\a.7z "C:\Program Files"
compresses "C:\Program Files" completely, including all subfolders.

 7z a  c:\a.zip c:\dir\*.txt
compresses all files wih extension txt

7z.exe a c:\a.7z c:\file1.txt d:\dir2\file2.txt
compress multiple files at different locations
7z.exe e c:\test\archive.zip
extract all files to the current location

7z.exe e c:\test\archive.zip -oc:\test\AllFiles
extract all files to the specified path i.e. c:\test\AllFiles

Powershell commands

This is an extract of what I have read here. http://www.powershellpro.com/powershell-tutorial-introduction/powershell-tutorial-conditional-logic/

Powershell commands are called Cmdlet (Command-Let).
get-command, List all powershell commands
get-command -verb Get, List all commands starting with word Get
get-command -noun service, List all commands ending with word service
get-help get-service, Get help for a particular command i.e. get-service
remove-item c:\test.txt, Removes the specified item
get-childItem c:\zip , Get items and subitems from a specified location
get-childItem c:\zip *.txt, Get all txt files from specified location
get-date , Get current date
======== Script to add 2 days to the current day ==========
$Now = Get-Date
$LastWrite = $Now.AddDays(2)
Write-Host $LastWrite
===========================================================
======= Display full path of file and last edited date time =============
$Files  =  $Files = get-childitem c:\zip  -include *.txt -recurse
foreach ($File in $Files)
{
write-host "File Name : $File " $File.LastWriteTime
}
=========================================================================

PowerShell Comparison Operators:

Operator Description
-eq Equal to
-lt Less than
-gt Greater than
-ge Greater than or Eqaul to
-le Less than or equal to
-ne Not equal to

Space in the file path - Powershell script file

If there is space in the file path then following technique can be used to execute that script file.

Powershell.exe -noexit "&'c:\program files\check_memory.ps1'"

Powershell

Powershell Tutorial is available at http://www.powershellpro.com/powershell-tutorial-introduction/

How to Write a PowerShell Script
Reference : http://www.suite101.com/content/how-to-write-a-powershell-script-a115669


There are no restrictions as to where the scripts are placed on the computer, but they must always have the same file name extension - .ps1.

An Example PowerShell Script

The best way to learn about scripting is to examine a real example, in this case a script that will:
  • list all running processes that are using more that 1M of memory
  • ignore the PowerShell process
  • sort the result
  • place the result in a file formatted for viewing on a web site
The script is quite simple, although it is worth noting the use of pipes (represented by |). Pipes allow the output of one Cmdlet to be used as the input to another:
get-process |
where-object {$_.WS -ge 1048576} |
where-object {$_.processname -ne "powershell"} |
sort-object WS –descending |
convertto-html -property Name,WS > c:\Inetpub\wwwroot\ps.html
If this script is saved to “c:\powershell\check_memory.ps1” then it can be run from the command prompt by typing:
powershell c:\powershell\check_memory
However, life is never quite that simple.

Enabling PowerShell Scripts

Anyone running a PowerShell at this point will probably receive an error something like:
File c:\powershell\check_memory.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about_signing" for more details.
At line:1 char:26
+ c:\powershell\check_memory <<<<
That’s because PowerShell does not automatically allow scripts. It actually has four levels of security (known as execution policies):
  • Restricted: This is the default execution policy, and stops all scripts from running
  • AllSigned: Allows scripts to run, but only if they have an associated digital signature from a trusted publisher.
  • RemoteSigned: Allows all scripts on the computer to run and any other scripts that have an associated digital signature from a trusted publisher
  • Unrestricted: Allows all scripts to run (but this should be avoided)
The current execution policy can be examined from the command prompted:
powershell get-executionpolicy
It will, in all likelihood, return “Restricted”. It’s then just a matter of setting the execution policy to be less restrictive:
powershell set-executionpolicy RemoteSigned
The script can now be run and the results examined in the HTML file that it produces.


Read more at Suite101: How to Write a PowerShell Script: An Introduction to Automating PowerShell http://www.suite101.com/content/how-to-write-a-powershell-script-a115669#ixzz11gSjWgZD

Friday, October 1, 2010

Repository Pattern

This will be an ongoing blog post that will coverup Domain driven design (DDD) using Repository pattern and its implementation.
In this post I will also show unit testing using repository pattern.
To be continued....