Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement File manipulation by Javascript

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "Javascript how to achieve file operation", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "Javascript how to achieve file operation" this article.

First, the core of function realization: FileSystemobject object

To realize the file manipulation function in Javascript, it mainly depends on the FileSystemobject object.

2. FileSystemObject programming

Programming with FileSystemObject objects is simple, generally through the following steps: creating FileSystemObject objects, applying related methods, and accessing object-related properties.

(1) create a FileSystemObject object

The code to create a FileSystemObject object is only 1 line:

Var fso = new ActiveXObject ("Scripting.FileSystemObject")

After the above code is executed, fso becomes an instance of a FileSystemObject object.

(2) Application of relevant methods

After you create an object instance, you can use the relevant 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) access relevant attributes of the object

To access the relevant properties of an object, we must first establish a handle to the object, which is achieved through a series of get methods: GetDrive is responsible for obtaining drive information, GetFolder is responsible for obtaining folder information, and GetFile is responsible for obtaining file information. For example, after pointing to the following code, F1 becomes a handle 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 relevant 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 value of the last modified date property of c:myjstest.txt is displayed.

However, please note that for objects created using the create method, it is no longer necessary to use the get method to obtain the object handle, and then 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 the driver (Drives)

Using FileSystemObject objects to program to manipulate drives (Drives) and folders (Folders) is easy, just like interacting with files in a windows file browser, such as copying, moving folders, and getting folder properties.

(1) attributes of Drives object

The Drive object is responsible for collecting physical or logical drive resource content in the system and has the following properties:

L TotalSize: the drive size in bytes (byte).

L AvailableSpace or FreeSpace: the free drive space in bytes (byte).

L DriveLetter: drive letters.

L DriveType: drive type. Value: removable (mobile media), fixed (fixed media). NETwork (network resources), CD-ROM or RAM disk.

L SerialNumber: serial code of the drive.

L FileSystem: the file system type of the drive in which it resides. The values are fat, FAT32, and NTFS.

L IsReady: whether the drive is available.

L ShareName: share name.

L VolumeName: volume label name.

L Path and rootFolder: the path or root name of the drive.

(2) Drive object operation routine

The following routine displays information such as the volume label, total capacity, and free 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 + = "FreeSpace:" + drv.FreeSpace / 1024

S + = "Kb" + "n"

Alert (s)

4. Operation folder (Folders)

Actions that involve folders include creating, moving, deleting, and getting related properties.

Folder object operation routines:

The following routines practice getting the name of the parent folder, creating a folder, deleting a folder, determining whether it is a root directory, and so on:

Var fso, fldr, s = ""

/ / create a FileSystemObject object instance

Fso = new ActiveXObject ("Scripting.FileSystemObject")

/ / get the Drive object

Fldr = fso.GetFolder ("c:")

/ / Show parent directory name

Alert ("Parent folder name is:" + fldr + "n")

/ / display the name of the drive where it is located

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")

/ / displays the base name of the folder, 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")

Operation file (Files)

Operations on files are more complex than the drive (Drive) and folder (Folder) operations described above, and are basically divided into the following two categories: creation, copy, move, delete of files, and creation, addition, deletion, and reading of file contents. The following are described in detail.

(1) create a document

There are three ways 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 is to use the OpenTextFile method and add the ForWriting attribute, which has a value of 2 for ForWriting. 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 is to use the OpenAsTextStream method, which also sets the ForWriting property. 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 a file is created, it is generally necessary to follow the steps of "Open File-> fill in data-> close File" to add data to the file.

You can open the file using the OpenTextFile method of the FileSystemObject object or the OpenAsTextStream method of the File object.

Use the Write, WriteLine, or WriteBlankLines methods of the TextStream object to fill in the data. Under the same function of writing data, the difference between the three 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 blank lines.

The Close method of the TextStream object is used to close the file.

(3) create files and add data routines

The following code combines the steps of creating a file, adding data, and closing a file:

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 newline characters

Tf.WriteLine ("Testing 1,2,3.")

/ / add 3 blank lines

Tf.WriteBlankLines (3)

/ / fill in a line without a newline character

Tf.Write ("This is a test.")

/ / close the file

Tf.Close ()

(4) read the contents of the file

To read data from a text file, use the Read, ReadLine, or ReadAll methods of the TextStream object. The Read method reads a specified number of characters in the file; the ReadLine method reads an entire line, but does not include newline characters; and the ReadAll method reads the entire contents of the text file. The read content is stored in string variables for display and analysis. When using the Read or ReadLine method to read the contents of a file, if you want to skip some parts, use the Skip or SkipLine method.

The following code demonstrates opening the file, filling in the data, and then reading the data:

Var fso, f1, ts, s

Var ForReading = 1

Fso = new ActiveXObject ("Scripting.FileSystemObject")

/ / create a file

F1 = fso.CreateTextFile ("c:testfile.txt", true)

/ / fill in a row of data

F1.WriteLine ("Hello World")

F1.WriteBlankLines (1)

/ / close the file

F1.Close ()

/ / Open the file

Ts = fso.OpenTextFile ("c:testfile.txt", ForReading)

/ / read one line of the file to a string

S = ts.ReadLine ()

/ / display string information

Alert ("File contents ='" + s + ")

/ / close the file

Ts.Close ()

(v) moving, copying and deleting documents

Javascript has two corresponding methods for each of the above three file operations: File.Move or FileSystemObject.MoveFile for moving files, File.Copy or FileSystemObject.CopyFile for copying files, and File.Delete or FileSystemObject.DeleteFile for deleting files.

The following code demonstrates creating a text file under the root of drive C, filling in some contents, then moving the file to the tmp directory, creating a copy of the file under the directory temp, and finally deleting the files in both directories:

Var fso, f1, f2, s

Fso = new ActiveXObject ("Scripting.FileSystemObject")

F1 = fso.CreateTextFile ("c:testfile.txt", true)

/ / write a line

F1.Write ("This is a test.")

/ / close the file

F1.Close ()

/ / get the file handle under the C: root directory

F2 = fso.GetFile ("c:testfile.txt")

/ / move files to the tmp directory

F2.Move ("c:tmptestfile.txt")

/ / copy the file to the temp directory

F2.Copy ("c:temptestfile.txt")

/ / get file handle

F2 = fso.GetFile ("c:tmptestfile.txt")

F3 = fso.GetFile ("c:temptestfile.txt")

/ / Delete files

F2.Delete ()

F3.Delete ()

The above is all the contents of the article "how to manipulate files in Javascript". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report