In one of the projects developed in my firm we had encoding set on almost all the files to Something like CodeBase 1252 which supports only the limited set of characters.

There was a problem with Swedish characters “ö,ä,å” and also especially pound currency character which was used in one of the regular expression in the app. With my operating system, configured with the English culture settings using CodeBase 1250, those characters where changed and there were couple of runtime errors. We had to change the encoding to UTF8 that’s one of the solutions. UTF8 has a bigger character set.

There is an option in Visual Studio File –> Advanced Save Options that can be used to change encoding but it’s only usable for scenarios with one file. In this situation I had to change couple thousand of files. To fix this problem I have found a nice solution that uses PowerShell .

Script:

function ChangeEncoding ($baseDirectory)
{
        $allFiles = Get-ChildItem $baseDirectory -include *.aspx,*.ascx -recurse 
               | where-object { -not $_.PSIsContainer}
        foreach( $file in $allFiles){
            $fileContent = get-content $file.FullName -force
            $fileContent | set-content -encoding utf8  $file.FullName -force
         }
}
ChangeEncoding("BaseDirectoryPath")

This script iterates through all the files in directories and their directories and rewrites them with correct encoding. It’s not the best solution but it works.

Little Explanation:

Get-ChildItem $baseDirectory -include .aspx,.ascx –recurse

Gets all the files with specified extensions  (aspx and ascx) , recurse option enables search through all the files in directories and subdirectories

where-object { -not $_.PSIsContainer}

Used to exclude directories from the files list.

This script loads the file to a temporary variable because we have to read and write the same file. Pipelined solution would be cleaner but it reads and writes the data one line at time. You can’t write and read on the same file at the same time.

Share