[Repost] Talk about FSO

xiaoxiao2021-03-06  41

How to delete a file [JScript] function DeleteFile (filespec) {var fso; fso = new ActiveXObject ( "Scripting.FileSystemObject"); fso.DeleteFile (filespec);} [VBScript] Sub DeleteAFile (filespec) Dim fso Set fso = CreateObject ("Scripting.filesystemObject") fso.deletefile (filespec) End Sub

FileExists method See driveexists method | FoldeRexists method | GetFile method | getFileName method Applications to: FileSystemObject object language JScript

VBScript

Display all languages ​​If the specified file is returned; otherwise returns false.

Object.fileexists (filespec) Parameters Object. Should be the name of FileSystemObject. FileSpec must option. Need to determine if the name of the file exists. If the file may not be in the current folder, a complete path description (can be absolute or relative). The following example illustrates the usage of the FileExists method.

[JScript] function ReportFileStatus (filespec) {var fso, s = filespec; fso = new ActiveXObject ( "Scripting.FileSystemObject"); if (fso.FileExists (filespec)) s = "exists."; Else s = " does not exist. "; return (s);} [VBScript] Function ReportFileStatus (filespec) Dim fso, msg Set fso = CreateObject (" Scripting.FileSystemObject ") If (fso.FileExists (filespec)) Then msg = filespec & "ELSE MSG = filespec &" doesn't exist. "end if reportfilestatus = msgend function) Please refer to DRIVEEXISTS Method | FOLDEREXISTS Method | GetFile Method | GetFileName Application to: FileSystemObject Object

Script Runtime Library

Processing file language JScript

VBScript

There are two main file processing types in all languages:

Create, add, or delete data, and read file movements, copy, and delete files Create a file to create an empty text file (sometimes called "text stream").

The first method is to use the CreateTextFile method. The following example demonstrates how to create a text file with the CreateTextFile method:

[VBScript] Dim fso, f1Set fso = CreateObject ( "Scripting.FileSystemObject") Set f1 = fso.CreateTextFile ( "c: /testfile.txt", True) [JScript] var fso, f1; fso = new ActiveXObject ( "Scripting .FilesystemObject "); f1 = fso.createtextFile (" c: //testfile.txt ", true); the second method of creating text files is to use the OpenTextFile method of the FILESystemObject object and set the Forwriting flag. [VBScript] Dim fso, tsConst ForWriting = 2Set fso = CreateObject ( ". Scripting FileSystemObject") Set ts = fso.OpenTextFile ( "c: /test.txt", ForWriting, True) [JScript] var fso, ts; var ForWriting = 2; fso = new activXObject ("scripting.filesystemObject"); ts = fso.opentextFile ("c: //test.txt", forwriting, true); the third method of creating text files is to use the OpenAsTextStream method, And set the Forwriting flag. To use this method, use the following code:

[VBScript] DIM FSO, F1, TSCONST ForWriting = 2SET FSO = CreateObject ("scripting.filesystemObject") fso.createtextfile ("c: /test1.txt") set f1 = fso.getfile ("c: /test1.txt" ) Set ts = f1.openastextStream (ForWriting, True) [JScript] 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); Add data to the file once created a text file, use the following three steps Document Add Data:

Open the text file.

data input.

Close the file.

To open an existing file, use the OpenTextFile method of the FileSystemObject object or the OpenASTextStream method for the File object.

To write data to open text files, the Write, WriteLine or Writeblanklines method of the TextStream object is used under the following table.

The task method writes data to the open text file without subsequent a new line character. Write write data to the open text file, followssee a new line character. WriteLine writes one or more blank lines to the open text file. Writeblanklines

To turn off an open file, use the Close method of the TextStream object. Note that the new row character contains one or more characters (depending on the operating system) to move the cursor to the start position of the next row (Enter / Return). Note that some string may already have this non-printing character. The following example demonstrates how to open a file, and simultaneously use three write methods to add data to the file, then close the file:

