Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for txtcomp.ps

(view source code of txtcomp.ps as plain text)

  1. param(
  2. 	[string]$file1,
  3. 	[string]$file2,
  4. 	[switch]$All,
  5. 	[switch]$h,
  6. 	[parameter( ValueFromRemainingArguments = $true )]
  7. 	[string]$invalidArgs
  8. )
  9.  
  10.  
  11. function Show-Help {
  12. 	param ( [string]$errormessage )
  13.  
  14. 	if ( $errormessage -ne "" ) {
  15. 		Write-Host
  16. 		Write-Host "Error: " -ForegroundColor Red -NoNewline
  17. 		Write-Host $errormessage -ForegroundColor White
  18. 	}
  19.  
  20. 	Write-Host
  21. 	Write-Host "TxtComp.ps1,  Version 1.01"
  22. 	Write-Host "Compare two text files and display the differences"
  23. 	Write-Host
  24. 	Write-Host "Usage:  " -NoNewline
  25. 	Write-Host "powershell ./TxtComp.ps1  file1  file2  [ -All ]" -ForegroundColor White
  26. 	Write-Host
  27. 	Write-Host "Where:  " -NoNewline
  28. 	Write-Host "file1 " -ForegroundColor White -NoNewline
  29. 	Write-Host "and " -NoNewline
  30. 	Write-Host "file2" -ForegroundColor White -NoNewline
  31. 	Write-Host "    are the files to be compared"
  32. 	Write-Host "        -All               " -ForegroundColor White -NoNewline
  33. 	Write-Host "displays " -NoNewline
  34. 	Write-Host "All " -ForegroundColor White -NoNewline
  35. 	Write-Host "lines, not just the differences"
  36. 	Write-Host
  37. 	Write-Host "Written by Rob van der Woude"
  38. 	Write-Host "http://www.robvanderwoude.com"
  39. 	Write-Host
  40.  
  41. 	Exit 1
  42. }
  43.  
  44.  
  45. $file1 = "$file1".Trim( )
  46. $file2 = "$file2".Trim( )
  47.  
  48. if ( $h -or ( $file1 -eq "" ) -or ( $file1 -eq "/?" ) -or ( $file1 -eq "--help" ) -or ( $file2 -eq "/?" ) -or ( $file2 -eq "--help" ) ) {
  49. 	Show-Help
  50. }
  51.  
  52. if ( $file2 -eq "" ) {
  53. 	Show-Help "Please specify TWO files to compare"
  54. }
  55.  
  56. if ( [string]$invalidArgs -ne "" ) {
  57. 	if ( ( $invalidArgs -eq "/?" ) -or ( $invalidArgs -eq "--help" ) ) {
  58. 		Show-Help
  59. 	} else {
  60. 		Show-Help "Invalid command line argument(s)"
  61. 	}
  62. }
  63.  
  64. if ( $file1 -eq $file2 ) {
  65. 	Show-Help "Cannot compare a file to itself"
  66. }
  67.  
  68. if ( -not ( Test-Path $file1 ) ) {
  69. 	Show-Help "File `"$file1`" not found"
  70. }
  71.  
  72. if ( -not ( Test-Path $file2 ) ) {
  73. 	Show-Help "File `"$file2`" not found"
  74. }
  75.  
  76. if ( ( $file1 -eq $file2 ) -or ( $file1 -eq "" ) -or ( $file2 -eq "" ) ) {
  77. 	Show-Help
  78. }
  79.  
  80. Compare-Object $( Get-Content $file1 ) $( Get-Content $file2 ) -IncludeEqual:$All
  81.  

page last modified: 2024-02-26; loaded in 0.0255 seconds