{"id":325,"date":"2006-11-24T02:00:00","date_gmt":"2006-11-24T10:00:00","guid":{"rendered":"http:\/\/testblog.sapien.com\/index.php\/2006\/11\/24\/open-sesame\/"},"modified":"2006-11-24T02:00:00","modified_gmt":"2006-11-24T10:00:00","slug":"open-sesame","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2006\/11\/24\/open-sesame\/","title":{"rendered":"Open Sesame"},"content":{"rendered":"<p>As you develop more and more scripts, you&#8217;re bound to find yourself working with files. Often, the easiest thing to do is to hard code a filename that we want to open and use. Or we may provide a run time parameter for a file name. Wouldn&#8217;t it be nice to be give the user an option to browse for the file like other Windows apps? If your script is running on Windows XP or Windows 2003, you can use the SAFRCFileDlg object.<\/p>\n<p>This object has one property (Filename) and one method (OpenFileOpenDlg). Here&#8217;s a short script that demonstrates how to use this object.<\/p>\n<p>&#8216;<span style=\"font-family: Courier New,Courier,mono;\">OpenFileDemo.vbs<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">myfile=OpenFile()<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">wscript.echo &#8220;Opening &#8221; &amp; myfile<\/span><br style=\"font-family: Courier New,Courier,mono;\"><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&#8216;**********************************<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&#8216;*&nbsp; Open File Function&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; *<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&#8216;**********************************<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Function OpenFile()<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&nbsp;&nbsp;&nbsp; On Error Resume Next<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&nbsp;&nbsp;&nbsp; Dim objDialog<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&nbsp;&nbsp;&nbsp; &#8216;This requires Windows XP\/2003<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&nbsp;&nbsp;&nbsp; Set objDialog=CreateObject(&#8220;SAFRCFileDlg.FileOpen&#8221;)<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&nbsp;&nbsp;&nbsp; objDialog.OpenFileOpenDlg<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&nbsp;&nbsp;&nbsp; OpenFile=objDialog.FileName<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">End Function<\/span><\/p>\n<p>This function will return what ever is entered in the dialog box as objDialog.FileName. A user could simply mistype a filename in the dialog box. You&#8217;ll still want to verify the existence of the file with the FileSystemObject before doing anything with it. <\/p>\n<p>I wish there was someway to populate a default or specify a starting directory. It appears the object itself has no interface for this and will open to the last directory used. I&#8217;ve tried to find a workaround, but it looks like this would involve mucking around with binary registry entries so I guess I&#8217;ll live with it.<\/p>\n<p>What about saving a file?&nbsp; I&#8217;ll show you that another day.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As you develop more and more scripts, you&#8217;re bound to find yourself working with files. Often, the easiest thing to do is to hard code a filename that we want to open and use. Or we may provide a run time parameter for a file name. Wouldn&#8217;t it be nice to be give the user an option to browse for the file like other Windows apps? If your script is running on Windows XP or Windows 2003, you can use the SAFRCFileDlg object.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2,24],"tags":[],"class_list":["post-325","post","type-post","status-publish","format-standard","hentry","category-general","category-vbscript"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/325","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=325"}],"version-history":[{"count":0,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/325\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=325"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=325"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=325"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}