{"id":63713,"date":"2007-10-26T23:28:00","date_gmt":"2007-10-26T23:28:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/10\/26\/hey-scripting-guy-how-can-i-present-a-dialog-box-that-allows-users-to-rename-files\/"},"modified":"2007-10-26T23:28:00","modified_gmt":"2007-10-26T23:28:00","slug":"hey-scripting-guy-how-can-i-present-a-dialog-box-that-allows-users-to-rename-files","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-present-a-dialog-box-that-allows-users-to-rename-files\/","title":{"rendered":"Hey, Scripting Guy! How Can I Present a Dialog Box that Allows Users to Rename Files?"},"content":{"rendered":"<p><H2><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> <\/H2>\n<P>Hey, Scripting Guy! How can I present users with a dialog box that enables them to rename a file? Ideally I\u2019d like this to be in a loop of some kind, so that they could rename multiple files without having to restart the script.<BR><BR>&#8212; ER<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, ER. Before we begin today we\u2019d like to note that on this date, way back in 1854, American entrepreneur C. W. Post was born. Post was the founder of the Postum cereal company, a company which was eventually renamed Post Cereals. Today, Post makes such breakfast staples as Alpha-Bits, Raisin Bran, and Post Toasties, along with both Cocoa Pebbles <I>and<\/I> Fruity Pebbles! <\/P>\n<P>And no, now that you mention it, we have no idea why C. W. Post\u2019s birthday is not a national holiday. After all, despite his many achievements, even Dr. Martin Luther King, Jr. never invented a cereal like Marshmallow Alpha-Bits.<\/P>\n<TABLE id=\"EDD\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P><B>Note<\/B>. OK, so, technically, neither did C. W. Post; instead, he\u2019s credited with inventing Grape Nuts and Post Toasties (which, by the way, was originally named Elijah\u2019s Mannah). Interestingly enough, however, the first product developed and sold by the C. W. Post company wasn\u2019t even a breakfast cereal; instead, it was a powdered coffee substitute named Postum, which is made by roasting ingredients such as wheat bran, wheat, molasses, and corn dextrin and then grinding the resulting mass into a powder. Just add hot water and enjoy!<\/P>\n<P>In case you\u2019re wondering, Postum is still sold today. And no, we have no idea <I>why<\/I>.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Anyway, happy birthday, C. W. Post. The Scripting Guys wanted to do something to mark the occasion, but we weren\u2019t sure what. And then it hit us: what better tribute to the man who brought us Grape Nuts (which was also originally marketed as another coffee substitute; as it turned out, though, people liked eating the stuff way more than they liked drinking it) than to write a script that presents users with a dialog box that enables them to rename files?<\/P>\n<P>OK, so maybe it isn\u2019t all <I>that<\/I> great of a tribute. But it was the best we could do on such short notice; after all, we didn\u2019t even know it was C. W. Post\u2019s birthday until we heard it on the radio while driving in to work.<\/P>\n<P>Here\u2019s the script:<\/P><PRE class=\"codeSample\">Set objOpenDialog = CreateObject(&#8220;UserAccounts.CommonDialog&#8221;)\nSet objSaveDialog = CreateObject(&#8220;SAFRCFileDlg.FileSave&#8221;)<\/p>\n<p>objOpenDialog.Filter = &#8220;All Files|*.*&#8221;\nobjOpenDialog.InitialDir = &#8220;C:\\&#8221;<\/p>\n<p>Do While True\n    objOpenDialog.FileName = &#8220;&#8221;\n    intResult = objOpenDialog.ShowOpen<\/p>\n<p>    If intResult = 0 Then\n        Wscript.Quit\n    Else\n        strOldFileName = objOpenDialog.FileName\n    End If<\/p>\n<p>    objSaveDialog.FileName = strOldFileName<\/p>\n<p>    intReturn = objSaveDialog.OpenFileSaveDlg<\/p>\n<p>    If intReturn &lt;&gt; 0 Then\n        strNewFileName = objSaveDialog.FileName\n        Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\n        objFSO.MoveFile strOldFileName, strNewFileName\n    Else\n        Wscript.Quit\n    End If\nLoop\n<\/PRE>\n<TABLE id=\"E2D\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Very important note<\/B>. For better or worse (and no doubt a lot of you will say \u201cfor worse\u201d), this script works <I>only<\/I> on Windows XP; it doesn\u2019t work on Windows 2000, Windows Server 2003, Windows Vista, or any other version of Windows. Why not? Because the two dialog box objects needed to carry out this task are available only on Windows XP. (Why were these objects not included in Windows Vista? We have no idea.) Anyway, if you have Windows XP then you\u2019re in business; if you <I>don\u2019t<\/I> have Windows XP, well, sorry, but we don\u2019t really have a very good answer for you. (Unless you consider <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/officetips\/aug05\/tips0825.mspx\"><B>calling dialogs boxes from Microsoft Word<\/B><\/A> a good answer. If you do, well, then we <I>do<\/I> have a good answer for you.)<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>For those of you who <I>are<\/I> running Windows XP, we kick things off by creating two different objects: <B>UserAccounts.CommonDialog<\/B> and <B>SAFRCFileDlg.FileSave<\/B>. The first object is going to present us with a File Open dialog box; the user will use that dialog box to select the file to be renamed. The second object is going to present us with a File Save dialog box; this is the dialog box the user will use to type in a new name for the previously-selected file. <\/P>\n<P>Incidentally, you can also select a new folder for the file; in that case the file will be moved to the new folder and then named, well, whatever name you typed in the dialog box. That means we have a dual purpose script here: not only can it be used to rename files, but it can also be used to move files. Much like the Post cereal Shredded Wheat and Bran, which combines everyone\u2019s two favorite things \u2013 shredded wheat and bran \u2013 in a single cereal.<\/P>\n<TABLE id=\"EBF\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P><B>Note<\/B>. At the risk of interrupting, could you use this script to move multiple files at one time? No, not this script. However, we know people are interested in doing that, so we\u2019ll see if we can tackle that task sometime in the future.<\/P>\n<P>And yes we know: according to the old credo, the future is now. But not here in the Script Center. In the Script Center, even now is sometime in the future.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>After we create our objects we then assign values to two of the File Open dialog box\u2019s properties:<\/P><PRE class=\"codeSample\">objOpenDialog.Filter = &#8220;All Files|*.*&#8221;\nobjOpenDialog.InitialDir = &#8220;C:\\&#8221;\n<\/PRE>\n<P>The <B>Filter<\/B> property enables us to show only files of a specified type (e.g., .txt files) in the dialog box. Of course, we don\u2019t <I>want<\/I> to filter out any file type; therefore, we use the wild card value *.* to indicate that we want to look at all files. Suppose, however, that we were interested only in .txt files. In that case, we would set the filter to the following:<\/P><PRE class=\"codeSample\">objOpenDialog.Filter = &#8220;Text Files|*.txt&#8221;\n<\/PRE>\n<P>This variation lets you switch between .txt and .csv files:<\/P><PRE class=\"codeSample\">objOpenDialog.Filter = &#8220;Text Files|*.txt|CSV Files|*.csv&#8221;\n<\/PRE>\n<P>And <I>this<\/I> variation \u2013 well, we digress.<\/P>\n<P>Which is highly unusual for this column.<\/P>\n<P>Meanwhile, the <B>InitialDir<\/B> property simply instructs the dialog box to, by default, show the C:\\ directory each time it starts up. At the risk of digressing even further, here\u2019s a nifty little block of code; this one causes the dialog box to open up in the working directory of the script itself:<\/P><PRE class=\"codeSample\">Set objShell = CreateObject(&#8220;Wscript.Shell&#8221;)\nstrFolder = objShell.CurrentDirectory\nobjOpenDialog.InitialDir = strFolder\n<\/PRE>\n<P>OK, enough about the InitialDir property. After we assign the property values, we next set up a Do loop designed to run forever (or at least until True is no longer equal to True):<\/P><PRE class=\"codeSample\">Do While True\n<\/PRE>\n<TABLE id=\"EIG\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Don\u2019t worry; we\u2019ll provide a way for you to escape this seemingly-endless loop. Trust us: when you\u2019re a Scripting Guy you <I>always<\/I> have to have an escape plan handy.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Inside this loop, we set the File Open dialog box\u2019s <B>FileName<\/B> property to an empty string; that ensures that a file won\u2019t be pre-selected for you when the dialog box opens. Once that\u2019s done we then go ahead and open the dialog box, something we do by calling the <B>ShowOpen<\/B> method:<\/P><PRE class=\"codeSample\">intResult = objOpenDialog.ShowOpen\n<\/PRE>\n<P>Like all good dialog boxes, our File Open dialog box is going to remain open \u2013 and the script is going to remain \u201cblocked,\u201d until one of two things happens: either we select a file and click <B>Open<\/B>, or we click the <B>Cancel<\/B> button. As soon as one of these buttons is clicked the script unblocks and we execute this block of code:<\/P><PRE class=\"codeSample\">If intResult = 0 Then\n    Wscript.Quit\nElse\n    strOldFileName = objOpenDialog.FileName\nEnd If\n<\/PRE>\n<P>All we\u2019re doing here is examining the value of the variable intResult, a variable that gets populated when a user dismisses the File Open dialog box. If intresult is equal to 0 that means the user clicked the <B>Cancel<\/B> button; in that case, we call the <B>Quit<\/B> method and terminate the script. (Which, needless to say, also breaks us out of our not-so-endless loop.) If intResult is <I>not<\/I> equal to 0 (in which case it would be equal to -1) we then use this line of code to assign the path of the selected file (the dialog box\u2019s FileName property) to a variable named strOldFileName:<\/P><PRE class=\"codeSample\">strOldFileName = objOpenDialog.FileName\n<\/PRE>\n<P>Now it\u2019s time to bring up our second dialog box, the File Save dialog box: <\/P><PRE class=\"codeSample\">intReturn = objSaveDialog.OpenFileSaveDlg\n<\/PRE>\n<TABLE id=\"EWH\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. OK, technically, you don\u2019t have to use a second dialog box; instead, you could bring up the first dialog box, locate the file to be renamed, right-click the file (in the dialog box), and then click <B>Rename<\/B>. That works fine, but it might not be the most intuitive way to go about renaming files. Therefore, we went with the two-dialog box approach.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>When the File Save dialog box appears onscreen, we, again, have two options: 1) enter a new name for the file and then click <B>OK<\/B>, or 2) click <B>Cancel<\/B>. Once we\u2019ve done either one of those things we trigger this block of code:<\/P><PRE class=\"codeSample\">If intReturn &lt;&gt; 0 Then\n    strNewFileName = objSaveDialog.FileName\n    Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\n    objFSO.MoveFile strOldFileName, strNewFileName\nElse\n    Wscript.Quit\nEnd If\n<\/PRE>\n<P>Here we\u2019re using an If Then statement to examine the return value from the dialog box. If intReturn is equal to 0 that means the user clicked <B>Cancel<\/B>; in that case, we call the Quit method and terminate the script. Otherwise we:<\/P>\n<TABLE class=\"numberedList\" border=\"0\" cellSpacing=\"0\" cellPadding=\"0\">\n<TBODY>\n<TR vAlign=\"top\">\n<TD class=\"listNumber\" noWrap align=\"right\">\n<P>1.<\/P><\/TD>\n<TD>\n<P>Get the path of the selected file (the value of the FileName property) and store it in a variable named strNewFileName.<\/P><\/TD><\/TR>\n<TR vAlign=\"top\">\n<TD class=\"listNumber\" noWrap align=\"right\">\n<P>2.<\/P><\/TD>\n<TD>\n<P>Create an instance of the Scripting.FileSystemObject.<\/P><\/TD><\/TR>\n<TR vAlign=\"top\">\n<TD class=\"listNumber\" noWrap align=\"right\">\n<P>3.<\/P><\/TD>\n<TD>\n<P>Call the MoveFile method to \u201cmove\u201d the file from its old path to its new path. In some cases, that might involve actually moving the file from one folder to another. In other cases, the file stays in the same folder but simply gets renamed: for example, C:\\Scripts\\Test.txt is \u201cmoved\u201d to the path C:\\Scripts\\NewName.txt. In the latter scenario, we\u2019re only \u201cmoving\u201d the file in a technical sense; in a practical sense, we\u2019re renaming the file.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>And then we loop around, display a new instance of the File Open dialog box, and repeat the process all over again.<\/P>\n<P>We hope that helps, ER. And now that we\u2019ve officially wished C. W. Post a happy birthday we can let everyone in on a secret: we\u2019re actually a little disappointed that it\u2019s C. W.\u2019s birthday rather than John Harvey Kellogg\u2019s birthday. John Harvey Kellogg, whose brother founded the Kellogg Company, was way more interesting than C. W. Post. For example, in addition to helping invent Kellogg\u2019s Corn Flakes, John Harvey was a noted physician (he believed people should eat only twice a day, and only bland food) and social commentator. He and his wife, Ella, were married for 40 years and never once slept together in the same room. <\/P>\n<P>Needless to say, they had no children.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I present users with a dialog box that enables them to rename a file? Ideally I\u2019d like this to be in a loop of some kind, so that they could rename multiple files without having to restart the script.&#8212; ER Hey, ER. Before we begin today we\u2019d like to note [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[15,3,4,5],"class_list":["post-63713","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-dialog-boxes","tag-scripting-guy","tag-scripting-techniques","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I present users with a dialog box that enables them to rename a file? Ideally I\u2019d like this to be in a loop of some kind, so that they could rename multiple files without having to restart the script.&#8212; ER Hey, ER. Before we begin today we\u2019d like to note [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63713","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=63713"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63713\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=63713"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=63713"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=63713"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}