VBS text file operation implementation code _vbs

Source: Internet
Author: User
Tags readline first row
For the operation of the file, for example, generate one or open one or delete a. txt format file.
Although the official FILEPlug-ins provide some basic functionality, but not many features.
You do not need to understand what is the FSO template, what is a VBS statement, directly apply the following statement!
Red word for your own definition of the part, the blue word for any selected part

If you can not understand, please refer to the following example!
operation of the same file the red part must be the same

please look carefully at the explanations in the brackets
Define a name named FSOThe object of the relational file problem, the operation of a file is only written once, This sentence must be written
VBS Set fso = CreateObject ("Scripting.FileSystemObject")

0. Determine whether a file or folder exists ( ynis the return value, the file exists to return 1, does not exist return 0)
VBS yn=fso. FileExists ( to determine the directory of a file)

1. Create a file (the blue Word defines whether the text file can be overwritten by the next write, omitting default to Ture)
VBS Set Ttfile=fso.createtextfile ( The file directory created, Ture|false)

2. Open an existing file (the blue definition file is written in a. Read only B. Can read and write, but every time you open a file rewrite C. At the end of the file)
VBS Const Forreading=1
VBS Const FORWRITING=2
VBS Const FORAPPENDING=8
Note: To change the open mode, you must close the file to reopen it
VBS Set Ttfile=fso.opentextfile ( Open Directory for files, forreading|forwriting|forappending)

3. Close an open file (the red portion is the same as the red part of the file that is already open)
VBS Ttfile . Close

4. Read one line of open file and enter (the red part ttfile to the same red portion of the file that is already open)
VBS Read= Ttfile. ReadLine

5. Read all file contents (red part ttfile to the red part of the file already opened)
VBS Read= Ttfile. ReadAll

6. Write one line and return (the red portion is the same as the red part of the file that is already open)
VBS Ttfile. WriteLine ( What you want to write)

7. Delete the specified file (if already defined Ttfile you don't need the first sentence .)
VBS Set Ttfile=fso. GetFile ( directory of files to be deleted )
VBS Ttfile . Delete

8. To determine whether the input tag at the end (is return-1, otherwise return 0)
VBS yn=ttfile. atendofstream

The following are the actions of some folders
8. Determine whether the root directory (yn is the return value, the file exists to return 1, does not exist return 0)
VBS yn = FSO. IsRootFolder

9. Read folder
VBS Set Ttfile=fso. GetFolder ( folder Directory)

10. Create a Folder
VBS Set Ttfile=fso.creafolder ( folder directory created)

11. Delete the specified folder (if already defined Ttfile you don't need the first sentence .)
VBS Set Ttfile=fso. GetFolder ( directory of files to be deleted )
VBS Ttfile . DeleteFolder

Here are some other file actions that are often used (note: Files that are available in all formats.) The scarlet Letter is defined by your previous script.
VBS ttfile. size returns the file size
VBS Ttfile. Type return file type
VBS ttfile. DateCreated Return file creation time
VBS ttfile. datelastaccessed return file recent access time
VBS ttfile. DateLastModified returns the last modified time of the file
VBS Ttfile. Name returns the name of the file
VBS Ttfile. ShortPath returns the short path name of the file
VBS Ttfile. Path returns the physical address of the file

Copy code code as follows:

VBS Set fso = CreateObject (" Scripting.FileSystemObject ")
//Determine if there is a file 1.txt
VBS pd1=fso under D disk. FileExists (d:\1.txt)
If 0=PD1
//If not, create an file under D disk 1.txt
VBS set Txtfile=fso.createtextfile ("D:\1.tx T ", false"
//Open 1.txt
VBS set Txtfile=fso.opentextfile ("D:\1.txt", ForAppending)
//write one line at the end 1234567 890 "
vbs txtfile.writeline (" 1234567890 ")
//close 1.txt
VBS txtfile.close
endif
//Open 1.t as read-only XT
VBS Set Txtfile=fso.opentextfile ("D:\1.txt", ForReading)
//reads the first row and assigns it to the variable read
VBS read=txtfile. ReadLine
//Turn off 1.txt
VBS txtfile.close


Time does not fully list all functions, the insufficient part of the hope that everyone thread up.

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.