在 Windows 上递归更改文件的编码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1681568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:23:36  来源:igfitidea点击:

Change files' encoding recursively on Windows?

windowsunicodeutf-8

提问by Pekka

Does anybody know a tool, preferably for the Explorer context menu, to recursively change the encoding of files in a project from ISO-8859-1 to UTF-8 and other encodings? Freeware or not too expensive would be great.

有没有人知道一个工具,最好是资源管理器的上下文菜单,可以递归地将项目中的文件编码从 ISO-8859-1 更改为 UTF-8 和其他编码?免费软件或不太贵会很棒。

Edit:Thanks for the answers, +1 for all of then. But I would really like to be able to just right click a folder and say "convert all .php files to UTF-8". :) Further suggestions are appreciated, starting a bounty.

编辑:感谢您的回答,+1。但我真的希望能够右键单击一个文件夹并说“将所有 .php 文件转换为 UTF-8”。:) 进一步的建议表示赞赏,开始赏金。

回答by

You could easily achieve something like this using Windows PowerShell. If you got the content for a file you could pipe this to the Out-File cmdlet specifying UTF8 as the encoding.

您可以使用 Windows PowerShell 轻松实现类似的功能。如果您获得了文件的内容,您可以将其通过管道传输到指定 UTF8 作为编码的 Out-File cmdlet。

Try something like:

尝试类似:

Get-ChildItem *.txt -Recurse | ForEach-Object {
$content = $_ | Get-Content

Set-Content -PassThru $_.Fullname $content -Encoding UTF8 -Force}  

回答by Mark

I don't know about from the context menu, but notepad++allows you to change file encodings and it has a macro option... so you could automate the process

我不知道上下文菜单中的内容,但是notepad++允许您更改文件编码,并且它有一个宏选项...因此您可以自动化该过程

回答by Oleg

If you import a test.regfile having the following contain

如果您导入test.reg包含以下内容的文件

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\shell\ConvertPHP]
@="convert all .php files to UTF-8"

[HKEY_CLASSES_ROOT\Directory\shell\ConvertPHP\command]
@="cmd.exe /c C:\TEMP\t.cmd php \"%1\""

After this you will receive the menu item "convert all .php files to UTF-8" in the context menu of explorer on every directory. After the choosing of the item the batch program C:\TEMP\t.cmdwill be started with "php" string as the first parameter and the quoted directory name as the second parameter (of cause the first parameter "php" you can skip if it is not needed). The file t.cmdlike

在此之后,您将在每个目录的资源管理器的上下文菜单中收到菜单项“将所有 .php 文件转换为 UTF-8”。选择项目后,批处理程序C:\TEMP\t.cmd将以“php”字符串作为第一个参数,引用的目录名称作为第二个参数(因为第一个参数“php”,如果不需要,可以跳过)。该文件t.cmd

echo %1>C:\TEMP\t.txt
echo %2>>C:\TEMP\t.txt

can be used to prove that all this work.

可以用来证明所有这些工作。

So you can decode the *.php files with any tool which you prefer. For example you can use Windows PowerShell (see the answer of Alan).

因此,您可以使用您喜欢的任何工具解码 *.php 文件。例如,您可以使用 Windows PowerShell(请参阅 Alan 的回答)。

If you want that the extension like PHP will be asked additionally you can write a small program which display the corresponding input dialog and then start the Windows PowerShell script.

如果您希望像 PHP 这样的扩展程序被额外询问,您可以编写一个显示相应输入对话框的小程序,然后启动 Windows PowerShell 脚本。

回答by bgmCoder

I know this answer is late-coming, but here are two commandline apps to convert encoding. Just make a batch-file wrapper for one, and add it to your * key in the registry.

我知道这个答案来晚了,但这里有两个命令行应用程序来转换编码。只需为一个批处理文件包装器,并将其添加到注册表中的 * 键。

http://www.autohotkey.com/forum/topic10796.html

http://www.autohotkey.com/forum/topic10796.html

http://www.gbordier.com/gbtools/stringconverter.htm

http://www.gbordier.com/gbtools/stringconverter.htm

I used the stringconvertorby adding it as a button in my file-manager, FreeCommanderXE. It only converts one file at a time, but I can click on one, and push the convert button, then click on the next.

