I am trying to convert UTF-8 to ANSI files. With little bit of knowledge and help of Google I found one line to convert SINGLE file
Get-Content C:\Output2\PA01.094 | Set-Content C:\Output\PA01094 -Encoding Ascii
Now I want to convert all UTF-8 files in a folder to another folder without changing filename.
The following will read all files in $sourceFolder
and re-create them under $destFolder
encoded as ASCII.
$sourceFolder = "c:\temp\src"
$destFolder = "c:\temp\dst"
Get-ChildItem -Path $sourceFolder |
foreach-object {
get-content $_ | Set-content -Path ( Join-Path $destFolder $_.Name ) -Encoding ASCII
}
NB This code DOES NOT verify the encoding of the original file.
You could use code like below. Modify Get-ChildItem as required to specify files you need.
$sourcePath = "C:\source"
$destinationPath = "C:\output"
if (!(Test-Path $destinationPath))
{
New-Item -ItemType Directory -Path $destinationPath
}
Get-ChildItem -Path $sourcePath -File | ForEach-Object {
Write-Host "Converting $_"
$content = Get-Content $_.FullName
Set-content (Join-Path -Path $destinationPath -ChildPath $_) -Encoding Ascii -Value $content
}
ASCII encoding cannot handle all characters that UTF8 or other Unicode encodings can handle, character that can't be translated may result in ? in output file.
To check encoding of output you can use PowerShell.
For example for a text file created in Notepad that says "Hello, World!"
The following encodings will produce these results. Note start of UTF-8 has special characters, these indicate file is UTF-8, not the default Save format in Notepad.
PS C:\support> [System.IO.File]::ReadAllBytes("C:\support\helloworld_ansi.txt")
72
101
108
108
111
44
32
87
111
114
108
100
33
PS C:\support> [System.IO.File]::ReadAllBytes("C:\support\helloworld_unicode.txt")
255
254
72
0
101
0
108
0
108
0
111
0
44
0
32
0
87
0
111
0
114
0
108
0
100
0
33
0
PS C:\support> [System.IO.File]::ReadAllBytes("C:\support\helloworld_utf8.txt")
239
187
191
72
101
108
108
111
44
32
87
111
114
108
100
33
PS C:\support>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.