$lookupTable = @{
'¿' = '|'
'Ù' = '|'
'À' = '|'
'Ú' = '|'
'³' = '|'
'Ä' = '-'
}
$original_file = 'C:\FilePath\NOV_1995.txt'
$destination_file = 'C:\FilePath\NOV_1995_NEW.txt'
Get-Content -Path $original_file | ForEach-Object {
$line = $_
$lookupTable.GetEnumerator() | ForEach-Object {
if ($line -match $_.Key)
{
$line = $line -replace $_.Key, $_.Value
}
}
$line
} | Set-Content -Path $destination_file
function Replace-Characters
{
<#
.SYNOPSIS
Short Description
.DESCRIPTION
Detailed Description
.EXAMPLE
Remove-Something
explains how to use the command
can be multiple lines
.EXAMPLE
Remove-Something
another example
can have as many examples as you like
#>
[CmdletBinding()]
param (
$lookupTable = @{ '¿' = '|';'Ù' = '|';'À' = '|';'Ú' = '|';'³' = '|';'Ä' = '-'},
[string] $original_file,
[string] $destination_file
)
$output=@()
$contents = Get-Content -Path $original_file
$linecounter = 0
$max = $contents.Count
foreach ($line in $contents){
$linecounter++
write-progress -Activity 'Checking lines' -status "line # $linecounter of $max" -PercentComplete ($linecounter/$max * 100)
$lookupTable.GetEnumerator() | ForEach-Object {
if ($line -match $_.Key) {
$line = $line -replace $_.Key, $_.Value
}
}
$output += $line
}
$output | out-file -FilePath $destination_file
}
replace-characters -original_file $original_file -destination_file $destination_file
$filelist = get-childitem c:\filepath\
foreach ($file in $filelist){
$original_file = $file.fullname
$destination_file = $original_file.Substring(0,$original_file.length-4) + '_NEW.txt'
$destination_file
G:\Documents\WindowsPowershell\Scripts\replace-characters.ps1 -original_file $original_file -destination_file $destination_file
}
$lookupTable = @{
'¿' = '|'
'Ù' = '|'
'À' = '|'
'Ú' = '|'
'³' = '|'
'Ä' = '-'
}
$patterns = $lookupTable.GetEnumerator() | Select -ExpandProperty Value -Unique | ForEach `
{
$value = $_
[pscustomobject] @{
pattern = ($lookupTable.GetEnumerator() | Where { $_.value -eq $value } | Select -ExpandProperty Name) -join "|"
replacement = $value
}
}
$sourcedir = 'C:\filepath'
Get-ChildItem $sourcedir -File | ForEach `
{
$filepath = Split-Path $_.FullName -Parent
$destinationfile = "$($_.BaseName)_NEW$($_.Extension)"
$destination = Join-Path $filepath $destinationfile
Write-Host "Source: $($_.Name) Destination: $destinationfile"
Get-Content -Path $_.FullName -ReadCount 0 | ForEach-Object {
$line = $_
foreach ($pattern in $patterns)
{
Write-Host "Matching pattern ""$($pattern.pattern)"" and replacing with ""$($pattern.replacement)""" -ForegroundColor Yellow
$line = $line -replace $pattern.pattern,$pattern.replacement
}
$line
} | Set-Content -Path $destination
}
Open in new window
$filelist = get-childitem c:\inputdir
foreach ($file in filelist){
$original_file = $file.fullname
$destination_file = $original_file.Substring(0
replace-characters $original_file $destination_file
}