[Vbscript] SUB CREATEFILE () DIM FSO, TF SET FSO = CREATEOBJECT ("scripting.filesystemObject") set tf = fso.createtextfile ("c: /testfile.txt", true) 'Write a row, and has a new line character. Tf.Writeline ("Testing 1, 2, 3.") 'writes three new line characters to the file. Tf.WriteBlankLines (3) 'Write a line. Tf.Write ("this is a test.") tf.closend sub [jscript] function createfile () {var fso, tf; fso = new activXObject ("scripting.filesystemObject"); tf = fso.createtextfile ("C: //testfile.txt ", true); // Write a row and bring a new line character. Tf.writeline ("Testing 1, 2, 3."); // Write three new row characters to the file. Tf.WriteBlankLines (3); // Write a line. Tf.write ("this is a test."); Tf.close ();} Reading files To read data from text files, use the Read, Readline, or Readall method of the TextStream object. The following table describes which method should be used for different tasks.

Task methods read from the file to the specified number of characters. Reading a single line (until but does not include new line characters). Readline reads the entire content of the text file. Readall

If you use the READ or READLINE method, and you want to skip the special parts of the data, use the SKIP or SKIPLINE method. The result text of the read method has a string that can be displayed in one control or to analyze, connect, and so on.

The following example demonstrates how to open the file, and how to write data into the file and read data from the file:

