FileSystemObject object

xiaoxiao2021-03-06  104

The FileSystemObject (FSO) object model contains the following items and collection objects.

Item / Collection Item Description FileSystemObject main object. The methods and properties included let you build, delete and process discs, data clips, and files, and achieve related information. Most methods related to this object and method in other FSO objects, which provide these methods for convenience. Drive objects. The methods and properties contained in it allows you to collect disaggregat information, such as the shared name of the disk machine, and available space capacity. Note that "disk machine" does not necessarily specifically, it can be a CD-ROM disc, a RAM disk machine, and the like. A disk machine does not have to be actually connected to the system, or it can be logically connected through the network. DRIVES collection object. Provide a list of disabled discs, including entities or logical disks. The DRIVES collection item contains all the discs, regardless of their type. The extracted disk machine does not need to be inserted into the media, and can appear in this collection object. File object. The methods and properties included will allow you to build, delete or move files. Also let you query the archive name, path, and other properties to your system. Files collection object. Provides a list of all files contained in a folder. Folder object. The methods and properties contained in it allow you to build, delete or move the folder. Also let you query the data clip name, path, and other properties to your system. Folders set objects. Provide a list of all data clamps in Folder. TextStream object. Allows you to read and write text files.

Establish FileSystemObject object

First, use the CreateObject method to establish a FileSystemObject object. In VBScript, use the following code code to create FileSystemObject's execution individual:

DIM FSO

SET FSO = CreateObject ("scripting.filesystemObject")

This paradigm code shows how to establish a FileSystemObject executable individual.

Use the appropriate method

Second, use the FileSystemObject object appropriate method. For example, if you want to create a new item, use the CreateTextFile or CreateFolder (FSO object model, which is not supported or deleted).

To delete items, use the DELETEFILE and DELETEFOLDER methods for the FileSystemObject object, or the DELETE method for the File and Folder objects. You can also use the appropriate method to copy and move the files and data clips.

Note that some of the FileSystemObject object model is repeated. For example, you can copy the file using a CopyFile method of the FileSystemObject object or a COPY method of the File object. These two methods can achieve the same effect; at the same time, the purpose is to provide elasticity of programming.

Access existing disk machines, archives and data clips

To access an existing disk machine, file or folder, use the appropriate "GET" method in the FileSystemObject object:

GetDrive getFolder getFile

To save an existing archive in VBScript:

DIM FSO, F1

SET FSO = CreateObject ("scripting.filesystemObject")

Set f1 = fso.getfile ("c: /test.txt")

Do not use the "GET" method for the original object, because the "CREATE" function has passed the object code back to the object. For example, if you create a new folder using the CreateFolder method, do not use the getFolder method to access its properties, such as Name, Path, and Size, etc. Please set a variable for the CreateFolder function, get a control code for the newly established folder, and then access its properties, methods, and events. The following procedures can be used in VBScript: SUB CREATEFOLDER

DIM FSO, FLDR

SET FSO = CreateObject ("scripting.filesystemObject")

Set fldr = fso.createfolder ("c: / mytest")

Response.write "Created Folder:" & FLDR.NAME

End Sub

Access objects of properties

When you have the control code of the object, you can access its properties. For example, if you want to obtain a name of a particular data clip, create an auto execution individual of an article, then utilize an appropriate method (in this case, the GetFolder method, because the data clip already exists) to specify a control code.

In VBScript, use the following code code to specify a control code:

Set fldr = fso.getfolder ("c: /")

You now have a control code for the Folder object, so you can view its name properties. In VBScript, use the following code code to view the properties:

Response.write "Folder Name is:" & fldr.name

To see the last modified time of the file, use the following VBScript syntax:

DIM FSO, F1

SET FSO = CreateObject ("scripting.filesystemObject")

'Get a File object to be queried.

Fso.getfile ("c: /detlog.txt")

'Print message.

Response.write "File Last Modified:" & f1.dateLastModified

Treatment of material holders

The following table lists the common data clip tasks and methods to perform these tasks.

Task method establishes a folder. FileSystemObject.createFolder deletes the folder. Folder.delete or FileSystemObject.deletefolder Mobile Data Clip Folder.move or FileSystemObject.moveFolder replication folder. Folder.copy or FileSystemObject.copyfolder captures the name of the folder. Folder.Name Checks Whether there is a folder on the disk machine. FileSystemObject.foldeRexists acquires the execution of the existing Folder object. FileSystemObject.getFolder finds the upper layer of folder name of the folder. FileSystemObject.getParentFoldername finds the path to the system folder. FILESYSTEMOBJECT.GETSPECIALFOLDER

Sub showfolderinfo () DIM FSO, FLDR, S 'Remove the individual execution of FileSystemObject. Set fso = createObject ("scripting.filesystemObject") 'Remove the Drive object. Set fldr = fso.getfolder ("C:") 'Listed upper folder name. Response.write "Parent Folder Name IS:" & FLDR & "
" column printed disk name. Response.write "Contained On Drive" & Fldr.drive & "
" The name of the file is the name. If FLDR.ISROOTFOLDER = TRUE THEN RESPONSE.WRITE "this is the root folder." & ""
"
" Else Response.write "this folder isn't a root folder." & "
< BR> "Endiff" establishes a new folder with the FileSystemObject object. Fso.createFolder ("C: / Bogus") response.write "create" create "" "&"
"list of this data clip. Response.write "BaseName =" & fso.getBaseName ("c: / bogus") & "
" deletes the newly established folder. Fso.Deletefolder ("C: / Bogus") response.write "deleted folder c: / bogus" & "
" The processing of the process file of the END SUB file is divided into two categories: establish, join or remove information, and Three ways to read file movements, copy, and delete files, have three ways to create blank text files (sometimes called "text data stream"). The first method is to use the CreateTextFile method. The following example shows how to build a text file in VBScript: DIM FSO, F1

