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.