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

时间:2009-11-05 16:05:01

标签: windows unicode utf-8

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

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

5 个答案:

答案 0 :(得分:29)

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

尝试类似:

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

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

答案 1 :(得分:4)

我不知道从上下文菜单中,但notepad++允许您更改文件编码,它有一个宏选项...所以您可以自动化该过程

答案 2 :(得分:3)

如果您导入包含以下内容的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\""

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

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

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

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

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

答案 3 :(得分:1)

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

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

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

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

答案 4 :(得分:1)

这是一个很好的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>