1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| $encoding = "ASCII"
[regex]$regex = "^\w\:\\"
foreach ($InputFile in $args) {
Write-Host "ReadFile : Processing $InputFile"
#Convert into complete path if not already done
$FilePath = ""
If ($regex.ismatch($InputFile)) {
$FilePath = $InputFile
}
ElseIf ($regex.ismatch($InputFile.fullname)) {
$FilePath = $InputFile.fullname
}
Else {
$FilePath = "$($pwd)\$InputFile"
}
$InputFile = Get-Item $FilePath
$FullName = $InputFile.FullName
try {
Write-Host "ReadFile : Reading $FullName"
Write-Host "ReadFile : Encoding $encoding"
# Open StreamReader
$reader = new-object System.IO.StreamReader($FullName,[System.Text.Encoding]::$encoding)
[int]$NbLines = 0
# Create empty hashtable
$hash = @{}
while ($reader.Peek() -ge 0) {
$NbLines++
# Read line
$line = $reader.ReadLine()
# Get first 13 characters
if ($line.length -ge 13) {
$key = $line.substring(0, 13)
# If hasn't been stored yet, add it to the hashtable and print it
# This ways only disctinct element will be printed
if(! $hash.ContainsKey($key)) {
$hash.add($key, 1)
Write-Output $key
}
}
}
Write-Host "ReadFile : $NbLines lines read"
}
catch [System.IO.IOException] {
Write-Host $_
}
finally {
if ($reader -ne $null) {
Write-Host "ReadFile : Dispose reader"
$reader.dispose()
$reader.close()
}
}
} |