[VBScript] Sub ReadFiles Dim Fso, F1, TS, S const forreading = 1 set fso = creteObject ("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 'reads the file content. Response.write "Reading file
" SET TS = fso.opentextfile ("c: /testfile.txt", forreading s = ts.readline response.write "file contents = '" & S & "" TS. Closend sub [jscript] function readfiles () {var forreading = 1; fso = new activXObject ("scripting.filesystemObject"); 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
"); TS = fso.opentextfile ("c: //testfile.txt", forreading; s = ts.readline (); response.write ("file contents = '" S "'"; ts.close ();} Mobile, copy, and delete files FSO object models have two ways to move, copy, and delete files, as described in the table below. Task Method Mobile File File.Move or FileSystemObject.movefile Copy File File.copy or FileSystemObject.copyFile Delete File File.delete or FileSystemObject.deletefile

The following example creates a text file in the root directory of the drive C, writes some information, then moves it into the / tmp directory, and makes a backup in / temp, and finally remove them from two directories.

To run the following example, you need to create a / TMP and / TEMP directory in the root of the drive C:

[VBScript] SUB MANIPFILES DIM FSO, F1, F2, 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.") 'Closes the written file. F1.close Response.write "Moving File to C: / TMP
" get the handle of the file in the C: / root directory. Set f2 = fso.getfile ("c: /testfile.txt") 'moves the file to the / TMP directory. F2.move ("c: /tmp/testfile.txt") response.write "Copying File to C: / Temp
" copies files to / TEMP. F2.copy ("c: /temp/testfile.txt") response.write "deleding files
" get the handle of the file current location. Set f2 = fso.getfile ("c: /tmp/testfile.txt") set f3 = fso.getfile ("c: /temp/testfile.txt") 'Delete files. f2.Delete f3.Delete Response.Write "All done!" End Sub [JScript] function ManipFiles () {var fso, f1, f2, s; fso = new ActiveXObject ( "Scripting.FileSystemObject"); f1 = fso.CreateTextFile ("c: //testfile.txt", true); response.write ("Writing file
"); // Write a line. F1.Write ("this is a test."); // Close the write to the file. F1.close (); Response.write ("Moving File to C: // TMP
"); // Get the handle of the file in the C: / root directory. 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
"); // obtain the handle of the file current location.

F2 = fso.getfile ("c: //tmp/testfile.txt"); f3 = fso.getfile ("c: //temp/testfile.txt"); // Delete files. F2.delete (); f3.delete (); Response.write ("all done!");} 'Create a FileSystemObject case

Set myfileObject = Server.createObject ("scripting.filesystemobject")

'Creating a file IF myfileObject.fileexists ("c: /test.txt")

Response.write ("exists this file")

Else

Response.write ("There is no this file")

END IF

Set myfile = myfileObject.createtextfile ("c: /test.txt")

Myfile.writeline ("Hello")

Myfile.close

'Copy file operation

MyFileObject.copyFile "c: /test.txt" "c: /test2.txt"

'Mobile file operation

MyFileObject.movefile "c: /test.txt" "c: / passt3.txt"

'Delete these files

MyFileObject.deletefile "c: /test.txt"

MyFileObject.deletefile "c: /test3.txt"

%>

How to create a directory FUNCTION CREATEDIR (ByVal LocalPath) program that creates a directory, if there is a multi-level directory, the first level of the first level creates an ON Error Resume Next localPath = Replace (LocalPath, "/", "/") SET FileObject = CreateObject ( "Scripting.FileSystemObject") patharr = Split (LocalPath, "/") path_level = UBound (patharr) For I = 0 To path_level If I = 0 Then pathtmp = patharr (0) & "/" Else pathtmp = pathtmp & patharr (I) & "/" cpath = Left (pathtmp, Len (pathtmp) - 1) If Not FileObject.FolderExists (cpath) Then FileObject.CreateFolder cpath Next Set FileObject = Nothing If Err.Number <> 0 Then createDIR = False err.clear else createdir = True End IFEND FUNCTION

<% '------- Open text documentation, and write an example of content ------------------------- SET FSO = createobject ( "scripting.filesystemobject") const iomode = 8 const create = false set open_text = fso.OpenTextFile (server.mappath ( "new_text.txt"), iomode, create) open_text.writeline ( "Tel: Tel01") set open_text = Nothing set fso = nothing%>

Iomode settings

1 read 2 rewriting 8 Add

CREATE Setting True If the file to be opened does not exist, create a new file False If the file to be opened does not exist, then do not create a file, and an error message appears: the file is not found

Error Tip Analysis: Enter the principle of the file: Each READLINE is called, the Readline pointer will decline, and the error will appear when the decline exceeds the end of the text. Specific reasons: call Readline method multiple times,

Error file mode principle: Whether the file open does not only determine the permission settings of the folder, the settings of the Iomode in the OpenTextFile are also related to the operation of the IOMODE, the error specific cause: Iomode = 1 has executed WRITE or WRITELINE method or IOMOE = 2 but implemented Readline method

Document does not find the principle: The file to open does not have a specific reason: create set to: false and the file to open does not exist ->

Three ways to disable FSO components are well known that the powerful functionality and destructive of FileSystemObject components are the reasons why it has been disabled by the free homepage providers (those supporting ASP). The first: uses REGSRV32 / U C: /WINDOWS/SYSTEM/SCRRUN.DLL (WIN98 path) to log out of this component. Windows98 system Enter the following command in the DOS command line: Close command: regsvr32 / u c: /windows/system/scrrun.dll Open Command: regsvr32 c: /windows/system/scriptu.dllwin2000 system: Enter the following in the CMD command line status Command: Close command: regsvr32 / u c: /winnt/system32/scrrun.dll Open Command: Regsvr32 C: /Winnt/System32/scrrun.dll

The second: Modify the value of Progid, the way the component is called in the ASP is usually the SET object name = Server.createObject ("progid"), and we can achieve the disabled by modifying the progid value in the registry. method. In the start-runn into regedit, then find hkey_classes_root / scripting.filesystemObject, then we can change the value of the RogID, such as change to scripting.filesystemObject8. This is called in the ASP page: <% @ language = VBScript%> <% set fs = server.createObject ("scripting.filesystemObject8)%> (If you have no call to the component in front, there is no need to restart, You can see the effect, otherwise, please restart.) When we look at the results of the original call method: <% @ language = VBScript%> <% set fs = server.createObject ("scripting.filesystemObject) ")%> The result of this time is: server object error '' '' ASP 0177: 800401F3 '' '' Server.createObject failed

/ASPIMAGE/TestFile2.asp, line 3

800401F3 OK to achieve our requirements.

The third: can disable the component by modifying the progID value, which can also be implemented by modifying the CLSID. We know, in addition to the createObject method, you can also create a component using a general , we can use HTML in ASP to add a component in the web page. The method is: runat said is executed on the server, Scope represents the life cycle of the component, you can use session, Application or Page. Indicates that the current page, can also be default) This kind of writing is useless, there is also a way to write is: We It is also possible to disable the component by modifying the value of the CLSID, such as the value of HKEY_CLASS_ROOT / SCRIPTING.FILESTEMOBJECT / CLSID is changed to 0D43FE01-F093-11CF-8940-00A0C9054229 (changed) At the end of the last side), this time is: at this time. Create a new user: IUSR_DOMAINIIS Set anonymous user IUSR_DOMAINCACLS for the corresponding site: set directory permissions

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

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.046, SQL: 9