{"id":70913,"date":"2004-11-30T19:31:00","date_gmt":"2004-11-30T19:31:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2004\/11\/30\/can-i-edit-ini-files-using-a-script\/"},"modified":"2004-11-30T19:31:00","modified_gmt":"2004-11-30T19:31:00","slug":"can-i-edit-ini-files-using-a-script","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/can-i-edit-ini-files-using-a-script\/","title":{"rendered":"Can I Edit .INI Files Using a Script?"},"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! Can I edit.INI files using a script?<BR><BR>&#8212; MZ<\/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, MZ. Yes, you <I>can<\/I> edit .INI files (or any kind of text files, for that matter) using a script. The method you have to use might not be the most elegant process in the world, but it\u2019ll work. And that\u2019s all we really care about, right?<\/P>\n<P>Before we get into the whys and wherefores of editing .INI files, let\u2019s take a look at a sample file. This happens to be a section of the .INI file for Adobe Reader, although most .INI files will look similar:<\/P><PRE class=\"codeSample\">[OEM Install]\nDisplayWelcomeDlg=YES\nDisplayEULA=NO\nDisplayTypeOfInstallDlg=NO\nDisplaySelectDestDirDlg=YES\nDisplayCustomDlg=NO\nDisplayUserInfoDlg=NO\nDisplayConfirmRegDlg=NO\nDisplayStartCopyDlg=NO\nDisplayFinishDlg=NO\nDisplayFinalMessage=YES\nDisplayRebootDlg=YES\nProgGroupName=\nDefaultDestDir=\nUserName=\nUserCompanyName=\nUserSerialNumber=\n<\/PRE>\n<P>Suppose we want to do two things to this file: we want to change <B>DisplayWelcomeDlg<\/B> to NO, and we want to set the <B>UserName<\/B> to Ken Myer. Ideally, we\u2019d write a script that would simply search the file for DisplayWelcomeDlg and change the assigned value to NO, search for the UserName property and set the value to Ken Myer, and then save the changes. Unfortunately, the FileSystemObject &#8211; the technology we need to use to read and modify text files &#8211; doesn\u2019t have those capabilities. Instead, we\u2019ll have to edit the .INI file by brute force. That probably doesn\u2019t make much sense to you at the moment, but hopefully you\u2019ll understand what we mean by the time you reach the end of this column.<\/P>\n<P>Let\u2019s show you the completed script, and then we\u2019ll explain how it works:<\/P><PRE class=\"codeSample\">Const ForReading = 1\nConst ForWriting = 2<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objTextFile = objFSO.OpenTextFile(&#8220;sample.ini&#8221;, ForReading)<\/p>\n<p>Do Until objTextFile.AtEndOfStream\n    strNextLine = objTextFile.Readline<\/p>\n<p>    intLineFinder = InStr(strNextLine, &#8220;DisplayWelcomeDlg&#8221;)\n    If intLineFinder &lt;&gt; 0 Then\n        strNextLine = &#8220;DisplayWelcomeDlg=NO&#8221;\n    End If<\/p>\n<p>    intLineFinder = InStr(strNextLine, &#8220;UserName&#8221;)\n    If intLineFinder &lt;&gt; 0 Then\n        strNextLine = &#8220;UserName=Ken Myer&#8221;\n    End If<\/p>\n<p>    strNewFile = strNewFile &amp; strNextLine &amp; vbCrLf\nLoop<\/p>\n<p>objTextFile.Close<\/p>\n<p>Set objTextFile = objFSO.OpenTextFile(&#8220;sample.ini&#8221;, ForWriting)<\/p>\n<p>objTextFile.WriteLine strNewFile\nobjTextFile.Close\n<\/PRE>\n<P>We start off by defining two constants: ForReading and ForWriting. These two constants will be required when we read and write to the .INI file. Note that there isn\u2019t a constant called ForEditing; that\u2019s because the FileSystemObject doesn\u2019t allow you to both read and write a file at the same time. Instead, we\u2019ll have to open the file and read it, then we\u2019ll have to close the file and re-open it for writing. At that point, we\u2019ll be able to save our changes. Like we said, it\u2019s not the most elegant system ever devised, but, hey, it is what it is.<\/P>\n<P>After defining the two constants we open our .INI file (sample.ini) for reading. We then set up a Do loop in which we\u2019ll read the file line-by-line, until there is nothing left to read (when the AtEdnOfStream property is TRUE). We start by using the Readline method to read the first line of the file, and store that line in the variable strNextLine.<\/P>\n<P>Now the fun begins. As we noted earlier, the FileSystemObject doesn\u2019t really allow you to search a file; instead, we have to read the file line-by-line, then individually check each line to see if it happens to be of interest. That\u2019s exactly what we\u2019re doing with this line of code:<\/P><PRE class=\"codeSample\">intLineFinder = InStr(strNextLine, &#8220;DisplayWelcomeDlg&#8221;)\n<\/PRE>\n<P>This code uses the InStr method to see if the string <B>DisplayWelcomeDlg<\/B> can be found anywhere within the variable strNextLine (and, remember, this variable contains the .INI line we just read in). InStr works by reporting the character position at which the target string is found. For example, suppose strNextLine equaled this:<\/P><PRE class=\"codeSample\">xxxxxDisplayWelcomeDlgxxxxx\n<\/PRE>\n<P>In that case, InStr would return a 6, because our target string starts at the sixth position (we have 5 x\u2019s, then we have DisplayWelcomeDlg). If the target string can\u2019t be found, then InStr returns a 0. <\/P>\n<P>And that\u2019s exactly what happens when we read the first line of the .INI file. Because the first line in the file is <B>[OEM Install]<\/B>, InStr returns a 0. Consequently, we skip our first If-Then statement, and continue on. We then check to see if <B>UserName<\/B> can be found anywhere in the variable. Again, InStr returns a 0, so we skip the second If-Then statement as well.<\/P>\n<P>This is what we mean by editing the file using brute force: we\u2019re checking each and every line of the file for each of our test conditions. Suppose we wanted to modify the <B>DisplayUserInfoDlg<\/B> property, too. Well, then we\u2019d have to add a <I>third<\/I> test condition, one similar to this:<\/P><PRE class=\"codeSample\">intLineFinder = InStr(strNextLine, &#8220;DisplayUserInfoDlg&#8221;)\n<\/PRE>\n<P>Got that? We\u2019ve now checked the first line of the .INI file, and it isn\u2019t a line of interest. So do we just discard it and move on? Nope, not quite. Instead, we use this line of code:<\/P><PRE class=\"codeSample\">strNewFile = strNewFile &amp; strNextLine &amp; vbCrLf\n<\/PRE>\n<P>What this code does is construct a brand-new .INI file in memory, storing this new file in the variable strNewFile. The first time we run through the loop, strNewFile will consist of everything currently in strNewFile (which is nothing the first time through) <I>plus<\/I> whatever happens to be in the variable strNextLine ([OEM Install]) <I>plus<\/I> a carriage return linefeed (vbCrLf). In other words, after one iteration strNewFile looks like this:<\/P><PRE class=\"codeSample\">[OEM Install]\n<\/PRE>\n<P>Exciting, isn\u2019t it?<\/P>\n<P>Now we loop around and read the second line in the text file (DisplayWelcomeDlg=YES). And guess what: this time InStr <I>finds<\/I> the target string, and the variable intLineFinder will equal 1 (because DisplayWelcomeDlg begins in the first character position). Because intLineFinder does not equal 0, we now enter our first If-Then block. Inside that block, we simply change the value of strNewLine:<\/P><PRE class=\"codeSample\">strNextLine = &#8220;DisplayWelcomeDlg=NO&#8221;\n<\/PRE>\n<P>Now our variable doesn\u2019t contain the text we read in from the .INI file; instead, it contains this modified text:<\/P><PRE class=\"codeSample\">DisplayWelcomeDlg=NO\n<\/PRE>\n<P>In other words, we didn\u2019t really edit the value of the DisplayWelcomeDlg property. Instead, we simply threw out the old line and replaced it with a brand-new line. However, the end result is no different than it would have been had we\u2019d been able to edit the property value directly.<\/P>\n<P>When we get to the end of the loop, the variable strNewFile will now look like this:<\/P><PRE class=\"codeSample\">[OEM Install]\nDisplayWelcomeDlg=NO\n<\/PRE>\n<P>See how this works? We\u2019ll keep reading through the .INI file line-by-line, and we\u2019ll keep constructing the variable strNewFile line-by-line as well. By the time the script is finished, strNewFile will contain this information:<\/P><PRE class=\"codeSample\">[OEM Install]\nDisplayWelcomeDlg=NO\nDisplayEULA=NO\nDisplayTypeOfInstallDlg=NO\nDisplaySelectDestDirDlg=YES\nDisplayCustomDlg=NO\nDisplayUserInfoDlg=NO\nDisplayConfirmRegDlg=NO\nDisplayStartCopyDlg=NO\nDisplayFinishDlg=NO\nDisplayFinalMessage=YES\nDisplayRebootDlg=YES\nProgGroupName=\nDefaultDestDir=\nUserName=Ken Myer\nUserCompanyName=\nUserSerialNumber=\n<\/PRE>\n<P>And, yes, that <I>is<\/I> exactly the same information we want in our modified .INI file.<\/P>\n<P>We\u2019re on our way now. We next close the file sample.ini, then immediately reopen it <I>for writing<\/I> (silly, but that\u2019s what we have to do). We then use this single line of code to replace the existing text in sample.ini with the information stored in the variable strNewFile:<\/P><PRE class=\"codeSample\">objTextFile.WriteLine strNewFile\n<\/PRE>\n<P>When we close the file again, the data will be saved, and we will have successfully modified our .INI file.<\/P>\n<P>Now, we don\u2019t claim this is the coolest piece of code ever written; it\u2019s not. And there might very well be .INI files where this doesn\u2019t work. But the truth is, it\u2019s probably the best you\u2019re going to be able to do, at least with the tools built into the operating system.<\/P><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\/nov04\/hey1130.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\/nov04\/hey1130.mspx#top\">Top of page<\/A><\/TD><\/TR><\/TBODY><\/TABLE><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! Can I edit.INI files using a script?&#8212; MZ Hey, MZ. Yes, you can edit .INI files (or any kind of text files, for that matter) using a script. The method you have to use might not be the most elegant process in the world, but it\u2019ll work. And that\u2019s all we really [&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":[40,3,4,14,5],"class_list":["post-70913","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-filesystemobject","tag-scripting-guy","tag-scripting-techniques","tag-text-files","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! Can I edit.INI files using a script?&#8212; MZ Hey, MZ. Yes, you can edit .INI files (or any kind of text files, for that matter) using a script. The method you have to use might not be the most elegant process in the world, but it\u2019ll work. And that\u2019s all we really [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70913","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=70913"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70913\/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=70913"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70913"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70913"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}