特别注意: 只在IE浏览器中运行有效
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
</head>
<body>
<table width="341" border="1">
<tr>
<td width="331" height="50">
<form name="form1" method="post" action="">
原文件路径:<input type="text" name="text1" value="E:\ss.txt"><br>
目前文件路径:<input type="text" name="text2" value="E:\sss.txt">
<input type="button" name="Button" value="复制" onclick="filecopy(document.form1.text1,document.form1.text2)">
</form>
</td>
</tr>
<tr>
<td width="331" height="50">
<form name="form2" method="post" action="">
文件路径:<input type="text" name="text3" value="ceshidel.txt">
<input type="button" name="Button" value="删除" onclick="filedelete(document.form2.text3)">
</form>
</td>
</tr>
<tr>
<td width="331" height="50">
<form name="form3" method="post" action="">
原文件路径:<input type="text" name="text4" value="E:\ss.txt"><br>
目前文件路径:<input type="text" name="text5" value="E:\sss.txt">
<input type="button" name="Button" value="移除" onclick="filemove(document.form3.text4.value,document.form3.text5.value)">
</form>
</td>
</tr>
<tr>
<td width="331" height="50">
搜索
<input type="text" name="fixfolder" value="E:/"><!--指定文件夹:-->
<input type="button" value="搜索" onclick="searchFiles()">
<div id="textarea"></div>
</td>
</tr>
<tr>
<td width="331" height="50">
删除目录下所有文件
<div id="">
<input type="text" id="alldeldata" name="" value="d:\\Documents\\myword" />
<input type="submit" name="Submit" value="清空文件夹" onclick="test()"/>
</div>
</td>
</tr>
<tr>
<td width="331" height="50">
创建文件,存在则覆盖
<div id="">
文件路径:<input type="text" id="createurl" name="" value="c:\\test.txt" /><br>
文件类容:<textarea type="text" id="createdata" name="" value="" ></textarea>
<input type="submit" name="Submit" value="创建文件" onclick="create()"/>
</div>
</td>
</tr>
<tr>
<td width="331" height="50">
读取文件类容
<div id="">
文件路径:<input type="text" id="contentdata" name="" value="./111/a.txt" />
<input type="submit" name="Submit" value="读取文件" onclick="oncontent()"/>
<textarea id="contentlist" style="width: 331px;word-wrap:break-word;"></textarea>
</div>
</td>
</tr>
</table>
</body>
</html>
<script>
function filecopy(sname,dname){
var fso,f;
fso=new ActiveXObject("Scripting.FileSystemObject");
f=fso.GetFile(sname.value);
f.Copy(dname.value);
alert("文件复制成功");
}
function filedelete(fname){
var fso,f;
fso=new ActiveXObject("Scripting.FileSystemObject");
f=fso.GetFile(fname.value);
f.Delete();
alert("文件删除成功");
}
function filemove(fname,mname){
var fso,f;
fso=new ActiveXObject("Scripting.FileSystemObject");
f=fso.GetFile(fname.value);
f.Move(mname);
alert("文件移除成功");
}
//JS如何遍历一个文件夹下的所有文件与目录
function searchFiles() {
var fso = new ActiveXObject("Scripting.FileSystemObject");
var f = fso.GetFolder(document.all.fixfolder.value);
var fc = new Enumerator(f.files);
var s = "";
for (; !fc.atEnd(); fc.moveNext()) {
s += fc.item();
s += "<br/>";
}
fk = new Enumerator(f.SubFolders);
for (; !fk.atEnd(); fk.moveNext()) {
s += fk.item();
s += "<br/>";
}
textarea.innerHTML = s
}
//清空所有文件
function test(){
//创建FileSystemObject对象fso
var fso = new ActiveXObject("Scripting.FileSystemObject");
//path 删除文件的路径(文件夹)
// var path = "d:\\Documents\\myword";
var path = document.getElementById("alldeldata").value;
//如果指定的文件夹存在则返回 True ;否则返回 False
if(fso.FolderExists(path)) {
try{
//删除除文件夹以外的所有文件
fso.DeleteFile(path+"\\*");
//删除所有文件夹
fso.DeleteFolder(path+"\\*");
alert("删除成功!");
}catch(e){
alert("*.*" + e.message);
}
}else{
alert("不存在这个目录:"+path);
}
}
//创建文件
function create(){
var fso = new ActiveXObject("Scripting.FileSystemObject");
//创建新文件
let createurl = document.getElementById("createurl").value;
let createdata = document.getElementById("createdata").value;
var writeFile = fso.createTextFile(createurl,true); //true是否可以覆盖文件
writeFile.write(createdata);
writeFile.close();
if(writeFile){
alert("创建成功!");
}
}
//读取文件
function oncontent(){
var fso = new ActiveXObject("Scripting.FileSystemObject");
let contentdata = document.getElementById("contentdata").value;
var openfile = fso.OpenTextFile(contentdata,1);
var content = openfile.readLine();
// alert(content);
//需要显示html代码,可以引入代码高亮插件
document.getElementById("contentlist").value = content
openfile.close();
}
</script>
改进带样式版
<!DOCTYPE html>
<html>
<!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
</head>
<body>
<ul class="nav nav-pills list-group-item-info" >
<li id="active1" onclick="tabs('active1')" role="presentation"><a href="#">复制文件</a></li>
<li id="active2" onclick="tabs('active2')" role="presentation"><a href="#">删除文件</a></li>
<li id="active3" onclick="tabs('active3')" role="presentation"><a href="#">移动文件</a></li>
<li id="active4" onclick="tabs('active4')" role="presentation"><a href="#">搜索文件</a></li>
<li id="active5" onclick="tabs('active5')" role="presentation"><a href="#">清空文件夹</a></li>
<li id="active6" onclick="tabs('active6')" role="presentation"><a href="#">创建文件</a></li>
<li id="active7" onclick="tabs('active7')" role="presentation"><a href="#">读取文件类容</a></li>
</ul>
<ul class="list-group"><li role="separator" class="divider"></li></ul>
<ul class="list-group">
<li id="tab1" class="list-group-item list-group-item-success">
<form name="form1" method="post" action="">
原文件路径:<input type="text" class="form-control" name="text1" value="E:\ss.txt"><br>
目前文件路径:<input type="text" class="form-control" name="text2" value="E:\sss.txt">
<input type="button" class="btn btn-danger" name="Button" value="复制" onclick="filecopy(document.form1.text1,document.form1.text2)">
</form>
</li>
<li id="tab2" class="list-group-item list-group-item-info">
<form name="form2" method="post" action="">
文件路径:<input type="text" class="form-control" name="text3" value="ceshidel.txt">
<input type="button" class="btn btn-danger" name="Button" value="删除" onclick="filedelete(document.form2.text3)">
</form>
</li>
<li id="tab3" class="list-group-item list-group-item-warning">
<form name="form3" method="post" action="">
原文件路径:<input type="text" class="form-control" name="text4" value="E:\ss.txt"><br>
目前文件路径:<input type="text" class="form-control" name="text5" value="E:\sss.txt">
<input type="button" class="btn btn-danger" name="Button" value="移除" onclick="filemove(document.form3.text4.value,document.form3.text5.value)">
</form>
</li>
<li id="tab4" class="list-group-item list-group-item-danger">
搜索
<input type="text" class="form-control" name="fixfolder" value="E:/"><!--指定文件夹:-->
<input type="button" class="btn btn-danger" value="搜索" onclick="searchFiles()">
<div id="textarea"></div>
</li>
<li id="tab5" class="list-group-item list-group-item-success">
删除目录下所有文件
<div id="">
<input type="text" class="form-control" id="alldeldata" name="" value="d:\\Documents\\myword" />
<input type="submit" class="btn btn-danger" name="Submit" value="清空文件夹" onclick="test()"/>
</div>
</li>
<li id="tab6" class="list-group-item list-group-item-info">
创建文件,存在则覆盖
<div id="">
文件路径:<input type="text" class="form-control" id="createurl" name="" value="c:\\test.txt" /><br>
文件类容:<textarea type="text" style="width: 100%;" id="createdata" name="" value="" ></textarea>
<input type="submit" class="btn btn-danger" name="Submit" value="创建文件" onclick="create()"/>
</div>
</li>
<li id="tab7" class="list-group-item list-group-item-warning">
读取文件类容
<div id="">
文件路径:<input type="text" class="form-control" id="contentdata" name="" value="./111/a.txt" />
<input type="submit" class="btn btn-danger" name="Submit" value="读取文件" onclick="oncontent()"/>
<textarea id="contentlist" style="width: 100%;word-wrap:break-word;"></textarea>
</div>
</li>
</ul>
</body>
</html>
<script>
function filecopy(sname,dname){
var fso,f;
fso=new ActiveXObject("Scripting.FileSystemObject");
f=fso.GetFile(sname.value);
f.Copy(dname.value);
alert("文件复制成功");
}
function filedelete(fname){
var fso,f;
fso=new ActiveXObject("Scripting.FileSystemObject");
f=fso.GetFile(fname.value);
f.Delete();
alert("文件删除成功");
}
function filemove(fname,mname){
var fso,f;
fso=new ActiveXObject("Scripting.FileSystemObject");
f=fso.GetFile(fname.value);
f.Move(mname);
alert("文件移除成功");
}
//JS如何遍历一个文件夹下的所有文件与目录
function searchFiles() {
var fso = new ActiveXObject("Scripting.FileSystemObject");
var f = fso.GetFolder(document.all.fixfolder.value);
var fc = new Enumerator(f.files);
var s = "";
for (; !fc.atEnd(); fc.moveNext()) {
s += fc.item();
s += "<br/>";
}
fk = new Enumerator(f.SubFolders);
for (; !fk.atEnd(); fk.moveNext()) {
s += fk.item();
s += "<br/>";
}
textarea.innerHTML = s
}
//清空所有文件
function test(){
//创建FileSystemObject对象fso
var fso = new ActiveXObject("Scripting.FileSystemObject");
//path 删除文件的路径(文件夹)
// var path = "d:\\Documents\\myword";
var path = document.getElementById("alldeldata").value;
//如果指定的文件夹存在则返回 True ;否则返回 False
if(fso.FolderExists(path)) {
try{
//删除除文件夹以外的所有文件
fso.DeleteFile(path+"\\*");
//删除所有文件夹
fso.DeleteFolder(path+"\\*");
alert("删除成功!");
}catch(e){
alert("*.*" + e.message);
}
}else{
alert("不存在这个目录:"+path);
}
}
//创建文件
function create(){
var fso = new ActiveXObject("Scripting.FileSystemObject");
//创建新文件
let createurl = document.getElementById("createurl").value;
let createdata = document.getElementById("createdata").value;
var writeFile = fso.createTextFile(createurl,true); //true是否可以覆盖文件
writeFile.write(createdata);
writeFile.close();
if(writeFile){
alert("创建成功!");
}
}
//读取文件
function oncontent(){
var fso = new ActiveXObject("Scripting.FileSystemObject");
let contentdata = document.getElementById("contentdata").value;
var openfile = fso.OpenTextFile(contentdata,1);
var content = openfile.readLine();
// alert(content);
// console.log(content)
//需要显示html代码,可以引入代码高亮插件
document.getElementById("contentlist").value = content
openfile.close();
}
</script>
<script>
$(document).ready(function(){
$(".list-group-item").css("display","none")
$("#tab1").css("display","inherit")
$("#active1").addClass("active");
});
function tabs(id){
// console.log(id)
$("li").removeClass("active");
$("#"+id).addClass("active");
$(".list-group-item").css("display","none")
$("#tab"+id.split("")[6]).css("display","inherit")
// console.log(("tab1").split("")[3])
}
</script>