Opening/SAVING file script code for vbs com

Source: Internet
Author: User

First Code Copy the file and save it as filesave. vbs. Double-click it to open it. What do you see? Copy code The Code is as follows: Set objdialog = Createobject ("safrcfiledlg. filesave ")
Set FSO = Createobject ("scripting. FileSystemObject ")
Objdialog. filename = "test"
Objdialog. filetype = ". txt"
Intreturn = objdialog. openfilesavedlg
If intreturn then
FSO. createtextfile (objdialog. filename & objdialog. filetype)
Else
Wscript. Quit
End if

in many cases, vbs always opens or saves unfriendly files to users and processes some FSO operations in the background, instead, you cannot perform some DIY operations on your own. So why not use com? COM has always existed in the system. Using it, our user experience will be improved a lot.
why can't I publish a dialog box containing the test.txt file? You can modify the extension names and file names by yourself. It is estimated that anyone who understands vbs basic knowledge can modify this small script and embed it into your own script?
let's look at another example. This is to open the file and get the file name. copy Code the code is as follows: set objfile = Createobject ("safrcfiledlg. fileopen ")
Bret = objfile. openfileopendlg
if Bret then
wscript. echo "file opened successfully! File Name: "& objfile. filename
else
wscript. Quit
end if

Run this script (fileopen. vbs). A dialog box for users to select files is displayed, which allows users to select files easily. Bret is the execution return value. If it is not 0, the operation succeeds. the filename of the objfile can return the selected file name.
A name and a call method.
If you think this is not handsome enough, what should you do if you want to use a cfiledialog similar to self-DIY in MFC? It is also very simple. The useraccounts object provides us with commondialog, which is a more advanced Open General dialog box.Copy codeThe Code is as follows: Set objdialog = Createobject ("useraccounts. commondialog ")
Objdialog. Filter = "vbs file | *. vbs"
Objdialog. initialdir = "E :"
Bret = objdialog. showopen
If Bret then
Strloadfile = objdialog. filename
Wscript. Echo strloadfile
Else
Wscript. Quit
End if

Save it as fileopenadvance. vbs and double-click it to open it. The filtered file type is reserved for you. Default file name, suffix filter, suffix index, flag, initialization unknown, owner, a call method.

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.