全国统一服务热线:400-633-9193

js获取文件里面的所有文件名(实例)

    网络     2017-11-13    1250

如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
 function init(){
  var fso = new ActiveXObject("Scripting.FileSystemObject");
  // 获取文件相关信息
  var f1 = fso.GetFile("F:\\test\\test.txt");
  alert('文件上次修改日期:' + f1.DateLastModified);
  var drv;
  var s = '';
  // 获取磁盘相关信息
  drv = fso.GetDrive(fso.GetDriveName("C:\\"));
  s += 'Drive C:' + '-' + drv.VolumeName + '\n';
  s += 'Total Space:' + drv.TotalSize / 1024 + 'Kb' + '\n';
  s += 'Free Space:' + drv.FreeSpace / 1024 + 'Kb' + '\n';
  alert('C盘信息' + s);
  // 操作文件夹
  fldr = fso.GetFolder("F:\\test");
  alert('父文件夹名称:' + fldr + '\n');
  // 显示所在drive名称
  alert("Contained on drive " + fldr.Drive + "\n");
  // 判断是否为根目录
  if (fldr.IsRootFolder){
   alert("This is the root folder.");
  }
  else {
   alert("This folder isn't a root folder.");
  }
  // 创建新文件夹
  fso.CreateFolder ("F:\\test\\Bogus");
  alert("Created folder F:\\testBogus" + "\n");
  // 显示文件夹基础名称,不包含路径名
  alert("Basename = " + fso.GetBaseName("F:\\test\\bogus") + "\n");
  // 删除创建的文件夹
  fso.DeleteFolder ("F:\\test\\Bogus");
  alert("Deleted folder F:\\test\\Bogus" + "\n");
 }
  
 function init2(){
  var fso = new ActiveXObject("Scripting.FileSystemObject");
  // 获取目录下所有文件,对于该浏览器缓存目录,仅能获取到一个文件
  var path = 'C:\\Users\\zhang\\AppData\\Local\\Microsoft\\Windows\\Temporary Internet Files';
  //path = 'F:\\test';
  var fldr = fso.GetFolder(path);
  var ff = new Enumerator(fldr.Files);
  var s = '';
  var fileArray = new Array();
  var fileName = '';
  var count = 0;
  for(; !ff.atEnd(); ff.moveNext()){
   fileName = ff.item().Name + '';
   fileName = fileName.toLowerCase();
   if(fileName.indexOf('cookie') >= 0){
    fileName = fileName.substring(0,fileName.indexOf('.'));
    fileName = fileName.substring(fileName.lastIndexOf('@')+1);
    s += fileName + '\n';
   }
   count++;
  }
  alert(count + ',' + s);
 }
</script>
</head>
<body onload="init2();">
</body>
</html>


  分享到:  
0.2404s