Vbscript script Programming Tutorial 2 Use fso for file operations

Source: Internet
Author: User

By sssa2000
Let's take a look at how to use fso for file operations. Fso is the core of file operations in vbs. As a hacker, No matter what language you are learning, you should be familiar with file operations, so please study carefully.

Let's not talk nonsense. Let's first look at the objects fso consists:

Drive object: contains information about storage devices, including hard drives, optical drives, ramdisks, and network drives.

Drives collection: provides a list of physical and logical drives

File object: Check and process files

Files set: provides a list of files in a folder.

Folder object: Check and process folders

Folders set: provides a list of folder subfolders.

Textstream object: read/write text files

Look at the fso method: because there are many methods, I will not write down each function. If you do not understand it, check msdn on your own. Don't say no.

Buw.path: adds the file path information to the existing file path.

Copyfile

Copyfolder

Createfolder

Createtextfile

Deletefile

Deletefolder

Dreveexits

Fileexits

Folderexists

Getabsolutepathname: returns the absolute path of a folder or file.

Getbasename: returns the basic path of a file or folder.

Getdrive: returns a dreve object.

Getdrivename: returns the name of a drive.

Getextensionname: returns the extension.

Getfile: returns a file object.

Getfilename: returns the name of the file in the folder.

Getfolder

Getparentfoldername: returns the parent folder of a folder.

Getspecialfolder: returns the object pointer to a special folder.

Gettempname: returns the name of a randomly generated file or folder that can be used by createtextfile.

Movefile

Movefolder

Opentextfile

Well, I think you have understood more than half of it here. Maybe you don't need to talk about it later. The script is so simple. Heheh, let's continue.

1. Use fso

Because fso is not part of wsh, we need to build its model.

For example, set fs = wscript. createobject ("scripting. filesystemobject ")

In this way, the fso model is created. It would be easy to release, set fs = nothing

2. Use folders

Create:

Before creation, check whether the program exists.

* *************************** Createfolder. vbs *****************************

Dim fs, s

Set fs = wscript. createobject ("scripting. filesystemobject ")

If (fs. folderexists ("c: \ temp") then

S = "is available"

Else

S = "not exist"

Set foldr = fs. createfolder ("c: \ temp ")

End if

Delete, copy, and move

Delete:

Set fs = wscript. createobject ("scripting. filesystemobject ")

Fs. deletefolder ("c: \ windows ")

Copy:

Set fs = wscript. createobject ("scripting. filesystemobject ")

Fs. copyfolder "c: \ data" "d: \ data"

NOTE: If both c: \ data and d: \ data exist at this time, an error will occur and the replication will stop. If you want to forcibly overwrite data, use fs. copyfolder "c: \ data" "d: \ data", true

Mobile

Set fs = wscript. createobject ("scripting. filesystemobject ")

Fs. movefolder "c: \ data" "d: \ data"

Wildcard characters:

You can use the wildcard for convenient operations:

For example, fs. movefolder: c: \ data \ te * "," d: \ working"

I didn't use "\" at the end of the target path. That is to say, I didn't write it like this:

Fs. movefolder: c: \ data \ te * "," d: \ working \"

If the d: \ working directory does not exist, windows will not automatically create this directory for us.

Another point is that none of the above mentioned involves the folder object. We are all using the method provided by fso. Of course, the folder can be used in the same way:

Set fs = wscript. createobject ("scripting. filesystemobject ")

Set f = fs. getfolder ("c: \ data ")

F. delete 'delete. Sub-directories are also deleted.

F. copy "d: \ working", true' copy to d: \ working

F. move: "d: \ temp" 'move to d: \ temp

Special folder

It generally refers to the System Folder \ windows \ system32, Temporary Folder, windows Folder

Let's take a look at the following: we use environment variables to get the windows directory. We will detail the environment variables later. If I forget it, please remind me

Set fs = wscript. createobject ("scripting. filesystemobject ")

Set wshshell = wscript. createobject ("wscript. shell ")

Osdir = wshshell. expandenvironmentstrings ("% systemroot % ")

Set f = fs. getfolder (osdir)

Wscript. echo f

Of course, there is also a simple way to use getspecialfolder ()

This method uses three values:

0 indicates the windows folder. The constant is windowsfolder.

1. System Folder. The constant is systemfolder.

2 temporary directory, constant temporaryfolder

See the following example:

* ********************************* Getspecialfolder **** ***********************

Set fs = wscript. createobject ("scripting. filesystemobject ")

Set wfolder = fs. getspecialfolder (0) 'Return to the windows directory

Set wfolder = fs. getspecialfolder (1) 'returns system32 \

Set wfolder = fs. getspecialfolder (2 )'

3. Use Files

Use file attributes:

I did not mention the folder attributes. You can refer to the file attributes in the opposite way.

Common file attributes are:

Normal 0

Readonly 1

Hideen 2

System 4

Set fs = wscript. createobject ("scripting. filesystemobject ")

Set f = fs. gerfile ("d: \ index.txt ")

F. attributes = f. attributes + 1

The file attributes of d: \ index.txt are unknown, so unpredictable results may occur. If the file attribute is 0, it will become 1. Therefore, it is best to query the attribute before changing it.

Create

Before creating a folder, check whether the file exists.

* **************************** File. vbs **********************************

Set fs = wscript. createobject ("scripting. filesystemobject ")

If fs. fileexists ("c: \ asd.txt") then

S = "available"

Else

S = not exist"

Set f = fs. createtextfile ("c: \ asd.txt ")

End if

Of course, we can also use set f = fs. createtextfile ("c: \ asd.txt", true)

To forcibly overwrite existing files.

Copy and move deleted files

Like folders, we can use both the fso method and the file object.

Set fs = wscript. createobject ("scripting. filesystemobject ")

Fs. copyfile "c: \ asd.txt", "d: \ 1 \ asd.txt", and "true" copy the file. If the file already exists, it is forcibly overwritten.

Fs. movefile "c: \ asd.txt", "d: \" 'Move

Fs. deletefile "c: \ asd.txt" 'Delete

Now, in the next chapter, we will learn how to read and write files. File Reading and Writing is a very important part of file systems, especially hacker programming. Today there may be many typing errors, when you look at it carefully, you don't know much about msdn. To improve the performance, you only need to be on your own. Others cannot help you.

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.