Home > Web Front-end > JS Tutorial > Javascript File Operation_Basic Knowledge

Javascript File Operation_Basic Knowledge

WBOY
Release: 2016-05-16 19:18:06
Original
1028 people have browsed it

1. Core function implementation: FileSystemObject object
To implement file operation functions in JavaScript, we mainly rely on the FileSystemobject object.
2. FileSystemObject Programming
Programming using the FileSystemObject object is very simple. Generally, you need to go through the following steps: Create a FileSystemObject object, apply related methods, and access object-related properties.
(1) Create a FileSystemObject object
The code to create a FileSystemObject object only needs one line:
var fso = new ActiveXObject("Scripting.FileSystemObject");
After the above code is executed, fso becomes a FileSystemObject Object instance.
(2) Apply related methods
After creating an object instance, you can use the related methods of the object. For example, use the CreateTextFile method to create a text file:
var fso = new ActiveXObject("Scripting.FileSystemObject");
var f1 = fso.createtextfile("c:\myjstest.txt",true");
(3) Accessing object-related attributes
To access object-related attributes, you must first establish a handle to the object, which is achieved through the get series of methods: GetDrive is responsible for obtaining drive information, GetFolder is responsible for obtaining folder information, GetFile Responsible for obtaining file information. For example, after pointing to the following code, f1 becomes the handle pointing to the file c:test.txt:
var fso = new ActiveXObject("Scripting.FileSystemObject");
var f1 = fso. GetFile("c:\myjstest.txt");
Then, use f1 to access the related properties of the object. For example:
var fso = new ActiveXObject("Scripting.FileSystemObject");
var f1 = fso. .GetFile("c:\myjstest.txt");
alert("File last modified: " f1.DateLastModified);
After executing the last sentence above, the last modified date attribute of c:myjstest.txt will be displayed Value.
But please note one thing: for objects created using the create method, you no longer need to use the get method to obtain the object handle. In this case, you can directly use the handle name created by the create method:
var fso = new ActiveXObject("Scripting.FileSystemObject");
var f1 = fso.createtextfile("c:\myjstest.txt",true");
alert("File last modified: " f1.DateLastModified);
3. Operating drives (Drives)
It is easy to use FileSystemObject objects to programmatically operate drives (Drives) and folders (Folders), just like interacting with files in the Windows file browser, such as: copy, Move a folder and get the properties of the folder.
(1) Drives object attributes
The Drive object is responsible for collecting the physical or logical drive resource content in the system. It has the following attributes:
l TotalSize: The drive size calculated in bytes.
l AvailableSpace or FreeSpace: The available space of the drive calculated in bytes.
l DriveLetter: drive letter.
l DriveType: Drive type, the value is: removable (removable media), fixed (fixed media), network (network resource), CD-ROM or RAM disk.
l SerialNumber: The serial number of the drive.
l FileSystem: The file system type of the drive, the values ​​are FAT, FAT32 and NTFS.
l IsReady: Whether the drive is available.
l ShareName: Share name.
l VolumeName: Volume name.
l Path and RootFolder: The path or root directory name of the drive.
(2) Drive object operation routine
The following routine displays the volume label, total capacity and available space of drive C:
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";
alert(s);
4. Operation folders (Folders)
Operations involving folders include creation, movement, deletion and acquisition Related properties.
Folder object operation routine:
The following routine will practice operations such as obtaining the name of the parent folder, creating a folder, deleting a folder, and determining whether it is the root directory:
var fso, fldr, s = "";
// Create FileSystemObject object instance
fso = new ActiveXObject("Scripting.FileSystemObject");
// Get Drive object
fldr = fso.GetFolder("c:\") ;
// Display the name of the parent directory
alert("Parent folder name is: " fldr "n");
// Display the name of the drive
alert("Contained on drive " fldr.Drive "n");
// Determine whether it is the root directory
if (fldr.IsRootFolder)
alert("This is the root folder.");
else
alert("This folder isn't a root folder.");
alert("nn");
// Create a new folder
fso.CreateFolder ("C:\Bogus");
alert( "Created folder C:\Bogus" "n");
// Display the folder base name, excluding the path name
alert("Basename = " fso.GetBaseName("c:\bogus") "n ");
//Delete the created folder
fso.DeleteFolder ("C:\Bogus");
alert("Deleted folder C:\Bogus" "n");
5 , operating files (Files)
The operations on files are more complicated than the drive (Drive) and folder (Folder) operations introduced above. They are basically divided into the following two categories: creating, copying, and moving files. , deletion operations and creation, addition, deletion and reading operations on file content. Each is introduced in detail below.
(1) Create a file
There are 3 methods to create an empty text file, which is sometimes called a text stream.
The first is to use the CreateTextFile method. The code is as follows:
var fso, f1;
fso = new ActiveXObject("Scripting.FileSystemObject");
f1 = fso.CreateTextFile("c:\testfile.txt", true);
The second method is to use the OpenTextFile method and add the ForWriting attribute. The value of ForWriting is 2. The code is as follows:
var fso, ts;
var ForWriting= 2;
fso = new ActiveXObject("Scripting.FileSystemObject");
ts = fso.OpenTextFile("c:\test.txt ", ForWriting, true);
The third method is to use the OpenAsTextStream method, and also set the ForWriting attribute. The code is as follows:
var fso, f1, ts;
var ForWriting = 2;
fso = new ActiveXObject("Scripting.FileSystemObject");
fso.CreateTextFile ("c:\test1.txt ");
f1 = fso.GetFile("c:\test1.txt");
ts = f1.OpenAsTextStream(ForWriting, true);
(2) Add data to the file
When After the file is created, you generally need to follow the steps of "open file -> fill in data -> close file" to add data to the file.
To open a file, you can use the OpenTextFile method of the FileSystemObject object, or the OpenAsTextStream method of the File object.
To fill in data, use the Write, WriteLine or WriteBlankLines method of the TextStream object. Under the same function of writing data, the difference between these three methods is that the Write method does not add a new line break at the end of the written data, the WriteLine method adds a new line break at the end, and WriteBlankLines adds one or more blanks. OK.
To close the file, you can use the Close method of the TextStream object.
(3) Routines for creating files and adding data
The following code combines the steps of creating files, adding data, and closing files:
var fso, tf;
fso = new ActiveXObject("Scripting.FileSystemObject");
// Create a new file
tf = fso.CreateTextFile("c:\testfile.txt", true);
// Fill in the data and add a newline character
tf.WriteLine("Testing 1, 2, 3.") ;
// Add 3 blank lines
tf.WriteBlankLines(3) ;
// Fill in one line without newlines
tf.Write ("This is a test.");
//Close the file
tf.Close();
(4) Read the file content
Read from the text file To get data, use the Read, ReadLine or ReadAll method of the TextStream object. The Read method is used to read a specified number of characters in the file; the ReadLine method reads an entire line, excluding newlines; and the ReadAll method reads the entire content of the text file. The read content is stored in a string variable for display and analysis.



Method or attribute description
BuildPath()
Generate a file path
CopyFile() Copy file
CopyFolder() Copy directory
CreateFolder( ) Create a new directory
CreateTextFile() Generate a file
DeleteFile() Delete a file
DeleteFolder() Delete a directory
DriveExists() Check whether the drive letter exists
Drives Return the drive letter Collection
FileExists() Check whether a file exists
FolderExists Check whether a directory exists
GetAbsolutePathName() Get the absolute path of a file
GetBaseName() Get the file name
GetDrive() Get the drive letter Name
GetDriveName() Get the drive letter name
GetExtensionName() Get the file suffix
GetFile() Generate the file object
GetFileName() Get the file name
GetFolder() Get the directory object
GetParentFolderName Gets the parent directory name of a file or directory
GetSpecialFolder() Gets a special directory name
GetTempName() Generates a temporary file object
MoveFile() Moves a file
MoveFolder() Moves a directory
OpenTextFile() Open a file stream


f.Files //A collection of all files in the directory
f.attributes //File attributes
Case 0 Str="Ordinary file. No setting Any attribute. "
Case 1 Str=" Read-only file. Readable and writable. "
Case 2 Str=" System file. Write. "
Case 16 Str=" Folder or directory. "
Case 32 Str=" Readable and writable. Link or shortcut. "
Case 2048 Str=" Compressed file."
f.Datecreated // 创建时间
f.DateLastAccessed //上次访问时间
f.DateLastModified // 上次修改时间
f.Path //文件路径
f.Name //文件名称
f.Type //文件类型
f.Size // 文件大小(单位:字节)
f.ParentFolder //父目录
f.RootFolder // 根目录 

 实例说明

BuildPath(路径,文件名) //这个方法会对给定的路径加上文件,并自动加上分界符 
<script> <BR><!-- <BR>var fso = new ActiveXObject("Scripting.FileSystemObject"); <BR>var newpath = fso.BuildPath("c:\\tmp", "51js.txt"); //生成 c:\tmp\51js.txt的路径 <BR>alert(newpath); <BR>--> <BR></script> 

CopyFile(源文件, 目标文件, 覆盖) //复制源文件到目标文件,当覆盖值为true时,如果目标文件存在会把文件覆盖 
<script> <BR><!-- <BR>var fso = new ActiveXObject("Scripting.FileSystemObject"); <BR>var newpath = fso.CopyFile("c:\\autoexec.bat", "d:\\autoexec.bak"); <BR>--> <BR></script> 

CopyFolder(对象目录,目标目录 ,覆盖) //复制对象目录到目标目录,当覆盖为true时,如果目标目录存在会把文件覆盖 
<script> <BR><!-- <BR>var fso = new ActiveXObject("Scripting.FileSystemObject"); <BR>fso.CopyFolder("c:\\WINDOWS\\Desktop", "d:\\"); //把C盘的Desktop目录复制到D盘的根目录 <BR>--> <BR></script> 

CreateFolder(目录名) //创建一个新的目录 
<script> <BR><!-- <BR>var fso = new ActiveXObject("Scripting.FileSystemObject"); <BR>var newFolderName = fso.CreateFolder("c:\\51JS"); //在C盘上创建一个51JS的目录 <BR>--> <BR></script> 

CreateTextFile(文件名, 覆盖) //创建一个新的文件,如果此文件已经存在,你需要把覆盖值定为true 
<script> <BR><!-- <BR>var fso = new ActiveXObject("Scripting.FileSystemObject"); <BR>var newFileObject = fso.CreateTextFile("c:\\autoexec51JS.bat", true); //脚本将在C盘创建一个叫 autoexec51JS.bat的文件 <BR>--> <BR></script> 

DeleteFile(文件名, 只读?) //删除一个文件,如果文件的属性是只读的话,你需要把只读值设为true 
<script> <BR><!-- <BR>var fso = new ActiveXObject("Scripting.FileSystemObject"); //为了安全我先把要删除的autoexec.bat备份到你的D盘 <BR>var newpath = fso.CopyFile("c:\\autoexec.bat", "d:\\autoexec.bat"); //把C盘的autoexec.bat文件删除掉 <BR>fso.DeleteFile("c:\\autoexec.bat", true); <BR>--> <BR></script> 

DeleteFolder(文件名, 只读?)//删除一个目录,如果目录的属性是只读的话,你需要把只读值设为true 
<script> <BR><!-- <BR>var fso = new ActiveXObject("Scripting.FileSystemObject"); <BR>fso.CopyFolder("c:\\WINDOWS\\Desktop", "d:\\"); //为了安全我先把你C盘的Desktop目录复制到你D盘的根目录 <BR>fso.DeleteFolder("c:\\WINDOWS\\Desktop", true); //把你的Desktop目录删除,但因为desktop是系统的东西,所以不能全部删除,但......... <BR>--> <BR></script> 

DriveExists(盘符) //检查一个盘是否存在,如果存在就返会真,不存在就返回....... 
<script> <BR><!-- <BR>var fso = new ActiveXObject("Scripting.FileSystemObject"); <BR>hasDriveD = fso.DriveExists("d"); //检查系统是否有D盘存在 <BR>hasDriveZ = fso.DriveExists("z"); //检查系统是否有Z盘存在 <BR>if (hasDriveD) alert("你的系统内有一个D盘"); <BR>if (!hasDriveZ) alert("你的系统内没有Z盘"); <BR>--> <BR></script> 

FileExists(文件名) //检查一个文件是否存在,如果存在就返会真,不存在就返回....... 
<script> <BR><!-- <BR>var fso = new ActiveXObject("Scripting.FileSystemObject"); <BR>fileName = fso.FileExists("c:\\autoexec.bat"); <BR>if (fileName) alert("你在C盘中有autoexec.bat文件,按下确定后这个文件将被删除!"); //开个玩笑:) <BR>--> <BR></script> 

FolderExists(目录名) //检查一个目录是否存在,如果存在就返会真,不存在就返回....... 
<script> <BR><!-- <BR>var fso = new ActiveXObject("Scripting.FileSystemObject"); <BR>folderName = fso.FolderExists("c:\\WINDOWS\\Fonts"); <BR>if (folderName) alert("按下确定后系统的字库将被删除!"); //开个玩笑:) <BR>--> <BR></script> 

GetAbsolutePathName(文件对象) //返回文件对象在系统的绝对路径 
<script> <BR><!-- <BR>var fso = new ActiveXObject("Scripting.FileSystemObject"); <BR>pathName = fso.GetAbsolutePathName("c:\\autoexec.bat"); <BR>alert(pathName); <BR>--> <BR></script> 

GetBaseName(文件对象) //返回文件对象的文件名 
<script> <BR><!-- <BR>var fso = new ActiveXObject("Scripting.FileSystemObject"); <BR>baseName = fso.GetBaseName("c:\\autoexec.bat"); //取得autoexec.bat的文件名autoexec <BR>alert(baseName); <BR>--> <BR></script> 

GetExtensionName(文件对象) //文件的后缀 
<script> <BR><!-- <BR>var fso = new ActiveXObject("Scripting.FileSystemObject"); <BR>exName = fso.GetExtensionName("c:\\autoexec.bat"); //取得autoexec.bat后缀bat <BR>alert(exName); <BR>--> <BR></script> 

GetParentFolderName(文件对象) //取得父级的目录名 
<script> <BR><!-- <BR>var fso = new ActiveXObject("Scripting.FileSystemObject"); <BR>parentName = fso.GetParentFolderName("c:\\autoexec.bat"); //取得autoexec.bat的父级目录C盘 <BR>alert(parentName); <BR>--> <BR></script> 

GetSpecialFolder(目录代码) //取得系统中一些特别的目录的路径,目录代码有3个分别是 0:安装Window的目录 1:系统文件目录 2:临时文件目录 
<script> <BR><!-- <BR>var fso = new ActiveXObject("Scripting.FileSystemObject"); <BR>tmpFolder = fso.GetSpecialFolder(2); //取得系统临时文件目录的路径 如我的是 C:\windows\temp <BR>alert(tmpFolder); <BR>--> <BR></script> 

GetTempName() //生成一个随机的临时文件对象,会以rad带头后面跟着些随机数,就好象一些软件在安装时会生成*.tmp 
<script> <BR><!-- <BR>var fso = new ActiveXObject("Scripting.FileSystemObject"); <BR>tmpName = fso.GetTempName(); //我在测试时就生成了radDB70E.tmp <BR>alert(tmpName); <BR>--> <BR></script> 

MoveFile(源文件, 目标文件) //把源文件移到目标文件的位置 
<script> <BR><!-- <BR>var fso = new ActiveXObject("Scripting.FileSystemObject"); <BR>var newpath = fso.MoveFile("c:\\autoexec.bat", "d:\\autoexec.bat"); //把C盘的autoexec.bat文件移移动到D盘 <BR>--> <BR></script> 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template