I have a bat file that performs some actions and I need to encode a text file with UTF-8 format.
Is there any way to perform this in windows command line??
Thanks in advance.
Only with other programs which may or may not be installed. If you're targetting Windows 7 and higher you could just use PowerShell:
powershell -Command "&{ param($Path); (Get-Content $Path) | Out-File $Path -Encoding UTF8 }" somefile.txt
Related
I'm on Windows 10 with Powershell 5.1
printargs.py is:
#! /usr/bin/python3
import sys
for arg in sys.argv:
print(arg)
Case 1
I have a Windows batch file runme.bat:
chcp 65001
py printargs.py ä
Note: py.exe is the Python launcher for Windows
This is working: I invoke the batch file in a Powershell terminal and I get output
printargs.py
ä
Case 2
Now I want powershell script runme.ps1 doing exactly the same thing:
# What code must go here?
& py printargs.py ä
This is NOT working: Because of some encoding problem I get
printargs.py
ä
I' am aware of this question.
I tried without success:
$OutputEncoding = [console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding
This comes up a lot. Powershell 5.1 can't read utf8 no bom. This will covert the script to utf8 with bom. Powershell 7 can handle utf8 no bom.
(get-content -encoding utf8 script.ps1) | set-content script.ps1 -encoding utf8
# bom tests
(get-content script.ps1 -AsByteStream)[0] -eq 0xef -and
(get-content script.ps1 -AsByteStream)[1] -eq 0xbb
True
# right side gets converted to string
'239 187' -eq (get-content script.ps1 -AsByteStream)[0..1]
True
I try to run two commands in a bat file using the powershell. My goal is to transform a file to a utf8 format. How can I achieve that?
Here is what I have so far:
PowerShell -Command (Get-Content 'ZipCode.csv' | Out-File 'ZipCode1.csv' -Encoding utf8)
I get the following error: "out-file is not recognized as an internal or external command"
The doublequotes seem sufficient to escape the pipe. Single quotes on the outside wouldn't work.
PowerShell "Get-Content ZipCode.csv | Out-File ZipCode1.csv -Encoding utf8"
If you're only using Out-File because your version of PowerShell doesn't include the -Encoding option with Set-Content, then it should read:
#"%__AppDir__%WindowsPowerShell\v1.0\powershell.exe" -NoProfile -Command "Get-Content -Path '.\ZipCode.csv' | Out-File -FilePath '.\ZipCode1.csv' -Encoding UTF8"
Obviously if you have a Version of PowerShell where Set-Content has the required -Encoding option, use it instead:
#"%__AppDir__%WindowsPowerShell\v1.0\powershell.exe" -NoProfile -Command "Get-Content -LiteralPath 'ZipCode.csv' | Set-Content -LiteralPath 'ZipCode1.csv' -Encoding UTF8"
These could obviously be shortened to remove the robustness and use aliases/shorthand:
#PowerShell -NoP "GC '.\ZipCode.csv'|Out-File '.\ZipCode1.csv' -E UTF8"
#PowerShell -NoP "GC -LP 'ZipCode.csv'|SC -LP 'ZipCode1.csv' -En UTF8"
I prefer to use -LiteralPath because I have a tendency to use [] in my file naming, and those can be problematic in filenames. Change the output file name to ZipCode[1], then try the -Set-Content version code with -Path or nothing instead of -LiteralPath/-LP option, and you should see what I mean.
My colleague found this Batch command (Powershell Command in Windows) online and I need to know what "encoding" it is using to output to the file. I know that it is not utf-8. Here is the command:
Select-String logfile.log -pattern "string pattern" | tee-object -filepath "output_file.txt" -append
My Windows edition is Windows Server 2012 RC Standard
Taken from https://technet.microsoft.com/en-us/library/hh849937.aspx
Tee-Object uses Unicode encoding when it writes to files. As a result, the output might not be formatted properly in files with a different encoding. To specify the encoding, use the Out-File cmdlet.
The command Select-String is found in PowerShell.
I search a way to do an automated task with Notepad++ from command line:
Open file
Change encoding to UTF-8
Save file
Is there any way to do it with some plugin or even with other program ?
Why do you want to use Notepad++ for that task? Which OS are you using?
Notepad++ got a Plugin-manager where you can install the Python Script plugin.
http://pw999.wordpress.com/2013/08/19/mass-convert-a-project-to-utf-8-using-notepad/
But if you want to convert files to UTF8 you can do that way easier with PowerShell on Windows or command line on Linux.
For Windows Power-Shell:
$yourfile = "C:\path\to\your\file.txt"
get-content -path $yourfile | out-file $yourfile -encoding utf8
For Linux use (e.g.) iconv:
iconv -f ISO-8859-15 -t UTF-8 source.txt > new-file.txt
Windows Powershell script to change all the files in the current folder (and in all subfolders):
foreach ($file in #(Get-ChildItem *.* -File -Recurse)) {
$content = get-content $file
out-file -filepath $file -inputobject $content -encoding utf8
}
If you want to change only specific files just change the *.* (in the first line).
Note: I tried the pipe (|) approach in Broco's answer and was not working (I got empty output files as Josh commented). I think is because we probably cannot read and write directly from and to the same file (while in my approach I put the content into a memory variable).
I have a bat file where I encode some CSV files. The problem is that there are one character at the begining of the file once the encoding have been done (BOM byte I guess). This character bothers me cause after encoding, I use this file to generate a database.
Here is the line for encoding (inside bat file):
powershell -Command "&{ param($Path); (Get-Content $Path) | Out-File $Path -Encoding UTF8 }" CSVs\\pass.csv
Is there any way to encode the file without BOM (if this is the problem)??
Thanks!
I found the solution.
Just change the line with this:
powershell -Command "&{ param($Path); $Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding($False); $MyFile = Get-Content $Path; [System.IO.File]::WriteAllLines($Path, $MyFile, $Utf8NoBomEncoding) }" CSVs\\pass.csv