{"id":70483,"date":"2005-02-08T17:13:00","date_gmt":"2005-02-08T17:13:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/02\/08\/how-can-i-find-and-replace-text-in-a-text-file\/"},"modified":"2005-02-08T17:13:00","modified_gmt":"2005-02-08T17:13:00","slug":"how-can-i-find-and-replace-text-in-a-text-file","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-find-and-replace-text-in-a-text-file\/","title":{"rendered":"How Can I Find and Replace Text in a Text File?"},"content":{"rendered":"<p><IMG 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\"> \n<P>Hey, Scripting Guy! From the command line, how can I use a script to open a file and replace text; for example, how can I replace all instances of \u201cJim\u201d with \u201cJames\u201d?<BR><BR>&#8212; JW<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG 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 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> \n<P>Hey, JW. As we\u2019ve found out numerous times when dealing with text files, there is no obvious way to do this; that is, there is no ReplaceText command that can open a text file and find and replace text. Fortunately, this problem is typical of text file questions in one other respect: although there\u2019s no <I>obvious<\/I> way to carry out the task, we can still find a way to get the job done.<\/P>\n<P>Although we can\u2019t directly search and replace text inside a text file, we can do the next best thing. We can: 1) open up a text file; 2) read the text into a variable; 3) do a search-and-replace <I>on that variable<\/I>; and 4) re-save the text file. We can even do all that from the command line, although we\u2019ll hold off on that for a moment. Instead, let\u2019s start with a simple script that carries out the search and replace:<\/P><PRE class=\"codeSample\">Const ForReading = 1\nConst ForWriting = 2<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\Text.txt&#8221;, ForReading)<\/p>\n<p>strText = objFile.ReadAll\nobjFile.Close\nstrNewText = Replace(strText, &#8220;Jim &#8220;, &#8220;James &#8220;)<\/p>\n<p>Set objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\Text.txt&#8221;, ForWriting)\nobjFile.WriteLine strNewText\nobjFile.Close\n<\/PRE>\n<P>We start off by creating two constants (<B>ForReading<\/B> and <B>ForWriting<\/B>), which we\u2019ll use for the two occasions when we\u2019ll open our text file. (Yes, we said <I>two<\/I> occasions). We create an instance of the <B>FileSystemObject<\/B>, and then use the <B>OpenTextFile<\/B> method to open the file C:\\Scripts\\Text.txt for reading. <\/P>\n<P>With the file open, we use the <B>ReadAll<\/B> method to read the contents of the entire file into the variable strText. We then close C:\\Scripts\\Text.txt even though we\u2019ll almost immediately reopen it, this time for writing. Seems silly, yes, but that\u2019s the way the FileSystemObject works: you can open a file for reading or you can open a file for writing, but you can\u2019t perform both operations at the same time. (As you should know by now, the FileSystemObject works in mysterious ways.)<\/P>\n<P>Having stored the contents of the file in the variable strText, we then use the VBScript <B>Replace<\/B> function to replace all instances of Jim with James. That\u2019s what we do in this line of code:<\/P><PRE class=\"codeSample\">strNewText = Replace(strText, &#8220;Jim &#8220;, &#8220;James &#8220;)\n<\/PRE>\n<P>Notice that we\u2019re looking for each instance of &#8220;Jim &#8221; (<I>Jim<\/I> followed by a blank space) and replacing those with &#8220;James &#8221; (<I>James<\/I> followed by a blank space). Though hardly foolproof, this gives our script a tiny bit of intelligence; if the script encounters the name <I>Jimmy<\/I> it won\u2019t try to replace the <I>Jim<\/I> with <I>James<\/I> (resulting in <I>Jamesmy<\/I>). The new file we create &#8211; the one where each Jim has been replaced by James &#8211; is stored in memory in the variable strNewText.<\/P>\n<P>Next we reopen our file (for writing), call the <B>WriteLine<\/B> method to write the contents of strNewText to the file, and then close the file a second time. The net effect? If we started off with a text file that looked like this:<\/P><PRE class=\"codeSample\">Jim Jones\nMary Smith\nJim Doe\nJim Johnson\nMary Johnston\n<\/PRE>\n<P>we\u2019ll end up with a text file that looks like this:<\/P><PRE class=\"codeSample\">James Jones\nMary Smith\nJames Doe\nJames Johnson\nMary Johnston\n<\/PRE>\n<P>As for doing this all from the command-line, we simply need to modify the script so that it will accept &#8211; in this order &#8211; three command-line arguments: the name of the file to open; the text we want to search for; and the text we want to replace. Here\u2019s a script that does just that. Note that we store the command-line arguments in the variables strFileName, strOldText, and strNewText, and we use those variables when opening and saving the text file and when calling the Replace function:<\/P><PRE class=\"codeSample\">Const ForReading = 1\nConst ForWriting = 2<\/p>\n<p>strFileName = Wscript.Arguments(0)\nstrOldText = Wscript.Arguments(1)\nstrNewText = Wscript.Arguments(2)<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objFile = objFSO.OpenTextFile(strFileName, ForReading)<\/p>\n<p>strText = objFile.ReadAll\nobjFile.Close\nstrNewText = Replace(strText, strOldText, strNewText)<\/p>\n<p>Set objFile = objFSO.OpenTextFile(strFileName, ForWriting)\nobjFile.WriteLine strNewText\nobjFile.Close\n<\/PRE>\n<P>To use this revised script (which we\u2019ll call <B>replace.vbs<\/B>) just type a command similar to this from the command prompt:<\/P><PRE class=\"codeSample\">cscript replace.vbs &#8220;C:\\Scripts\\Text.txt&#8221; &#8220;Jim &#8221; &#8220;James &#8221;\n<\/PRE><BR>\n<DIV>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" width=\"100%\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"\"><A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/feb05\/hey0208.mspx#top\"><IMG height=\"9\" alt=\"Top of page\" src=\"http:\/\/www.microsoft.com\/technet\/mnplibrary\/templates\/MNP2.Common\/images\/arrow_px_up.gif\" width=\"7\" border=\"0\"><\/A><A class=\"topOfPage\" href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/feb05\/hey0208.mspx#top\">Top of page<\/A><\/TD><\/TR><\/TBODY><\/TABLE><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! From the command line, how can I use a script to open a file and replace text; for example, how can I replace all instances of \u201cJim\u201d with \u201cJames\u201d?&#8212; JW Hey, JW. As we\u2019ve found out numerous times when dealing with text files, there is no obvious way to do this; that [&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":[3,4,14,5],"class_list":["post-70483","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-text-files","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! From the command line, how can I use a script to open a file and replace text; for example, how can I replace all instances of \u201cJim\u201d with \u201cJames\u201d?&#8212; JW Hey, JW. As we\u2019ve found out numerous times when dealing with text files, there is no obvious way to do this; that [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70483","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=70483"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70483\/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=70483"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70483"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70483"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}