Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of using fso to operate files

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "the method of using fso to operate files". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Instead of talking nonsense, let's take a look at which objects fso consists of:

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: checking and working with fil

Files collection: provides a list of files in a folder

Folder objects: checking and working with folders

Folders collection: provides a list of subfolders of folders

Textstream objects: reading and writing text files

Take a look at the fso method: because there are many, so I will not write down the role of each, if there is anything you do not understand, check the msdn for yourself. Don't say no.

Bulidpath: add file path information to an existing file path

Copyfile

Copyfolder

Createfolder

Createtextfile

Deletefile

Deletefolder

Dreveexits

Fileexits

Folderexists

Getabsolutepathname: returns the absolute path to 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: returns the 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, when I see here, I think you all understand more than half of it. Maybe I don't have to say more about it later. The script is so simple, hehe, let's go on.

1. Use fso

Since fso is not part of wsh, we need to model it

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

In this way, the model of fso is established. It's easy to release it, set fs=nothing.

2. Use folders

Create:

Before we create it, we need to check if it exists and take a 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 both c:\ data and d:\ data exist at this time, there will be an error and replication will stop. If you want to force overwriting, use fs.copyfolder "c:\ data"d:\ data", true

move

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

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

About wildcards:

We can use wildcards to facilitate operation:

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

Notice that I didn't use "\" at the end of the destination path, which means I didn't write:

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

If you write it this way, if the d:\ working directory doesn't exist, windows won't automatically create it for us.

In addition, you have noticed that none of the above involves folder objects. We are all using the methods provided by fso. Of course, we can also use folder:

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

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

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

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

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

Special folder

Generally refers to the system folder:\ windows\ system32, temporary folder, windows folder

Look below, we use environment variables to get the windows directory. We will talk about environment variables in more detail later. 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 three values:

0 indicates the windows folder, and the related constant is windowsfolder

1 system folder, the related constant is systemfolder

2 temporary directory, related constant temporaryfolder

Look at the following example:

* * getspecialfolder**

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

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

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

Set wfolder=fs.getspecialfolder (2) 'returns the temporary directory

3. Use files

Use file properties:

I didn't say the properties of the folder. You can learn from the attributes of the file.

The most 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

Unpredictable results occur here because the file attribute of d:\ index.txt is not known, and if the file attribute is 0, it becomes 1. So it's best to query before changing the attribute.

Create

You need to check whether the file exists before creating it, in the same way as the folder mentioned earlier.

* * 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 an existing file to be overwritten

Copy, move, delete files

Like folders, we can use either the methods provided by fso or the file object

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

Fs.copyfile "c:\ asd.txt", "d:\ 1\ asd.txt", true 'copy files and force overwrite if they already exist

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

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

This is the end of the content of "the method of using fso to operate files". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report