JavaScript Tutorial/MS JScript/File
Содержание
File
The File object provides access to all the properties of a file.
Property/Method Description Attributes Refers to the attributes of the file Copy() Copies a file from one location to another DateCreated Returns the date a file was created DateLastAccessed Returns the date a file was last accessed DateLastModified Returns the date a file was last modified Delete() Removes a file Drive Returns the drive on which a file exists Move() Moves a file from one location to another Name Returns the name for a file OpenAsTextStream() Opens a text stream for a file ParentFolder Returns the parent folder name for a file Path Returns the path to a file ShortName Returns the shortname of the file ShortPath Returns the shortpath of the file Size Returns the size of a file Type Returns the file type
<html>
<body>
<script language="JavaScript">
<!--
var myObject, f;
myObject = new ActiveXObject("Scripting.FileSystemObject");
f = myObject.GetFile("c:\\test.txt");
document.write("The name of the file is: " + f.Name);
-->
</script>
</body>
</html>
File.Attributes
Syntax
file.Attributes[newattributes]
The Attributes property is used to determine and set the attributes of a file.
This property is an array that takes the optional parameter newattributes to set any new attributes.
Attributes can be read/write or read-only.
Attribute properties Property Description 0 Specifies a Normal file. No attributes set. 1 Specifies that a file is a Read-only file. Attribute is read/write. 2 Specifies that a file is Hidden. Attribute is read/write. 4 Refers to a system file. Attribute is read/write. 8 Refers to the disk drive volume label. Attribute is read-only. Refers to a folder in a directory. Attribute is read-only. 32 Specifies that a file has changed since the last backup. Attribute is read/write. 64 Refers to a link or shortcut. Attribute is read-only. 128 Refers to the disk drive volume label. Attribute is read-only.
File.Copy()
Syntax
file.Copy(destination, overwrite)
File.DateCreated
The DateCreated property is used to get the date in which the file was created.
<html>
<BODY>
<script language="JScript">
<!--
function get()
{
var myObject, f, date;
myObject = new ActiveXObject("Scripting.FileSystemObject");
f = myObject.GetFile("c:\\mytest.txt");
date = f.DateCreated;
alert ("The date this file was created is: " + date);
}
//-->
</script>
Get the date that mytest.txt was created.
<form name="myForm">
<INPUT type="Button" value="Get Date" onClick="get()">
</form>
</BODY>
</html>
File.DateLastAccessed
The DateLastAccessed property is used to find out the last date that the file was accessed.
<html>
<BODY>
<script language="JScript">
<!--
function get()
{
var myObject, f, date;
myObject = new ActiveXObject("Scripting.FileSystemObject");
f = myObject.GetFile("c:\\test.txt");
date = f.DateLastAccessed;
alert ("The date this file was last accessed is: " + date);
}
//-->
</script>
Get the date that mytest.txt was last accessed.
<form name="myForm">
<INPUT type="Button" value="Get Date" onClick="get()">
</form>
</BODY>
</html>
File.DateLastModified
The DateLastModified property returns the date when the file was last modified.
<html>
<BODY>
<script language="JScript">
<!--
function get()
{
var myObject, f, date;
myObject = new ActiveXObject("Scripting.FileSystemObject");
f = myObject.GetFile("c:\\test.txt");
date = f.DateLastModified;
alert ("The date this file was last modified is: " + date);
}
//-->
</script>
Get the date that mytest.txt was last modified.
<form name="myForm">
<INPUT type="Button" value="Get Date" onClick="get()">
</form>
</BODY>
</html>
File.Delete()
The Delete() method is used to delete a specified file.
<html>
<body>
<script language="JScript">
<!--
function remove()
{
var myObject;
myObject = new ActiveXObject("Scripting.FileSystemObject");
var f = myObject.GetFile("c:\\test.txt");
f.Delete();
}
//-->
</script>
Delete file "mytest.txt".
<form name="myForm">
<input type="Button" vvalue="Delete File" onClick="remove()">
</form>
</body>
</html>
File.Move()
Syntax
file.Move(destination)
File.Name
The Name property is used to either set or get the name of a file object.
<html>
<BODY>
<script language="JScript">
<!--
function get()
{
var myObject, f;
myObject = new ActiveXObject("Scripting.FileSystemObject");
f = myObject.GetFile("c:\\test.txt");
alert("The name of the file is: " + f.Name);
}
//-->
</script>
<form name="myForm">
<INPUT type="Button" value="Get Name" onClick="get()">
</form>
</BODY>
</html>
File.ParentFolder
The ParentFolder property determines the parent folder from which the file was obtained.
<html>
<body>
<script language="JScript">
<!--
function get()
{
var myObject, f;
myObject = new ActiveXObject("Scripting.FileSystemObject");
f = myObject.GetFile("c:\\yourfolder\\myTest.txt");
alert("The name of the parent folder is: " + f.ParentFolder.Path);
}
//-->
</script>
Click to get the name of the parent folder
<form name="myForm">
<input type="Button" value="Get Parent Folder" onClick="get()">
</form>
</body>
</html>
File.Path
The Path property gets the path for a specified file.
<html>
<body>
<script language="JScript">
<!--
function getPath()
{
var myObject, f;
myObject = new ActiveXObject("Scripting.FileSystemObject");
f = myObject.GetFile("c:\\Test.txt");
alert("The name of the path is: " + f.Path);
}
//-->
</script>
<form name="myForm">
<input type="Button" value="Get Path" onClick="getPath()">
</form>
</body>
</html>
File.ShortName
The ShortName property returns the short name used by programs that require the earlier 8.3 naming convention.
<html>
<body>
<script language="JScript">
<!--
function get()
{
var myObject, f;
myObject = new ActiveXObject("Scripting.FileSystemObject");
f = myObject.GetFile("c:\\yourfolder\\test.txt");
alert("The file ShortName is: " + f.ShortName);
}
//-->
</script>
<form name="myForm">
<input type="Button" value="Get Shortname" onClick="get()">
</form>
</body>
</html>
File.ShortPath
The ShortPath property returns the short path used by programs that require the earlier 8.3 naming convention.
<html>
<BODY>
<script language="JScript">
<!--
function get()
{
var myObject, f;
myObject = new ActiveXObject("Scripting.FileSystemObject");
f = myObject.GetFile("c:\\yourfolder\\Test.txt");
alert("The file ShortPath name is: " + f.ShortPath);
}
//-->
</script>
<form name="myForm">
<INPUT type="Button" value="Get Shortpath" onClick="get()">
</form>
</BODY>
</html>
File.Size
The Size property returns the size of the specified file.
<html>
<BODY>
<script language="JScript">
<!--
function get()
{
var myObject, f;
myObject = new ActiveXObject("Scripting.FileSystemObject");
f = myObject.GetFile("c:\\Test.txt");
alert("The file size is: " + f.Size);
}
//-->
</script>
<form name="myForm">
type=value=<input type="Button" value="Get File Size" onClick="get()">
</form>
</body>
</html>
File.Type
The Type property is used to get information pertaining to the type of file.
<html>
<body>
<script language="JScript">
<!--
function get()
{
var myObject, f;
myObject = new ActiveXObject("Scripting.FileSystemObject");
f = myObject.GetFile("c:\\test.txt");
alert("The name type is: " + f.Type);
}
//-->
</script>
<form name="myForm">
<input type="Button" value="Get File Type" onClick="get()">
</form>
</body>
</html>