我通过将stringconvertor添加为文件管理器FreeCommanderXE 中的按钮来使用它。它一次只转换一个文件,但我可以单击一个文件,然后按下转换按钮,然后单击下一个。

回答by Niente3

Here's a nice ASP recursive converter, you need IIS running on your computer:

这是一个不错的 ASP 递归转换器,您需要在您的计算机上运行 IIS:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<HTML>
<HEAD>
<TITLE>Charset Converter - TFI 13/02/2015</TITLE>
</HEAD>
<BODY style='font-family:arial;font-size:11px;color:white;background-color:#7790c4;font-size:15px'>
<H1 style='color:yellow'>Recursive file charset converter</H1>
by TFI 13/02/2015<BR><BR>
<%
totalconverted=0

Function transcoder( ANSIFile)
   UFT8FileOut=ANSIFile&".tempfile" 
   Set oFS    = CreateObject( "Scripting.FileSystemObject" )
   Set oFrom  = CreateObject( "ADODB.Stream" )
   sFFSpec    = oFS.GetAbsolutePathName(ANSIFile)
   Set oTo    = CreateObject( "ADODB.Stream" )
   sTFSpec    = oFS.GetAbsolutePathName(UFT8FileOut)
   oFrom.Type    = 2 'adTypeText
   oFrom.Charset = fromchar '"Windows-1252"
   oFrom.Open
   oFrom.LoadFromFile sFFSpec
   oTo.Type    = 2 'adTypeText
   oTo.Charset = tochar '"utf-8"
   oTo.Open
   oTo.WriteText oFrom.ReadText
   oTo.SaveToFile sTFSpec,2
   oFrom.Close
   oTo.Close
   oFS.DeleteFile sFFSpec
   oFS.MoveFile sTFSpec,sFFSpec
End Function

Function ConvertFiles(objFolder, sExt, bRecursive, fromchar, tochar)
    Dim objFile, objSubFolder
    For each objFile in objFolder.Files
        If Ucase(fso.GetExtensionName(objFile)) = ucase(sExt) Then
            transcoder objFile.path
            totalconverted=totalconverted+1
            response.write "&bull; Converted <B>"&fso.GetAbsolutePathName(objFile)&"</B> from <B>"&fromchar&"</B> to <B>"&tochar&"</B><BR>"
        End If
    Next

    If bRecursive = true then
        For each objSubFolder in objFolder.Subfolders
            ConvertFiles objSubFolder, sExt, true, fromchar, tochar
        Next
    End If
End Function

sFolder=request.form("sFolder")
sExtension=request.form("sExtension")
fromchar=request.form("fromchar")
tochar=request.form("tochar")
sSubs=request.form("sSubs")
if sSubs="1" then
    sub1=True
else
    sub1=false
end if  

if len(sExtension)=0 then sExtension="asp"
if len(sFolder)>0 and len(fromchar)>0 and len(tochar)>0 then

    Dim fso, folder, files, NewsFile, sFolder, objFSO, strFileIn, strFileOut
    Set fso = CreateObject("Scripting.FileSystemObject")
    'sFolder = "C:\inetpub\wwwroot\naoutf8"
    ConvertFiles fso.GetFolder(sFolder), sExtension, Sub1, fromchar, tochar
    response.write "<hr><br>Total files converted: "&totalconverted&"<BR><BR>New conversion?<br><br>"
end if
%>  
<FORM name=ndata method=post action="UTF8converter.asp">
<TABLE cellspacing=0 cellpadding=5>
<TR>
    <TD>Folder to process:</TD>
    <TD><INPUT name=sFolder style='width:350px' placeholder="C:\example"></TD>
</TR>   
<TR>
    <TD>Extension:</TD>
    <TD><INPUT name=sExtension style='width:50px' value='asp'> (default is .asp)</TD>
</TR>
<TR>
    <TD>Process subfolders:</TD>
    <TD><INPUT type=checkbox name=sSubs value='1' checked></TD>
