javascript 系统文件夹文件操作及参数介绍

时间:2021-05-26

前期在.net 下进行系统操作,同样的功能实现起来非常复杂,没想到用javascript竟然如此简单,所以在网上搜索了代码并加以改进。
复制代码 代码如下:
function PathList(path) {
var fso = new ActiveXObject("Scripting.FileSystemObject");
var fldr = fso.GetFolder(path);
var fd = new Enumerator(fldr.SubFolders);
for (; !fd.atEnd(); fd.moveNext()) {
sd = fd.item();
WScript.Echo(sd.path);
//写入文件
writeFile("a.text",sd.path);
//document.write(sd.path);
PathList(sd.path);
}
}
function FileList(path) {
var fso = new ActiveXObject("Scripting.FileSystemObject");
var fldr = fso.GetFolder(path);
var fd = new Enumerator(fldr.SubFolders);
for (; !fd.atEnd(); fd.moveNext()) {
sd = fd.item();
var fc = new Enumerator(sd.files);
for (; !fc.atEnd(); fc.moveNext())
{
WScript.Echo(fc.item());
writeFile("B.text",fc.item());
}
WScript.Echo(sd.path);
//写入文件
writeFile("a.text",sd.path);
//document.write(sd.path);
FileList(sd.path);
}
}
//当前目录文件
function CurFileList(path) {
var fso = new ActiveXObject("Scripting.FileSystemObject");
var fldr = fso.GetFolder(path);
var fc = new Enumerator(fldr.files);
for (; !fc.atEnd(); fc.moveNext())
{
WScript.Echo(fc.item());
writeFile("c.text",fc.item());
}
}

//读文件
function readFile(filename){
var fso = new ActiveXObject("Scripting.FileSystemObject");
var f = fso.OpenTextFile(filename,1);
var s = "";
while (!f.AtEndOfStream)
s += f.ReadLine()+"\n";
f.Close();
return s;
}
//写文件
function writeFile(filename,filecontent){
var fso, f, s ;
fso = new ActiveXObject("Scripting.FileSystemObject");
f = fso.OpenTextFile(filename,8,true);
f.WriteLine(filecontent);
f.Close();
//alert('ok');
WScript.Echo("写入成功");
}
//删除文件
function deleteFile(filename,filecontent){
var fso, f, s ;
fso = new ActiveXObject("Scripting.FileSystemObject");
f = fso.GetFile(filename);
f.Delete();
//alert('ok');
WScript.Echo("删除成功");
}
//批量删除,未删除文件夹,删除不了当前目录文件
function DelFileList(path) {
var fso = new ActiveXObject("Scripting.FileSystemObject");
var fldr = fso.GetFolder(path);
var fd = new Enumerator(fldr.SubFolders);
for (; !fd.atEnd(); fd.moveNext()) {
sd = fd.item();
var fc = new Enumerator(sd.files);
for (; !fc.atEnd(); fc.moveNext())
{
WScript.Echo(fc.item());
writeFile("B.text",fc.item());
fc.item().Delete();
WScript.Echo("删除成功");
}
WScript.Echo(sd.path);
//写入文件
writeFile("a.text",sd.path);
writeFile("a.text","删除完毕");
//document.write(sd.path);
DelFileList(sd.path);
}
}

//删除当前目录文件
function CurDelFileList(path) {
var fso = new ActiveXObject("Scripting.FileSystemObject");
var fldr = fso.GetFolder(path);
var fc = new Enumerator(fldr.files);
for (; !fc.atEnd(); fc.moveNext())
{
WScript.Echo(fc.item());
writeFile("c.text",fc.item());
fc.item().Delete();
writeFile("c.text","删除成功");
}
}

function getDriveinfo()
{
var fso, drv, s ="";
fso = new ActiveXObject("Scripting.FileSystemObject");
drv = fso.GetDrive(fso.GetDriveName("c:\\"));
s += "Drive C:" + " - ";
s += drv.VolumeName + "\n";
s += "Total Space: " + drv.TotalSize / 1024;
s += " Kb" + "\n";
s += "Free Space: " + drv.FreeSpace / 1024;
s += " Kb" + "\n";
WScript.Echo(s);
}
CurFileList("D:\web_01");
FileList("D:\web_01");
getDriveinfo();

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章