Handling drives and folders
use
FileSystemObject (FSO) Object mode, can have a planned drive and folder, just like interacting them in the Windows Explorer. You can copy and move the folder to get information about the drives and folders, and so on.
Get information about the drive
Can use
DRIVE objects to obtain information about the various drives, which are connected to the system via the network. Its properties can be used to get the following information:
The total capacity of the drive is how much is available in bytes (TOTALSIZE attribute) drives, which number is assigned to the drive (Driveletter property) drive (Driveletter property) drive (Driveletter Properties) drive (Driveletter property) drive Such as movable, fixed, network, CD-ROM, or RAM disk (DRIVETYPE attribute) driver's serial number (SERIALNUMBER attribute) drive Used file system type, such as FAT, FAT32, NTFS, etc. (FileSystem Properties) drive You can use the (IsReady property) shared and / or volume name (Sharaname and Volumename property) drive path or root folder (Path and rootfolder attributes)
Example code, how to understand
These properties are used in FileSystemObject.
Method for using Drive object
use
Drive objects collect information about the drive. In the code below, there is no actual
DRIVE object reference; instead, use
GetDrive method to get existing
A reference to the Drive object (DRV in this example).
The example below demonstrates how to use Drive objects in VBScript:
Sub ShowdriveInfo (Drvpath)
DIM FSO, DRV, S
SET FSO = CreateObject ("scripting.filesystemObject")
SET DRV = fso.getdrive (fso.getdrivename (drvpath))
S = "Drive" & ucase (drvpath) & "-"
S = S & DRV.VOLUMENAME & "
"
S = S & "Total Space:" & FormatNumber (drv.totalsize / 1024, 0)
S = S & "KB" & "
"
s = S & "Free Space:" & FormatNumber (drv.freespace / 1024, 0)
S = S & "KB" & "
"
Response.write S
End Sub
The following code shows the same functionality in JScript:
Function ShowdriveInfo1 (Drvpath)
{
VAR FSO, DRV, S = ""
Fso = New ActiveXObject ("scripting.filesystemObject");
DRV = fso.getdrive (fso.getdrivename (drvpath));
S = "drive" drvpath.touppercase () "-"
S = drv.volumename "
";
S = "Total Space:" DRV.TOTALSIZE / 1024;
S = "KB" "
";
S = "Free Space:" DRV.FREESPACE / 1024;
S = "KB" "
";
Response.write (s);
}
Process folder
In the table below, a normal folder task and a method of performing them are described.
Task method creates a folder. FileSystemObject.createFolder deletes folders. Folder.delete or FileSystemObject.deletefolder Move folder. Folder.move or FileSystemObject.movefolder replicates folder. Folder.copy or FileSystemObject.copyFolder retrieves the name of the folder. Folder.name If the folder exists on the drive, find it. FileSystemObject.FoldRexists gains an instance of the existing Folder object. FileSystemObject.getFolder Favorites the parent folder name of the folder. FILESYSTEMOBJECT.GETPARETFolderName finds the path to the system folder. FILESYSTEMOBJECT.GETSPECIALFOLDER
Please check the sample code to see how many of these methods and properties have been used in FileSystemObject.
The following example demonstrates how to use the Folder and FileSystemObject objects in VBScript to operate folders and get information about them:
SUB ShowFolderinfo ()
DIM FSO, FLDR, S
'Instances of FileSystemObject.
SET FSO = CreateObject ("scripting.filesystemObject")
'Get a Drive object.
Set fldr = fso.getfolder ("c:")
'Print Parents folder name.
Response.write "Parent Folder Name IS:" & FLDR & "
"
'Print the drive name.
Response.write "Contained on Drive" & fldr.drive & "
"
'Printing the regenform.
If Fldr.isrootfolder = TRUE THEN
Response.write "this is the root folder." & "
"
"
Else
Response.Write "This folder is not a root folder." & "
"
END IF
'Create a new folder with the FileSystemObject object.
Fso.createFolder ("c: / bogus")
Response.write "CREATED FOLDER C: / BOGUS" & "
"
'Print the basic name of the folder.
Response.write "Basename =" & fso.getBaseName ("C: / Bogus") & "
"
'Delete the newly created folder.
Fso.Deletefolder ("c: / bogus")
Response.write "Deleted Folder C: / Bogus" & "
"
End Sub
The following example shows how to use in JScript
Folder and
FILESYSTEMOBJECT object:
Function showfolderinfo ()
{
VAR FSO, FLDR, S = ""
// Get an instance of FileSystemObject.
Fso = New ActiveXObject ("scripting.filesystemObject");
// Get the Drive object.
FLDR = fso.getfolder ("C:");
// Print the parent folder name.
Response.write ("Parent Folder Name IS: FLDR "
");
// Print the drive name.
Response.write ("Contained on Drive" Fldr.drive "
);
// Print the root name.
Fldr.isrootfolder
Response.write ("this is the root folder.");
Else
Response.write ("This Folder isn't a root folder.");
Response.Write ( "
");
// Create a new folder with the FileSystemObject object.
Fso.createfolder ("c: // bogus");
Response.write ("Created Folder C: // Bogus" "
");
// Print the basic name of the folder.
Response.write ("BaseName =" fso.getBaseName ("c: // bogus") "
");
// Delete the newly created folder.
Fso.Deletefolder ("C: // Bogus");
Response.write ("Deleted Folder C: // Bogus" "
");
}
Handle file
There are two main file processing types:
Create, add, or delete data, and read files, copy, and delete files
Create a file
Creating an empty text file (sometimes called "text stream") has three ways.
The first method is to use the CreateTextFile method. The following example demonstrates how to create text files in this method in VBScript:
DIM FSO, F1
SET FSO = CreateObject ("scripting.filesystemObject") set f1 = fso.createtextfile ("c: /testfile.txt", true)
To use this method in JScript, use the following code:
VAR FSO, F1;
Fso = New ActiveXObject ("scripting.filesystemObject");
F1 = fso.createtextfile ("c: //testfile.txt", true);
Please investigate
Example code, how to understand
Use in FileSystemObject
CreateTextFile method.
The second method of creating a text file is to use the OpenTextFile method of the FILESystemObject object and set the Forwriting flag. In VBScript, the code is like the example below:
Dim Fso, TS
Const forwriting = 2
Set fso = creteObject ("scripting. FilesystemObject")
Set ts = fso.opentextfile ("c: /test.txt", forwriting, true)
To use this method in JScript to create a text file, use the following code:
VAR FSO, TS;
Var forwriting = 2;
Fso = New ActiveXObject ("scripting.filesystemObject");
Ts = fso.opentextfile ("c: //test.txt", forwriting, true);
The third method for creating a text file is to use
OpenastextStream method, and set
Forwriting logo. To use this method, use the following code in VBScript:
DIM FSO, F1, TS
Const 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)
In JScript, use the code in the example below:
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 a text file is created, add data to the file using the following three steps:
Open the text file. data input. Close the file. Use it to open an existing file, use
FileSystemObject object
OpenTextFile method or
File object
OpenastextStream method.
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
Please examine sample code to understand how Write, WriteLine, and Writeblanklines methods are used in the FileSystemObject object.
To turn off an open file, use the Close method of the TextStream object.
Please examine sample code to understand how to use a Close method in FileSystemObject.
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 VBScript example demonstrates how to open the file, and simultaneously use three write methods to add data to the file, then close the file:
SUB CREATEFILE ()
DIM FSO, TF
SET FSO = CreateObject ("scripting.filesystemObject")
Set tf = fso.createtextFile ("c: /testfile.txt", true)
'Write a row and have a new line character.
Tf.Writeline ("Testing 1, 2, 3.")
'Write three new line characters to the file.
Tf.writeblanklines (3)
'Write a row.
Tf.write ("this is a test.")
Tf.close
End Sub
This example demonstrates how to use these three methods in JScript:
Function cretefile ()
{
VAR FSO, TF;
Fso = New ActiveXObject ("scripting.filesystemObject");
Tf = fso.createtextfile ("c: //testfile.txt", true);
// Write a row and have a new line character.
Tf.writeline ("Testing 1, 2, 3.");
/ / Write three new line characters to the file.
Tf.writeblanklines (3);
// Write a line.
Tf.write ("this is a test.");
Tf.close ();
}
Read file
Use it from the text file to read data, use
TextStream object
Read,
READLINE or
Readall method. 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
Please examine sample code to understand how to use Readall and Readline methods in FileSystemObject.
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 VBScript example demonstrates how to open a file, and how to write data into the file and read data from the file: Sub ReadFiles
DIM FSO, F1, TS, S
Const forreading = 1
SET FSO = CreateObject ("scripting.filesystemObject")
Set f1 = fso.createtextFile ("c: /testfile.txt", true)
'Write a row.
Response.write "Writing file
"
F1.writeLine "Hello World"
f1.writeblanklines (1)
f1.close
'Read the contents of the file.
Response.write "Reading File
"
Set ts = fso.opentextfile ("c: /testfile.txt", forreading)
s = ts.readline
Response.write "File Contents = '" & S & "" "
Ts.close
End Sub
The following code demonstrates the same thing in JScript:
Function readfiles ()
{
VAR FSO, F1, TS, S;
Var forreading = 1;
Fso = New ActiveXObject ("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 the contents of the file.
Response.write ("Reading File
);
Ts = fso.opentextfile ("c: //testfile.txt", forreading);
S = ts.readline ();
Response.write ("File Contents = '" S "");
Ts.close ();
}
Move, copy and delete files
FSO object mode has 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
Please examine sample code to take two methods of removing files in FileSystemObject.
The following VBScript example creates a text file in the root of the drive C, writes some information, then moves it to the / TMP directory, and makes a backup in / temp, and finally put them from two directories Delete.
To run the following example, you need to create a / TMP and / TEMP directory in the root of the drive C:
Submanipfiles
DIM FSO, F1, F2, S
SET FSO = CreateObject ("scripting.filesystemObject") set f1 = fso.createtextfile ("c: /testfile.txt", true)
Response.write "Writing file
"
'Write a row.
F1.write ("this is a test.")
'Close the file.
f1.close
Response.write "Moving File to C: / TMP
"
'Get the handle of the file in the root directory (C: /).
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 the / temp directory.
f2.copy ("c: /temp/testfile.txt")
Response.write "Deleting Files
"
'Get the handle of the current location of the file.
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
The following code demonstrates the same thing in 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.");
// Turn the file.
f1.close ();
Response.write ("Moving File to C: // TMP
");
/ / Get the handle of the file in the root directory (C: /).
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 the / temp directory.
F2.copy ("c: //temp/testfile.txt");
Response.write ("deleding files
);
// Get the handle of the current location of the file.
F2 = fso.getfile ("c: //tmp/testfile.txt");
F3 = fso.getfile ("c: //temp/testfile.txt");
// Delete Files.
f2.delete ();
f3.delete ();
Response.write ("all done!");}