Vbs COM Open/Save File script code _vbs

Source: Internet
Author: User
Tags save file
You first copy the following code, then save as Filesave.vbs, double-click Open, what do you see?
Copy Code code 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

VBS in many times has been to users with unfriendly file open or save the way, always in the background to dispose of some FSO operation, and can not allow users to do some of their own DIY. So, why not use COM? COM components are always there in the system, and using it, our user experience will improve a lot.
Does the above code not pop up a dialog box that saves test.txt files? Suffix name and filename Everyone to modify, it is estimated that the basic knowledge of the VBS can modify the author of this small script, and embed it into their own script to go?
Let's look at one more example, this time to open the file and get the filename.
Copy Code code 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

Running this script (Fileopen.vbs) will pop up a dialog window that allows the user to select the files themselves, very friendly to let the user choose the file themselves. Bret is the return value performed, not 0, and objfile filename can return the selected file name.

A name, a method to invoke, and nothing more.
If you think this is not handsome enough, you want to use similar to MFC completely DIY CFileDialog, what should you do? Also very simple, the UserAccounts object provides us with the CommonDialog, this more advanced open common dialog box.
Copy code code as follows:

Set ob JDialog = CreateObject ("UserAccounts.CommonDialog")
Objdialog.filter = "vbs file|*.vbs"
Objdialog.initialdir = "E:"
Bret=objdialog.showopen
If BRet then
Strloadfile = Objdialog.filename
WScript.Echo Str LoadFile
Else
Wscript.Quit
End If

Save it as a fileopenadvance.vbs bar, double-click to open, it even filters File types for you to keep an interface.
default filename, suffix filter, suffix index, flag, initialization unknown, owner, one calling 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.