VBScript script Programming tutorial 2 using the FSO for file manipulation _vbs

Source: Internet
Author: User
by sssa2000
Let's take a look at how to use the FSO for file operation. FSO is the core of file operation in the VBS. As a hacker, no matter what language to learn, the operation of the document should be at the fingertips, so please study carefully.

Do not say nonsense, first look at the FSO consists of the following objects:



Drive object: Contains information about the storage device, including hard disk, optical drive, RAM disk, network drive

Drives collection: Provides a list of physical and logical drives

File objects: Examining and processing files

Files Collection: Provides a list of documents in a folder

Folder objects: Checking and processing folders

Folders collection: Provides a list of subfolders of a folder

TextStream objects: Reading and writing text files



Look at the FSO method: Because a lot, so I will not write the role of each, if you do not understand, I check MSDN. Don't say no, no.

Bulidpath: Add file path information to 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 to a file or folder

Getdrive: Returns a Dreve object

GetDriveName: Returns the name of a drive

Getextensionname: Return extension

GetFile: Returns a File object

GetFileName: Returns the file name in the folder

GetFolder

Getparentfoldername: Returns the parent folder of a folder

GetSpecialFolder: Returns an 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, see here I think we also understand the half, may not be behind me to say more, the script is so simple, oh ah, or continue to put.



1, the use of FSO

Since the FSO is not part of the WSH, we need to build his model

For example set Fs=wscript.createobject ("Scripting.FileSystemObject")

In this way, the FSO model is established. It's also easy if you want to release it, set fs=nothing



2, use the folder

Create:

We need to check for existence before creating, look at the program

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, 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 that if C:\Data and D:\data are present at this time, there will be errors and replication will stop, and if you want to force overrides, use Fs.copyfolder "C:\Data" "D:\data", True



Move

Set Fs=wscript.createobject ("Scripting.FileSystemObject")

Fs.movefolder "C:\Data" "D:\data"



About wildcard characters:

We can use the wildcard character for easy operation:

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

Note No, I didn't use "\" at the end of the destination path that means I didn't write this:

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

In this case, if the d:\working directory does not exist, Windows will not automatically create this directory for us.



Another point, we note that none of the above mentioned are not involved in the folder object, we are in the FSO provided by the method, of course, the same as the use of folder:

Set fs= wscript.createobject ("Scripting.FileSystemObject")

Set F=fs.getfolder ("C:\Data")

F.delete ' Delete. If there is a subdirectory, it will also be deleted

F.copy "D:\working", True ' copy to D:\working

F.move: "D:\temp" moved to D:\temp



Special folders

Generally refers to the system folder: \Windows\System32, temporary folder, Windows folder

Look below, we use the environment variables to get the Windows directory, about environment variables we'll go over the details in a later sermon, if I forget, 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 a simple way to use GetSpecialFolder ()

This method uses 3 kinds of values:

0 represents the Windows folder, and the associated constants are Windowsfolder

1 system folders, related constants are Systemfolder

2 temporary directories, related constants Temporaryfolder

Look at the following example:

getspecialfolder***************************

Set Fs=wscript.createobject ("Scripting.FileSystemObject")

Set Wfolder=fs.getspecialfolder (0) ' returns to the Windows directory

Set Wfolder=fs.getspecialfolder (1) ' Returns system32\

Set Wfolder=fs.getspecialfolder (2) ' Returns temporary directory



3, the use of documents

Use file properties:

Folder Properties I didn't say, you can extrapolate from the file properties.

The common use of file attributes is:

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



Because it doesn't know D:\index.txt's file attributes, unpredictable results occur, and if the file has a property of 0, it becomes 1. So it's better to query before changing attributes



Create

You need to check that the file exists before you create it, as in the previous folder method

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 force overwriting of existing files.



Copy move Delete file

As with folders, we can use either the FSO-provided method or the file object

Set Fs=wscript.createobject ("Scripting.FileSystemObject")

Fs.copyfile "C:\asd.txt", "D:\1\asd.txt", True ' copy file, Force overwrite if already exists

Fs.movefile "C:\asd.txt", "D:\" ' Move

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



Well, in the next chapter we're going to learn how to read and write files, file reading and writing is a file system, especially hackers programming inside a very important part of the typing today may be a lot of mistakes, we look at the time carefully, do not know more to see MSDN, to improve the level only on their own, others can not 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.