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.