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

xiaoxiao2026-05-22  21

[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");} 相关资源:敏捷开发V1.0.pptx
转载请注明原文地址: https://www.6miu.com/read-5049177.html

最新回复(0)