今天剛出爐的,百度空間裡也有
今天應朋友的需要寫了個簡單的txt檔案內容去重複的js代碼。
這段代碼可以處理txt檔案中的重複資料,但是前提是該txt檔案每行是一條資料記錄。
使用時,將該代碼檔案儲存為任意名稱的.html檔案與你的txt源檔案放到一個目錄中,
運行該html檔案,輸入源txt檔案的名稱(注意不要帶尾碼.txt)點“去重”彈出框選擇是
就會顯示程式運行資訊,並在該檔案目錄下產生一個test.txt檔案。
例如:
目錄結構:
D:/test/src.txt源檔案
D:/test/test.html程式檔案
D:/test/test.txt被組建檔案
代碼:
源檔案:src.txt
13252491322132463
1323474226755
1323484945088
132340913294263
132377234213223
1323572633507
132592922813222
1323772329827
1325289426908
132529132280304
1323720690326
132871327636838
1323484945088
132522944132363
13238913228765
處理檔案 test.html
<html>
<body>
<IE:Download ID="oDownload1" STYLE="behavior:url(#default#download)" />
<script>
var my_array = new Array();
function onDownloadDone(downData){
document.getElementById('showImport1').innerHTML=downData;
}
function txt2array(downData){
my_array = downData.split("/r/n");
//document.write('去重複後:'+downData);
//CreateFile();
}
function CreateFile(tt)
{
var fso, tf;
fso = new ActiveXObject("Scripting.FileSystemObject");
tf = fso.CreateTextFile("test.txt", true);
tf.WriteLine(tt) ;
tf.Close();
}
Array.prototype.distinct=function(){
var a=[],b=[];
for(var prop in this){
var d = this[prop];
if (d===a[prop]) continue; //防止迴圈到prototype
if (b[d]!=1){
a.push(d);
b[d]=1;
}
}
return a;
}
function dosub(){
var filname= document.getElementById('txtid').value;
oDownload1.startDownload(filname+'.txt',txt2array);
var test1 =my_array.distinct();
var getstr =test1.toString();
re = /,/g;
var strtrans =getstr.replace(re,"/r/n");
CreateFile(strtrans);
document.getElementById('show_info').innerHTML="去重複成功:<br/>源檔案行數:"+my_array.length+'去除後行數為:'+test1.length;
}
//document.write("去重複成功:<br/>源檔案行數:"+my_array.length+'去除後行數為:'+test1.length);
//document.write('去重複後:'+my_array.distinct()+"<hr/>"+my_array.distinct().length);
</script>
<table width="500" border="0" align="center" cellpadding="0" cellspacing="0">
<tr onmouseover="this.style.background='#FFFFFF'" onmouseout="this.style.background=''">
<td width="120" align="center"><strong>說明</strong></td>
<td align="left"><strong>txt檔案去重複</strong></td>
</tr>
<tr onmouseover="this.style.background='#FFFFFF'" onmouseout="this.style.background=''">
<td align="center">源檔案名稱</td>
<td align="left"><label><input style="width:120px;" id="txtid" name="txtname" type="text" value="" /></label>
<font color='red'>檔案資料必須按行儲存</font></td>
</tr>
<tr onmouseover="this.style.background='#FFFFFF'" onmouseout="this.style.background=''">
<td width="120" align="center">操作</td>
<td align="center"> www.2cto.com
<input type="button" name="sub1" id="sub1" value="去重" onclick="javascript:dosub();"/>
</td>
</tr>
</table>
<div id='show_info'></div>
</body>
</html>
作者:zmyxmjz