VBScript (1) Working with files

Source: Internet
Author: User
Working with files

ScriptingFileSystemObject(FSO) object mode to process drives, folders, and files

 

To useFileSystemObject(FSO) for programming in object mode, then:

  • Use the Createobject method to create a FileSystemObject object.
  • Use the appropriate method on the newly created object.
  • Access Object Attributes.

The FSO object mode is included in the scripting library, which is located in the scrrun. dll file.

Listfiles. vbs

Option explicit

On Error resume next

Dim folderpath 'path to the folder to be searched for files

Dim objfso 'The FileSystemObject

Dim objfolder 'the folder object

Dim colfiles 'collection of files from files Method

Dim objfile 'individual file object

 

Folderpath = "C: \ FSO"

Set objfso = Createobject ("scripting. FileSystemObject ")

Set objfolder = objfso. getfolder (folderpath)

Set colfiles = objfolder. Files

 

For each objfile in colfiles

Wscript. Echo objfile. Name, objfile. Size & "bytes"

Wscript. Echo vbtab & "created:" & objfile. datecreated

Wscript. Echo vbtab & "modified:" & objfile. datelastmodified

Next

 

To enumerate a list of files

1.

Use Createobject to create the file system object.

2.

Define the folder to be searched by using getfolder.

3.

Use the files command to list files.

4.

Use a for each statement to walk through the folder.

 

The program can be divided into the following four parts:

Header information

It mainly includes three statements: Option explicit, on error resume next, and dim

 

In short, after option explicit is used, if the variable is used again, it must be declared with dim first. This is called force variable declaration.
On Error resume next, when a script error occurs, ignore this error and continue execution. When debugging the program, you need to disable this statement. Dim is used to declare a variable.

 

Reference Information

Folderpath = "C: \ FSO"

The folderpath variable is mainly used to make the script more convenient for future modification.

 

Set objfso = Createobject ("scripting. FileSystemObject ")

Set is a command in VBScript that is used to assign an object reference to a variable.

 

Set is used to assign an object reference to a variable.

To useFileSystemObject(FSO) to program in object mode, first use the Createobject method to create a FileSystemObject object before using some FSO methods and attributes.

Worker and output information

This script first creates a file sytem object and then assigns it to the variable objfso,

For each... Next
Usage:

 

Just the steps

To use for each... Next

1.

On a new line in a script, type for each and then a variable name. (For each a in acollection)

2.

On the next line, enter a command you want repeated.

3.

On the line following the command you want repeated, type next.

 

Used to traverse every object in the Set

If you hear collection, the first thing that comes to mind is for each... Next.

If you do not know how many cycles are required, you can use for each... Next

 

Creating files

Just the steps

To create a file

1.

Use Createobject to create an instance of FileSystemObject.

2.

Use the createtextfile method.

3.

Include the full path and the name of the desired file

Set objfso = Createobject ("scripting. FileSystemObject ")

Set objfile = objfso. createtextfile ("C: \ fso.txt ")

 

Writing to a text file

Just the steps

To write to a text file

1.

Create an instance of FileSystemObject.

2.

Use the appropriate parameter to indicate that you are going to either overwrite the file (2) or append data to the file (8 ).

3.

Use either the write, writeline, or writeblanklines method to write to the file.

4.

Close the text file.

Basiclog. vbs

LogFile = "C:\fso\fso.txt"
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(LogFile, ForWriting,True)
objFile.WriteLine "beginning process " & Now
objFile.WriteLine "working on process " & Now
objFile.WriteLine "Process completed at " & Now
objFile.Close

 

The logfile variable is used for storage. The location of the log file can be changed later.

Forwriting indicates overwriting. Each write operation overwrites the previous file.

 

Opentextfile Method

Open the specified file and returnTextstreamObject, which can be read, written, or appended to a file.

object.OpenTextFile(filename[, iomode[, create[, format]]])

 

Parameters

Object

Required. The name of the FileSystemObject object.

Filename

Required. String expression, indicating the name of the file to be opened.

Iomode

Optional. The input/output mode is one of the following three constants: forreading, forwriting, or forappending.

Create

Optional. Boolean value, indicating that when the specifiedFilenameWhether to create a new file if it does not exist. When a new file is allowedTrueOtherwiseFalse. The default value is
False.

Format

Optional. Indicates the format in which the file is opened. If this parameter is ignored, the file is opened in ASCII format.

Set

IomodeThe parameter can be one of the following settings:

Constant

Value

Description

Forreading

1

Open the file in read-only mode. You cannot write this file.

Forwriting

2

Open the file in write-only mode. You cannot read this file.

Forappending

8

Open the file and write it at the end of the file.

 

<P> 〉FormatThe parameter can be one of the following settings:

Constant

Value

Description

Tristateusedefault

-2

Open a file in the default format.

Tristatetrue

-1

To Unicode
Open files.

Tristatefalse

0

Use ASCII
Open files.

 

Verifying a file exists

It may be easier to use in actual work. First, determine whether the file exists. If yes, add content to the file. if this file does not exist, we recommend that you use this file first. in fact, opentextfile is the third option in this method. If it is set to true, the file will be created first when a file is opened and the file does not exist.

Verifyfileexists. vbs

LogFile = "C:\FSO\fso.txt"
Const ForAppending = 8
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
   If objFSO.FileExists(LogFile) Then
         Set objFile = objFSO.OpenTextFile(LogFile, ForAppending)
         objFile.Write "appending " & Now
   Else
         Set objFile = objFSO.CreateTextFile(LogFile)
             objFile.write "writing to new file " & now
   End If

TIPS: If you use createtextfile to create a new file, you do not need to use opentextfile to open the file. Because VB is intelligent, the file will be opened when you create a file to facilitate writing. therefore, remember to turn off the file.

FAQ:

Q.

What is required to talk to the file system by using FileSystemObject?

A.

You can use FileSystemObject by first using the Createobject command, and then assigning to a variable the object that comes back.

Q.

Why do you want an object for FileSystemObject?

A.

You want a object for FileSystemObject because it enables you want to work with files and folders.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.