{"id":56083,"date":"2008-03-03T23:55:00","date_gmt":"2008-03-03T23:55:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2008\/03\/03\/hey-scripting-guy-how-can-i-change-a-file-thats-found-in-a-folder-and-in-all-its-top-level-subfolders\/"},"modified":"2008-03-03T23:55:00","modified_gmt":"2008-03-03T23:55:00","slug":"hey-scripting-guy-how-can-i-change-a-file-thats-found-in-a-folder-and-in-all-its-top-level-subfolders","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-change-a-file-thats-found-in-a-folder-and-in-all-its-top-level-subfolders\/","title":{"rendered":"Hey, Scripting Guy! How Can I Change a File That\u2019s Found in a Folder and in All Its Top-Level Subfolders?"},"content":{"rendered":"<p><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\" \/> <\/p>\n<p>Hey, Scripting Guy! I need a little help. I have one top folder with many subfolders ; the subfolders are all top-level subfolders like C:\\Scripts\\Folder1 and C:\\Scripts\\Folder2. In each subfolder I have a text file with the same name. I need to open that text file in each folder and change a couple of characters. Is there any way that you could help me with this?<\/p>\n<p>&#8212; DJ<\/p>\n<p><img decoding=\"async\" height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\" \/><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\" \/><a href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><img decoding=\"async\" class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\" \/><\/a> <\/p>\n<p>Hey, DJ. Well, this is it: today is the last official day of the <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/default.mspx\"><b>2008 Winter Scripting Games<\/b><\/a>. By the time most of you read this the remaining two \u201cmain\u201d events (Events 9 and 10) will have already closed; the last event in the Sudden Death Challenge closes at 3:00 PM (Redmond time) this afternoon. Does that mean you\u2019re out of luck for this year? Heck no. Hustle on over to the <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/games08\/sdhome.mspx\"><b>Sudden Death Challenge<\/b><\/a> home page and enter Event 10; do that, and you\u2019ll be eligible for all the <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/games08\/prizes.mspx\"><b>great prizes<\/b><\/a> we\u2019re giving away as part of the 2008 Scripting Games, including the Dr.Scripto Bobblehead dolls and the 50 copies of Windows Vista Ultimate.<\/p>\n<table class=\"dataTable\" id=\"EOD\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\"><b>Note<\/b>. Isn\u2019t that a little unfair for someone to enter the Games at the very last minute, complete an event that doesn\u2019t even require very much effort, then walk away with one of the big prizes, all at the expense of someone who spent the last two weeks writing scripts for each event in each division? Well, we admit that it <i>sounds<\/i> a little unreasonable. However, it can\u2019t possibly be unfair; after all, as near as we can tell that\u2019s the way Microsoft doles out annual performance bonuses.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>At any rate, we hope everyone enjoyed the Scripting Games this year; we can tell you that the <i>Scripting Guys<\/i> thoroughly enjoyed the Games, aside from an occasional lack of sleep. And we\u2019re already looking forward to next year, when we hope to bring you a <i>really<\/i> cool competition. We haven\u2019t picked out the dates for the 2009 Games just yet, but February 13, 2009 is also a Friday the 13<sup>th<\/sup>. It would be pretty hard for us to pass up the symbolism of starting the Scripting Games on Friday the 13<sup>th<\/sup>. Mark your calendar now; you won\u2019t want to miss it.<\/p>\n<p>Of course, as the Games wind down that means the Scripting Guys have to get back to their <i>real<\/i> jobs. To tell you the truth, we can\u2019t even remember what those real jobs were; like we said, we haven\u2019t gotten much sleep in the past couple weeks. As best we can recall, however, those jobs involved showing people how to write scripts that can modify a specific text file found in a folder and the top-level subfolders of that folder. At least we <i>hope<\/i> that\u2019s what our job involves. Otherwise there might not be a lot of use for the following:<\/p>\n<pre class=\"codeSample\">Const ForReading = 1\nConst ForWriting = 2\n\nstrFileName = \"Test.txt\"\nstrParentFolder = \"C:\\Scripts\\\"\n\nSet objFSO = CreateObject(\"Scripting.FileSystemObject\")\n\nstrTextFile = strParentFolder &amp; strFileName\n\nSet objFile = objFSO.OpenTextFile(strTextFile, ForReading)\nstrContents = objFile.ReadAll\nobjFile.Close\n\nstrContents = Replace(strContents, \"Contoso.com\", \"Fabrikam.com\")\n\nSet objFile = objFSO.OpenTextFile(strTextFile, ForWriting)\nobjFile.Write strContents\nobjFile.Close\n\nSet objFolder = objFSO.GetFolder(strParentFolder)\nSet colSubfolders = objFolder.Subfolders\n\nFor Each objSubfolder in colSubfolders\n    strFolderName = objSubfolder.Path\n    strTextFile = strFolderName &amp; \"\\\" &amp; strFileName\n\n    Set objFile = objFSO.OpenTextFile(strTextFile, ForReading)\n    strContents = objFile.ReadAll\n    objFile.Close\n\n    strContents = Replace(strContents, \"Contoso.com\", \"Fabrikam.com\")\n    Set objFile = objFSO.OpenTextFile(strTextFile, ForWriting)\n    objFile.Write strContents\n    objFile.Close\nNext\n<\/pre>\n<p>Let\u2019s see if we can figure out how this script works, and if we can figure it out before we fall asleep. <\/p>\n<table class=\"dataTable\" id=\"EUE\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p><b>Note<\/b>. As you might have guessed, it\u2019s been another long night here in Scriptingville; it\u2019s already past midnight, and the Scripting Guy who writes this column still has several things to do before he can go to bed. But is he complaining? You <i>bet<\/i> he is. But the Scripting Editor, who can\u2019t go to bed until the Scripting Guy who writes this column finishes writing this column, isn\u2019t offering much sympathy.<\/p>\n<p>In other words, we have the power to keep the Scripting Editor awake all night, waiting for us to finish this column. Hmm \u2026 you know, that reminds us of several hundred stories we\u2019d like to tell you before we explain how today\u2019s script works\u2026.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>Never mind; we\u2019re too tired for that ourselves. Instead, we\u2019ll just point out that we start our script by defining a pair of constants, ForReading and ForWriting; we\u2019ll need these two constants when we open the text files in question. We then assign values to two variables. The variable strFileName is assigned the name of the file that we need to modify; in our case, that\u2019s <i>Test.txt<\/i>. Meanwhile, the variable strParentFolder is assigned the name of the, well, parent folder. For this sample script, that\u2019s C:\\Scripts\\.<\/p>\n<p>After taking care of those setup chores we next create an instance of the <b>Scripting.FileSystemObject<\/b> object; we\u2019re going to use this multi-purpose COM object both to read from and write to text files as well as to retrieve a collection of all the top-level subfolders in C:\\Scripts. We then construct a path to the text file in the parent folder (C:\\Scripts\\Test.txt) by combining the folder name and the file name:<\/p>\n<pre class=\"codeSample\">strTextFile = strParentFolder &amp; strFileName\n<\/pre>\n<p>Why do we need that path? That\u2019s easy; in the next line of code we\u2019re going to use the <b>OpenTextFile<\/b> method to open that text file for reading:<\/p>\n<pre class=\"codeSample\">Set objFile = objFSO.OpenTextFile(strTextFile, ForReading)\n<\/pre>\n<p>As soon as the file is open we use the <b>ReadAll<\/b> method to read in the entire contents of the file, storing that information in a variable named strContents. We then use the <b>Close<\/b> method to close the file.<\/p>\n<p>But not for long; we\u2019ll be revisiting this file in a few seconds.<\/p>\n<p>DJ didn\u2019t tell us exactly what characters he needed to replace in these files, so we\u2019ll use a simple example. Suppose each of these files lists the domain <i>Contoso.com<\/i>, and that domain has recently been changed to <i>Fabrikam.com<\/i>. How can we replace all instances of <i>Contoso.com<\/i> with <i>Fabrikam.com<\/i>? Why, like this, of course:<\/p>\n<pre class=\"codeSample\">strContents = Replace(strContents, \"Contoso.com\", \"Fabrikam.com\")\n<\/pre>\n<p>All we\u2019re doing here is using the <b>Replace<\/b> method to search through the variable strContents (a \u201cvirtual\u201d copy of the text file) and replace any instances of <i>Contoso.com<\/i> with <i>Fabrikam.com<\/i>. Believe it or not, it\u2019s that easy.<\/p>\n<p>Of course, that only modified the virtual copy of the file; we now need to make these changes to the actual text file itself. To do that, we first re-open Test.txt, this time for writing:<\/p>\n<pre class=\"codeSample\">Set objFile = objFSO.OpenTextFile(strTextFile, ForWriting)\n<\/pre>\n<p>Once that\u2019s done we can call the <b>Write<\/b> method to overwrite the existing contents of the file with the value of strContents:<\/p>\n<pre class=\"codeSample\">objFile.Write strContents\n<\/pre>\n<p>From there we call the Close method and we\u2019re done.<\/p>\n<p>Well, done with the file in the parent folder, at least; now we have to do something about the version of Test.txt that appears in each top-level subfolder. To that end, we use the following two lines of code to bind to the parent folder (C:\\Scripts) and then return a collection of all the top-level subfolders of that folder:<\/p>\n<pre class=\"codeSample\">Set objFolder = objFSO.GetFolder(strParentFolder)\nSet colSubfolders = objFolder.Subfolders\n<\/pre>\n<p>Now we\u2019re simply going to follow the same process as when we changed the copy of Test.txt found in the parent folder. To begin with, we construct a path to the copy of Test.txt in the first subfolder by using these two lines of code:<\/p>\n<pre class=\"codeSample\">strFolderName = objSubfolder.Path\nstrTextFile = strFolderName &amp; \"\\\" &amp; strFileName\n<\/pre>\n<p>Once we\u2019ve constructed that path we use the FileSystemObject to open the file and read the contents into memory. We do our little search-and-replace operation, then write the modified contents back to the file. And then we loop around and repeat the process with the next subfolder in the collection.<\/p>\n<p>Child\u2019s play.<\/p>\n<p>Well, assuming your children write system administration scripts, that is. And, apparently, <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/games08\/pictures.mspx\"><b>some people<\/b><b>\u2019s children<\/b><\/a> do.<\/p>\n<p>And that\u2019s it, DJ. Problem solved!<\/p>\n<p>Before we call it a night we\u2019d like to once more thank everyone who took part in the 2008 Winter Scripting Games; we are very excited about the number of people who entered this time around. (And a tip of the hat to all you Perl people; thank you for your support.) Remember, too, that while you can no longer submit a new entry for an event we <i>have<\/i> extended the deadline for resubmitting an existing entry; that deadline is now Thursday, March 6, 2008 at 8:00 AM Redmond time. In other words, if you got a 0 for one of your events, well, there\u2019s no need to worry; you have until Thursday morning to fix the problem with your script and resubmit it. Assuming the new script works, you\u2019ll get the points for that event.<\/p>\n<p>Oh, and don\u2019t forget to <a href=\"mailto:scripter@microsoft.com?subject=2008 Scripting Games Feedback\"><b>send us feedback<\/b><\/a> regarding the Games. Let us know what you liked and what you didn\u2019t like, and let us know what sorts of things you\u2019d like to see included in the 2009 Games. And that includes ideas for events; if you have an idea for an event (for either the Beginners or the Advanced division), well, we\u2019d love to hear about it.<\/p>\n<p>And just think: if enough people submit event ideas, we might not have to do <i>any<\/i> thinking when it comes time to plan the 2009 Winter Scripting Games. Needless to say, not thinking about things is what the Scripting Guys do best.<\/p>\n<p>Well, that and not sleeping.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I need a little help. I have one top folder with many subfolders ; the subfolders are all top-level subfolders like C:\\Scripts\\Folder1 and C:\\Scripts\\Folder2. In each subfolder I have a text file with the same name. I need to open that text file in each folder and change a couple of characters. [&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":[38,11,3,12,5],"class_list":["post-56083","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files","tag-folders","tag-scripting-guy","tag-storage","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I need a little help. I have one top folder with many subfolders ; the subfolders are all top-level subfolders like C:\\Scripts\\Folder1 and C:\\Scripts\\Folder2. In each subfolder I have a text file with the same name. I need to open that text file in each folder and change a couple of characters. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/56083","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=56083"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/56083\/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=56083"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=56083"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=56083"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}