JavaScript Tutorial/MS JScript/Files

Материал из Web эксперт
Версия от 08:24, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Files

The Files object represents a collection of files in a folder.

Properties and Methods of the Files Object

Property/Method Description Count Returns the number of items in a collection Item Sets or returns an item based on a specific key



<html>
    <body>
    <script language="JScript">
    <!--
    function ShowList(foldername)
    {
      var myObject, f, MyFiles, names;
      myObject = new ActiveXObject("Scripting.FileSystemObject");
      f = myObject.GetFolder(foldername);
      myFiles = new Enumerator(f.files);
      names="";
      for (i=0; !myFiles.atEnd(); myFiles.moveNext()){
          names += myFiles.item();
          names += "<br>";
      }
      document.write(names);
    }
    -->
    </script>
    <form name="form1">
    Enter the name of an existing folder including the drive letter.
    <input type="text" size="35" name="file">
    <br><br>
    <input type="Button" value="Get File List" onClick="ShowList(document.form1.file.value)">
    </form>
    </body>
    </html>


Files.Count

The Count property returns the number of items in the Files collection.



<html>
    <body>
    <script language="JScript">
    <!--
    function getCount(foldername)
    {
      var myObject, f, filesCount;
      myObject = new ActiveXObject("Scripting.FileSystemObject");
      f = myObject.GetFolder(foldername);
      filesCount = f.files.Count;
      document.write("The number of files in this folder is: " + filesCount);
    }
    -->
    </script>
    <form name="myForm">
    Enter the name of a folder which contains files.
    <input type="text" size="35" name="file">
    <br><br>
    <input type="Button" value="Get File Count"
    onClick="getCount(document.form1.file.value)">
    </form>
    </body>
    </html>


Files.Item

The Item property sets or returns an item based on a specified key in a collection of files.



<html>
    <body>
    <script language="JScript">
    <!--
    function getItem(foldername)
    {
      var myObject, f, filesItem;
      myObject = new ActiveXObject("Scripting.FileSystemObject");
      f = myObject.GetFolder(foldername);
      filesItem = f.files.Item("Setup.exe");
      document.write("The file that you got is: " + filesItem);
    }-->
    </script>
    <form name="myForm">
    Enter the name of a folder which contains files.
    <input type="text" size="35" name="file">
    <br><br>
    <input type="Button" value="Get File Item"
    onClick="getItem(document.form1.file.value)">
    </form>
    </body>
    </html>