Create a text file in vbs and check whether the file exists

Source: Internet
Author: User
Createtextfile Method

Create a specified file and returnTextstreamObject, which can be used to read or write files created.

Object.Createtextfile(Filename [, overwrite [, Unicode])

Parameters

Object

Required. The name of the FileSystemObject or folder object.

Filename

Required. String expression, indicating the file to be created.

Overwrite

Optional. Boolean indicates whether existing files can be overwritten. If the file can be overwritten, the value isTrueIf the file cannot be overwritten, the value isFalse. If this value is omitted, the existing file will be overwritten.

Unicode

Optional. Boolean indicates whether to create a file in Unicode or ASCII format. If you create a file in unicode format, the value isTrueIf a file is created in ASCII format, the value isFalse. If this part is omitted, an ASCII file is created.

Description

BelowCodeExamplesCreatetextfileMethod To create and open a text file:

 
Sub createafile dim FSO, myfile set FSO = Createobject ("scripting. FileSystemObject") set myfile = FSO.Createtextfile ("C: \ testfile.txt",True)Myfile. writeline ("This is a test.")Myfile. CloseEnd sub

ForFilenameAn existing file. IfOverwriteThe parameter isFalse, Or if this parameter is not provided, an error occurs.

Source: http://www.yesky.com/imagesnew/software/vbscript/html/vsmthCreateTextFile.htm

Below is a small example written by myself. , Code:

On Error resume next

Dim mu_sresult, mu_sfilepath, mu_sfilename
Mu_sresult = "hello, please call 900fd"
Mu_sfilepath = "textfolder /"
Mu_sfilename = mu_sfilepath & "resultvoice.txt"

Dim FS, TS
Set FS = Createobject ("scripting. FileSystemObject ")
If (FS. fileexists (mu_sfilename) then' checks whether the file exists
Msgbox "the file already exists"
End if

Set Ts = FS. createtextfile (mu_sfilename, true, true) 'If the second parameter is not set, the default value is true (overwrite the original file)
TS. writeline mu_sresult
TS. Close

Set Ts = nothing
Set FS = nothing

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.