Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将文件另存为unicode的脚本_Unicode_Powershell_Cygwin - Fatal编程技术网

将文件另存为unicode的脚本

将文件另存为unicode的脚本,unicode,powershell,cygwin,Unicode,Powershell,Cygwin,您知道我可以通过编程或scrirpt将以ansi字符编码保存的一组文本文件转换为unicode编码的任何方法吗 当我用记事本打开文件并选择将其保存为unicode文件时,我也会这样做。将System.IO.StreamReader(读取文件内容)类与System.Text.Encoding.Encoding(创建进行编码的编码器对象)基类一起使用。伪代码 Dim系统、文件、目录、新文件、旧文件 读取常数=1,写入常数=2,外观常数=3 常数AnsiFile=-2,UNICODIBLE=-1 设置

您知道我可以通过编程或scrirpt将以ansi字符编码保存的一组文本文件转换为unicode编码的任何方法吗


当我用记事本打开文件并选择将其保存为unicode文件时,我也会这样做。

将System.IO.StreamReader(读取文件内容)类与System.Text.Encoding.Encoding(创建进行编码的编码器对象)基类一起使用。

伪代码

Dim系统、文件、目录、新文件、旧文件

读取常数=1,写入常数=2,外观常数=3 常数AnsiFile=-2,UNICODIBLE=-1

设置system=CreateObject(“Scripting.FileSystemObject…”

Set file=system.GetFile(“text1.txt”)

设置oldFile=file.OpenAsTextStream(用于读取、解析)

contents=oldFile.ReadAll()

旧文件。关闭

system.CreateTextFile“text1.txt”

Set file=system.GetFile(“text1.txt”)

设置newFile=file.OpenAsTextStream(用于写入,Unicode)

写入内容

新建文件。关闭


希望这种方法能奏效。

您可以使用iconv。在Windows上,您可以在Cygwin下使用它

iconv -f from_encoding -t to_encoding file

最简单的方法是获取内容“path/to/text/file”| out file“name/of/file”

,默认为Unicode

如果要编写一批脚本,可以执行以下操作

$files = get-childitem 'directory/of/text/files' 
foreach ($file in $files) 
{
  get-content $file | out-file $file.fullname
}

您可以创建一个新的文本文件,并将原始文件中的字节写入新文件,在每个原始字节之前放置一个“\0”(假设原始文本文件是英文的)。

这可能对您有用,但请注意,它将抓取当前文件夹中的每个文件:


Get-ChildItem | Foreach-Object { $c = (Get-Content $_); `
Set-Content -Encoding UTF8 $c -Path ($_.name + "u") }
为了简洁起见,使用别名也是一样:


gci | %{ $c = (gc $_); sc -Encoding UTF8 $c -Path ($_.name + "u") }
Steven Murawski建议改用
Out File
。两种cmdlet之间的区别如下:

  • Out文件
    将尝试格式化接收到的输入
  • 输出文件
    的默认编码是基于Unicode的,而
    设置内容
    使用系统的默认编码
下面是一个假设文件
test.txt
在两种情况下都不存在的示例:


PS> [system.string] | Out-File test.txt
PS> Get-Content test.txt

IsPublic IsSerial Name                                     BaseType          
-------- -------- ----                                     --------          
True     True     String                                   System.Object     

# test.txt encoding is Unicode-based with BOM

事实上,如果您不需要任何特定的Unicode编码,还可以执行以下操作将文本文件转换为Unicode:


PS> Get-Content sourceASCII.txt > targetUnicode.txt

Out File
是一个带有可选参数的“重定向”操作符"诸如此类。

复制并查看为什么被接受的答案与Cygwin有关?问题被标记为powershell…是的,一开始我在寻找powershell解决方案,但结果证明这对我来说非常有效,我也可以使用Cygwin。无论如何,给出的所有回复似乎都是有效的方法,使用该文件会有所帮助我使用的是PS V5.1

PS> Get-Content sourceASCII.txt > targetUnicode.txt