ASP批量修改文件编码类型程序,GB2312与UTF-8互转。

将以下代码保存为ASP文件即可。

 

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
Option Explicit
Session.CodePage = 65001
Response.Charset = "UTF-8"
Response.Buffer = true
dim i

Sub Header()
Response.Write "<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">" & vbcrlf
Response.Write "<html xmlns=""http://www.w3.org/1999/xhtml"">" & vbcrlf
Response.Write "<head>" & vbcrlf
Response.Write "<meta http-equiv=""Content-Type"" content=""text/html; charset=utf-8"" />" & vbcrlf
Response.Write "<meta http-equiv=""X-UA-Compatible"" content=""IE=EmulateIE7"" />" & vbcrlf
Response.Write "</head>" & vbcrlf
Response.Write "<body>" & vbcrlf
End Sub

Sub Footer()
Response.Write "</body>" & vbcrlf
Response.Write "</html>" & vbcrlf
End Sub

Function Conversion(FolderPath, FolderLevel, strCharset, toCharset, FileType, BackUP, Level)
Dim Fso, Folders, Files, FilePath, FileExt, SubFolders, SubFolder
Dim strContent
Set Fso = Server.CreateObject("Scripting.FileSystemObject")
If Not Fso.FolderExists(FolderPath) Then Exit Function
Set Folders = FSO.GetFolder(FolderPath)
For Each Files In Folders.Files
FilePath = FolderPath & "\" & Files.Name
FileExt = FSO.GetExtensionName(FilePath)
If Instr(FileType,Ucase(FileExt))>0 And Cstr(Server.MapPath(Request.ServerVariables("url")))<>Cstr(FilePath) Then
If BackUP=1 Then
Fso.CopyFile FilePath, FilePath & ".bak"
End If
strContent = ReadFile(FilePath,strCharset)
If Len(strContent)>0 Then
Call CreateFile(strContent, FilePath, toCharset)
Response.Write "<p style=""font-size:12px"">成功转换文件:" & FilePath & "</p>"
Response.Flush
End If
End If
Next
If FolderLevel>0 And Clng(FolderLevel)>Clng(Level) Then Exit Function
Set SubFolders = Folders.SubFolders
For Each SubFolder In SubFolders
Call Conversion (FolderPath & "\" & SubFolder.Name, FolderLevel, strCharset, toCharset, FileType, BackUP, Level + 1)
Next
Set SubFolders=nothing
Set Folders=nothing
End Function


Function ReadFile(FilePath, strCharset)
dim objStream
set objStream=server.CreateObject("Adodb.Stream")
objStream.Type=2
objStream.Mode=3
objStream.Charset=strCharset
objStream.Open
objStream.Loadfromfile FilePath
ReadFile=objStream.ReadText
objStream.Close
set objStream=Nothing
End Function


Function CreateFile(Str, FilePath, strCharset)
dim objStream
Set objStream = CreateObject("Adodb.Stream")
objStream.Type = 2
objStream.Mode = 3
objStream.Charset = strCharset
objStream.Open
objStream.WriteText Str
objStream.SaveToFile FilePath, 2
objStream.Flush
objStream.Close
Set objStream = Nothing
End Function

If Request.QueryString("action")="conversion" then
Dim FolderPath, FolderLevel, strCharset, toCharset, FileType, BackUP
FolderPath = Request.QueryString("FolderPath")
FolderLevel = Request.QueryString("FolderLevel")
strCharset = Request.QueryString("strCharset")
toCharset = Request.QueryString("toCharset")
FileType = Request.QueryString("FileType")
BackUP = Request.QueryString("BackUP")
If Len(FolderPath)=0 Then FolderPath = "./"
Call Conversion(server.MapPath(FolderPath), FolderLevel, strCharset, toCharset, FileType, BackUP, 1)
Response.Write "<p style=""font-size:12px"">全部完成</p>"
Response.End()
End If

%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<script type="text/javascript">
function gogo(){
document.getElementById('step_1').style.display='none';
document.getElementById('step_2').style.display='block';
}
function backback(){
document.getElementById('step_1').style.display='block';
document.getElementById('step_2').style.display='none';
}
</script>
<style type="text/css">
*{margin:0; padding:0}
a{color:#0000FF; text-decoration:none}
a:hover {color:#c00; text-decoration:underline}
li{list-style:none}
img{border:none}
label,img,input{vertical-align:middle}
input.input{height:28px; line-height:28px; padding:0 5px; border:1px inset #bbb; margin-right:6px; background:#fff9e8; font-size:12px; color:#222}
textarea.textarea{padding:5px;border:1px inset #bbb; background:#FFF9E8; font-size:12px; color:#222}
input.submit{width:100px; height:36px; margin-right:5px}
body{font:12px/1.5em Arial,Verdana,Helvetica,sans-serif;color:#333}
.main{border:5px solid #f2faff;width:766px; margin:50px auto 0 auto; padding:2px}
.copy{text-align:center; padding-top:20px}
.install{width:760px;border:1px solid #6db2db; padding:2px}
.title{background:#e9f4fb; height:60px; line-height:60px; overflow:hidden; border-bottom:1px solid #b5cfd9; text-align:center}
.title h1{font-size:20px}
.content table{background:#f6fbff}
.content table th{background:#ecf5fa; width:100px; padding:10px; text-align:right; color:#0079ab; font-weight:normal}
.content table td{padding:10px; background:#fff; color:#333}
</style>
<title>文件编码转换程序 - Powered By Mozheng</title>
</head>
<body>
<form name="form1" action="" method="get" target="progress" onSubmit="return gogo();">
<input name="action" type="hidden" value="conversion" />
<div class="main">
<div class="install">
<div class="content">
<div id="step_1">
<div class="title">
<h1>文件编码转换程序</h1>
</div>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<th>起始目录:</th>
<td><input name="FolderPath" type="text" value="" class="input" size="50" /></td>
</tr>
<tr>
<th>目录层数:</th>
<td><select name="FolderLevel">
<option value="0">&nbsp;全部&nbsp;</option>
<%for i=1 to 20%>
<option value="<%=i%>">&nbsp;<%=i%>级&nbsp;</option>
<%next%>
</select></td>
</tr>
<tr>
<th>原文件编码:</th>
<td><select name="strCharset">
<option value="GB2312">&nbsp;GB2312&nbsp;</option>
<option value="GBK">&nbsp;GBK&nbsp;</option>
<option value="UTF-8">&nbsp;UTF-8&nbsp;</option>
</select></td>
</tr>
<tr>
<th>目标文件编码:</th>
<td><select name="toCharset">
<option value="GB2312">&nbsp;GB2312&nbsp;</option>
<option value="GBK">&nbsp;GBK&nbsp;</option>
<option value="UTF-8">&nbsp;UTF-8&nbsp;</option>
</select></td>
</tr>
<tr>
<th>转换文件类型:</th>
<td>
<label for="FileType_1"><input name="FileType" id="FileType_1" type="checkbox" value="HTML" />HTML</label>
<label for="FileType_2"><input name="FileType" id="FileType_2" type="checkbox" value="HTM" />HTM</label>
<label for="FileType_3"><input name="FileType" id="FileType_3" type="checkbox" value="ASP" />ASP</label>
<label for="FileType_4"><input name="FileType" id="FileType_4" type="checkbox" value="TXT" />TXT</label>
<label for="FileType_5"><input name="FileType" id="FileType_5" type="checkbox" value="JS" />JS</label>
<label for="FileType_6"><input name="FileType" id="FileType_6" type="checkbox" value="CSS" />CSS</label>
<label for="FileType_7"><input name="FileType" id="FileType_7" type="checkbox" value="TPL" />TPL</label>
<label for="FileType_8"><input name="FileType" id="FileType_8" type="checkbox" value="XML" />XML</label>
<label for="FileType_9"><input name="FileType" id="FileType_9" type="checkbox" value="ASPX" />ASPX</label>
<label for="FileType_10"><input name="FileType" id="FileType_10" type="checkbox" value="PHP" />PHP</label>
</td>
</tr>
<tr>
<th>备份原文件:</th>
<td><label for="backup_1"><input name="backup" type="radio" id="backup_1" value="1" checked="checked" />备份</label><label for="backup_1"><input name="backup" type="radio" id="backup_1" value="0" />不备份</label></td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="开始转换" class="submit" />
</td>
</tr>
</table>
</div>

<div id="step_2" style="display:none">
<div class="title">
<h1>转换进度</h1>
</div>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td align="center"><iframe scrolling="auto" frameborder="0" marginheight="0" marginwidth="0" style="width:740px;height:200px" name="progress" id="progress"></iframe></td>
</tr>
<tr>
<td align="center"><input type="button" value="返回" class="submit" οnclick="backback()" /></td>
</tr>
</table>
</div>
</div>
</div>
</div>
<div class="copy">程序制作:魔怔,<a href="http://www.cnblogs.com/mozheng/" target="_blank">http://www.cnblogs.com/mozheng/</a></div>
</form>
</body>
</html>

 

 

转载于:https://www.cnblogs.com/mozheng/archive/2011/10/17/2214523.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值