SET FSO = CreateObject ("scripting.filesystemObject")

Set f1 = fso.createtextFile ("C: /TestFile.txt", true) The second method of establishing a text file is the OpenTextFile method for setting the FileSystemObject item that sets the Forwriting flag. The program in VBScript is as shown in the following example: DIM FSO, TS

Const forwriting = 2

Set fso = creteObject ("scripting. FilesystemObject")

Set ts = fso.opentextFile ("c: /test.txt", forwriting, true) The third method of establishing a text file is to use the OpenASTextStream method that sets the Forwriting flag. When using this method, use the following code in VBScript: DIM FSO, F1, TSCONST forWriting = 2

SET FSO = CreateObject ("scripting.filesystemObject")

Fso.createtextfile ("c: /test1.txt")

Set f1 = fso.getfile ("c: /test1.txt")

SET TS = f1.openastextStream (forWriting, True) After the data is added to the file, you can add data to the file according to the following three steps: open the text file. Write information. Close the file. To turn on an existing file, use the OpenTextFile method of the FileSystemObject or the OpenAsTextStream method for the File object. To write data to the open text file, use the different tasks listed in the following table Use the Write, WriteLine or WriteBlanklines method of the TextStream object.

Task methods write data to the open text file, but do not join the wrap character. Write writes the information to the open text file and add a wrap character. WriteLine writes one or more rows of blank lines in the open text file. WriteBLANKLines To turn off the open file, use the Close method of the TextStream object. Remarks The wrap font contains one or more characters (depending on the job system), allows the cursor to move to the beginning of the next line (reset character / jump line). Please note that the end of some strings may already contain such non-royalprints. The following VBScript example shows how to open the file, use three write methods to add data to the file, and close the file: Sub CreateFile ()

DIM FSO, TF

SET FSO = CreateObject ("scripting.filesystemObject")

Set tf = fso.createtextFile ("c: /testfile.txt", true)

'Write a Line with a newline character.

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

'Write Three Newline Characters to the file.

Tf.writeblanklines (3)

'Write a line.

Tf.write ("this is a test.")

Tf.close

End Sub Read Archives To read data from a text file, use the Read, Readline or Readall method of the TextStream object. The following table shows how different tasks should be used.

Task Method Reads a specific number of characters from the file. READ reads a whole (excluding changing font). Readline reads the full content of the text file. Readall If you use the READ or READLINE method, use the SKIP or Skipline method using the information on a specific location. The result text taken by various reading methods will be a string, which can be displayed in the control item, analyzed by a string function (eg, LEFT, RIGHT, and MID) or add. The following VBScript example shows how to open files, write files, and read profiles: Sub ReadFiles

DIM FSO, F1, TS, SCONST Forreading = 1

SET FSO = CreateObject ("scripting.filesystemObject")

Set f1 = fso.createtextFile ("c: /testfile.txt", true)

'Write a line.

Response.write "Writing file
"

F1.writeLine "Hello World"

f1.writeblanklines (1)

f1.close

'Read file content.

Response.write "Reading File
"

Set ts = fso.opentextfile ("c: /testfile.txt", forreading)

s = ts.readline

Response.write "File Contents = '" & S & "" "

Ts.close

End Sub Mobile, Copy, and Remove Archives The following table describes the two ways of moving, copying, and deleting archives provided by the FSO object model.

Task way to move files File.Move or FileSystemObject.MoveFile File.Copy copy files or delete files FileSystemObject.CopyFile File.Delete or FileSystemObject.DeleteFile following VBScript example to create a text file in the root directory of the C disk drive, write in the archives Enter some information, move the file to the / tmp directory, copy the file to the / temp directory, and then delete the file from these two directories. To perform the following example, create / TMP and / TEMP directory in the root directory of the disk machine C: Sub Manipfiles

DIM FSO, F1, F2, S

SET FSO = CreateObject ("scripting.filesystemObject")

Set f1 = fso.createtextFile ("c: /testfile.txt", true)

Response.write "Writing file
"

'Write a line.

F1.write ("this is a test.")

'Close the write file.

f1.close

Response.write "Moving File to C: / TMP
"

'Under the c: / root directory, get a control code.

Set f2 = fso.getfile ("c: /testfile.txt")

'Move the file to the / TMP directory.

F2.move ("c: /tmp/testfile.txt")

Response.write "Copying File to C: / Temp
"

'Copy the file to / TEMP.

f2.copy ("c: /temp/testfile.txt")

Response.write "Deleting Files
"

'Take the control code to the current location of the archive.

Set f2 = fso.getfile ("c: /tmp/testfile.txt")

Set f3 = fso.getfile ("c: /temp/testfile.txt")

'Delete the file.

F2.delete

f3.deleteresponse.write "all done!"

End Sub

转载请注明原文地址:https://www.9cbs.com/read-96286.html

New Post(0)