</TR>
<TR>
    <TD>From charset:</TD>
    <TD><select name=fromchar>
    <option value="big5">charset=big5 - Chinese Traditional (Big5)
    <option value="euc-kr">charset=euc-kr - Korean (EUC)
    <option value="iso-8859-1">iso-8859-1 - Western Alphabet
    <option value="iso-8859-2">iso-8859-2 - Central European Alphabet (ISO)
    <option value="iso-8859-3">iso-8859-3 - Latin 3 Alphabet (ISO)
    <option value="iso-8859-4">iso-8859-4 - Baltic Alphabet (ISO)
    <option value="iso-8859-5">iso-8859-5 - Cyrillic Alphabet (ISO)
    <option value="iso-8859-6">iso-8859-6 - Arabic Alphabet (ISO)
    <option value="iso-8859-7">iso-8859-7 - Greek Alphabet (ISO)
    <option value="iso-8859-8">iso-8859-8 - Hebrew Alphabet (ISO)
    <option value="koi8-r">koi8-r - Cyrillic Alphabet (KOI8-R)
    <option value="shift-jis">shift-jis - Japanese (Shift-JIS)
    <option value="x-euc">x-euc - Japanese (EUC)
    <option value="utf-8">utf-8 - Universal Alphabet (UTF-8)
    <option value="windows-1250">windows-1250 - Central European Alphabet (Windows)
    <option value="windows-1251">windows-1251 - Cyrillic Alphabet (Windows)
    <option value="windows-1252" selected>windows-1252 - Western Alphabet (Windows)
    <option value="windows-1253">windows-1253 - Greek Alphabet (Windows)
    <option value="windows-1254">windows-1254 - Turkish Alphabet
    <option value="windows-1255">windows-1255 - Hebrew Alphabet (Windows)
    <option value="windows-1256">windows-1256 - Arabic Alphabet (Windows)
    <option value="windows-1257">windows-1257 - Baltic Alphabet (Windows)
    <option value="windows-1258">windows-1258 - Vietnamese Alphabet (Windows)
    <option value="windows-874">windows-874 - Thai (Windows)
    </select></TD>
</TR>
<TR>
    <TD>To charset:</TD>
    <TD><select name=tochar>
    <option value="big5">big5 - Chinese Traditional (Big5)
    <option value="euc-kr">euc-kr - Korean (EUC)
    <option value="iso-8859-1">iso-8859-1 - Western Alphabet
    <option value="iso-8859-2">iso-8859-2 - Central European Alphabet (ISO)
    <option value="iso-8859-3">iso-8859-3 - Latin 3 Alphabet (ISO)
    <option value="iso-8859-4">iso-8859-4 - Baltic Alphabet (ISO)
    <option value="iso-8859-5">iso-8859-5 - Cyrillic Alphabet (ISO)
    <option value="iso-8859-6">iso-8859-6 - Arabic Alphabet (ISO)
    <option value="iso-8859-7">iso-8859-7 - Greek Alphabet (ISO)
    <option value="iso-8859-8">iso-8859-8 - Hebrew Alphabet (ISO)
    <option value="koi8-r">koi8-r - Cyrillic Alphabet (KOI8-R)
    <option value="shift-jis">shift-jis - Japanese (Shift-JIS)
    <option value="x-euc">x-euc - Japanese (EUC)
    <option value="utf-8" selected>utf-8 - Universal Alphabet (UTF-8)
    <option value="windows-1250">windows-1250 - Central European Alphabet (Windows)
    <option value="windows-1251">windows-1251 - Cyrillic Alphabet (Windows)
    <option value="windows-1252">windows-1252 - Western Alphabet (Windows)
    <option value="windows-1253">windows-1253 - Greek Alphabet (Windows)
    <option value="windows-1254">windows-1254 - Turkish Alphabet
    <option value="windows-1255">windows-1255 - Hebrew Alphabet (Windows)
    <option value="windows-1256">windows-1256 - Arabic Alphabet (Windows)
    <option value="windows-1257">windows-1257 - Baltic Alphabet (Windows)
    <option value="windows-1258">windows-1258 - Vietnamese Alphabet (Windows)
    <option value="windows-874">windows-874 - Thai (Windows)
    </select></TD>
</TR>
</TABLE><BR>
    <INPUT TYPE=BUTTON onClick='if(document.ndata.sFolder.value.length>0)document.ndata.submit()'value='Convert folder and subfolders'>
</FORM> 
</BODY>
</HTML>