Save Me!

A few days ago I showed how to call an Open File dialog box. I promised to show you how to let a user specify a filename and path by browsing using the File dialog box.  This is much better than using an input box, especially when you want to save the file to a specific folder where you don’t know the full path off the top of your head.  Here’s a sample script.

‘SaveAsDemo.vbs
‘specify a default filename
strFile=SaveAs(“C:\demo.txt”)
If strfile<>”” Then WScript.Echo “Saving as ” & strFile

‘don’t specify a filename but set parameter to blank
strFile=SaveAs(“”)
If strfile<>”” Then WScript.Echo “Saving as ” & strFile

‘**********************************
‘*  File SaveAS Function          *
‘**********************************

Function SaveAs(strFile)
‘This requires Windows XP/2003
    Set objDialog=CreateObject(“SAFRCFileDlg.FileSave”)
    objDialog.filename=strFile
    objDialog.OpenFileSaveDlg
    SaveAs=objDialog.FileName
End Function

The object we’re using in the function is almost the same as what we used to open a file. The function as written requires a default vault value, even if it is blank.  If you select an existing file, you’ll get an warning about overwriting the file.  But nothing really happens. The function will return the full path and filename of whatever you select and that’s it.  You still need to use something like the FileSystemObject to create the file. This function simply makes it easier for a user to browse to folder and specify a file name instead of having to type it into an input box.

I prefer providing a default filename and path, expecting that in most cases it will be sufficient and all the user has to do is click Save.

Now you have some flexibility to offer your users when it comes to finding or specifying files and your scripts can look more like Windows utilities.