javascript 实现的文件拷贝(能够循环遍历所选文件夹)

[b]javascript 实现的文件拷贝[/b]
刚学习js,做了个文件拷贝练习,只在IE下实现了,火狐不好用,有待改进........
代码如下,仅供参考.
FileCopy.html

<html>
<head>
<title>FileCopy LX</title>
<script type="text/javascript" src="FileCopy.js"></script>
</head>
<body>
<div align="center">
<h1><font color="#00eeff">File Copy</font> </h1>
<hr color="#0099ff"/>
</div>
<br/>
<div>
<table align="left">
<tr>
<td><font size="5">Choose Folder CopyFrom: </font></td>
<td>
<input type="text" name="pathFrom" id="pathFrom" value ="" size="100" />
<input type="button" name="btnSelectPath" id="btnSelectPath" value ="Chose Folder" onclick="browseFolder('pathFrom')"/>
<hr color="#A4D3EE"/>
</td>
</tr>
</table>
</div>
<br/><br/><br/><br/>
<div>
<table align="left">
<tr>
<td><font size="5">Choose Folder CopyTo:    </font></td>
<td>
<input type="text" name="pathTo" id="pathTo" value ="" size="100" />
<input type="button" name="btnSelectPath" id="btnSelectPath" value ="Chose Folder" onclick="browseFolder('pathTo')"/>
<hr color="#A4D3EE"/>
</td>
</tr>
</table>
</div>
<br/><br/><br/>
<hr color="RGB(250,120,120)"/>
<div >
<div ><font color="#436EEE"><b>Please write the name of files you wanted to copy to the table</b></font></div>
<input type="button" name="clearTable" id ="clearTable" value="clear talbe" onclick="clearTable()"/>
<table id = "myTable" border="1" width="40%" bordercolor="#BCD2EE" cellpadding="0" cellSpacing="0" align="center">
<tr >
<td align="center">the name of the files must be contains the Extension </td>
</tr>
<tr>
<td>
<input type="text" name="file" id="file0" value="" size="100"/>
</td>
</tr>
<tr>
<td>
<div align="right" >
<input type="button" name="addRow" id="addRow" value="addRow" onclick="addRow('myTable')"/>
</div>
</td>
</tr>
</table>
</div>
<hr color="RGB(250,120,120)"/>
<div align="right" >
<input type="button" name="btnRun" id="btnRun" value="Run" onclick="run()"/>
</div>
</body>
</html>


FileCopy.js


/*put files' names in the array*/
var fileNames = new Array();

/* select folder */
function browseFolder(id){
try{
var Message = "Please choose the Folder!";
var shell = new ActiveXObject("Shell.Application");
var folder = shell.BrowseForFolder(0,Message,64,17);//start my computer

//var folder = shell.BrowseForFolder(0,Message,0);//start my zhumian

if( folder != null ){
var folders = folder.items();
var myFolder = folders.item();
var myPath = myFolder.Path;

//alert(myPath);
if(myPath.charAt(myPath.length - 1) != "\\"){
myPath = myPath + "\\";
}
document.getElementById(id).value = myPath;
return myPath;
}

}catch(e){
alert("wrong!");
}
}

/* add rows to table*/
function addRow(myTable){
var objTable = document.getElementById(myTable);
var rowLength = objTable.rows.length;
var newRow = objTable.insertRow(rowLength-1);//insert before 'rowLength-1'
var newCell = newRow.insertCell(0);
var fileId = rowLength-newRow.rowIndex-1
//var txt = "<input type='text'"+" name='file"+ fileId + "'" + "id ='file" +fileId+ "'" + " value='' size='100'/>"
var txt = "<input type='text'"+" name='file' id ='file" +fileId+ "'" + " value='' size='100'/>"
newCell.innerHTML =txt;
//alert(document.getElementById("file"+fileId).value);
//alert(rowLength);
}

/* clear table's contents*/
function clearTable(){
var objIdex;
var objInput = document.getElementsByName("file");

for (var i=0;i<objInput.length;i++){
var txtInput = objInput[i].value;
if (txtInput != null) {
//alert(objInput[i].value);
objInput[i].value = "";
}
}
}

/* copy the file*/
function copyFile(myFile){
var toPath = document.all.pathTo.value;
var fso = new ActiveXObject("Scripting.FileSystemObject");
var fFile = fso.GetFile(myFile);
fFile.Copy(toPath);
}

/*searching files,copy it after founded */
function searchFiles(fFolder){
var subFolder = new Enumerator(fFolder.SubFolders);

for (; !subFolder.atEnd();subFolder.moveNext()){
searchFiles(subFolder.item());
}

var fFiles = new Enumerator(fFolder.files);

for (; !fFiles.atEnd(); fFiles.moveNext()){
var myFile = fFiles.item();
var xIndex;

for ( xIndex in fileNames ) {
if (fileNames[xIndex] == myFile.name) {
// find the file's name and copy it
copyFile(myFile);
}
}
}
}

/* get all the names of files*/
function getAllFilesNames(){
var objIdex;
var objInput = document.getElementsByName("file");

for (var i=0;i<objInput.length;i++){
var txtInput = objInput[i].value;
if (txtInput != null) {
//alert(objInput[i].value);
fileNames[fileNames.length] = objInput[i].value;
}
}
//alert("getNames:" + fileNames);
}

/* check input contents*/
function checkInput(){
// var strFrom = document.all["pathFrom"].value;
var strFrom = document.getElementById("pathFrom").value;
//alert("strFrom:"+strFrom);
if (strFrom == "") {
alter("please choose the copyFrom Folder!");
document.getElementById("pathFrom").focus();
return false;
}

var strTo = document.all["pathTo"].value;
if (strTo == "") {
alter("please choose the copyTo Folder!");
document.getElementById("pathTo").focus();
return false;
}

if (fileNames.length <= 0) {
alter("please write the file's name which you want to copy!");
document.getElementById("file0").focus();
return false;
}
}

/* main run */
function run(){
checkInput();

var fso = new ActiveXObject("Scripting.FileSystemObject");

var folder = fso.GetFolder(document.all.pathFrom.value);

getAllFilesNames();
searchFiles(folder);
alert("finish");
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值