{"id":71193,"date":"2004-10-19T17:27:00","date_gmt":"2004-10-19T17:27:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2004\/10\/19\/how-can-i-change-a-read-only-file-to-a-read-write-file\/"},"modified":"2004-10-19T17:27:00","modified_gmt":"2004-10-19T17:27:00","slug":"how-can-i-change-a-read-only-file-to-a-read-write-file","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-change-a-read-only-file-to-a-read-write-file\/","title":{"rendered":"How Can I Change a Read-only File to a Read-write 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! How can I change a read-only file to a read-write file?<BR><BR>&#8212; WR<\/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, WR. We don\u2019t mean to shock you, but how can you change a read-only file to a read-write file? Why, by using a script, of course!<\/P>\n<P>We know: you never saw that coming, did you?<\/P>\n<P>As a matter of fact, you can use <I>this<\/I> script, which changes the file C:\\Scripts\\Test.vbs from a read-only file to a read-write file:<\/P><PRE class=\"codeSample\">Const ReadOnly = 1<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objFile = objFSO.GetFile(&#8220;C:\\Scripts\\Test.vbs&#8221;)<\/p>\n<p>If objFile.Attributes AND ReadOnly Then\n    objFile.Attributes = objFile.Attributes XOR ReadOnly\nEnd If\n<\/PRE>\n<P>You\u2019re right: maybe we <I>should<\/I> explain what\u2019s going on here. We begin by setting the value of the constant ReadOnly to 1 (more on this monetarily). We then create an instance of the FileSystemObject, and use the GetFile method to bind to the file C:\\Scripts\\Test.vbs. So far, so good.<\/P>\n<P>This is where it starts to get weird, or at least a little unusual. The Read-only attribute is stored as part of a \u201cbitmask\u201d along with other file attributes such as Hidden (value of 2, which indicates whether the file is a hidden file) and System (value of 4, which indicates whether the file is a system file). In a bitmask (loosely defined as a collection of attributes stored as a single value) individual attributes can be likened to switches, switches that can be either on or off. If the switch with value of 1 is on, then the file is read-only; if the switch with the value of 1 is off, then the file is read-write.<\/P>\n<P>That just leaves one question: how the heck do you determine whether one of these switches is on or off? A complete explanation of that is well beyond the scope of this column, but the simple answer is that you use the bitwise AND operator. Notice the following odd-looking line in the script:<\/P><PRE class=\"codeSample\">If objFile.Attributes AND ReadOnly Then\n<\/PRE>\n<P>Believe it or not, this line is checking to see if the ReadOnly attribute (with a value of 1) is on. What if we wanted to see if the file was a hidden file? Well, in that case (and assuming we set the value of a constant named Hidden to 2), we\u2019d use this line of code:<\/P><PRE class=\"codeSample\">If objFile.Attributes AND Hidden Then\n<\/PRE>\n<P>Basically the AND operator can be read like this: \u201cIf we\u2019re looking at the Attributes of the file and of the ReadOnly switch is on, then this is a read-only file and this If statement is True. If the ReadOnly switch is off, then this If statement is False.\u201d Experienced scripters might cringe a bit at that explanation, but that gives you an idea of what\u2019s going on here.<\/P>\n<P>So why <I>are<\/I> we checking to see if the file is read-only? Well, momentarily, we\u2019re going to use the XOR operator to \u201cflip\u201d the switch; that\u2019s what this line of code is for:<\/P><PRE class=\"codeSample\">objFile.Attributes = objFile.Attributes XOR ReadOnly\n<\/PRE>\n<P>In this example, XOR simply toggles the file from one state to the other. If the file is read-only, XOR switches it to read-write; if it\u2019s read-write, XOR switches it to read-only. That\u2019s why we first check to see if the file is <I>already<\/I> read-only. If it is, we want to flip the switch and make it read-write. If it\u2019s already read-write, though, we obviously don\u2019t want to flip the switch; after all, that would make the file read-only. (Remember, we\u2019re just toggling from one state to the other.)<\/P>\n<P>You\u2019re right: this bitwise stuff is confusing, so we owe you a treat. Therefore, here\u2019s a script which binds to the folder C:\\Scripts and turns all the read-only files in that folder into read-write files. We won\u2019t explain the details behind how the script gets back a list of all the files in a folder; we\u2019ll save that for tomorrow\u2019s column. (A sneaky way of making you come back tomorrow!) For now, though, here\u2019s your treat:<\/P><PRE class=\"codeSample\">Const ReadOnly = 1<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objFolder = objFSO.GetFolder(&#8220;C:\\Scripts&#8221;)\nSet colFiles = objFolder.Files<\/p>\n<p>For Each objFile in colFiles\n    If objFile.Attributes AND ReadOnly Then\n        objFile.Attributes = objFile.Attributes XOR ReadOnly\n    End If\nNext\n<\/PRE>\n<P>See you tomorrow, huh?<\/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\/oct04\/hey1019.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\/oct04\/hey1019.mspx#top\">Top of page<\/A><\/TD><\/TR><\/TBODY><\/TABLE><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I change a read-only file to a read-write file?&#8212; WR Hey, WR. We don\u2019t mean to shock you, but how can you change a read-only file to a read-write file? Why, by using a script, of course! We know: you never saw that coming, did you? As a matter of [&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,3,12,5],"class_list":["post-71193","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files","tag-scripting-guy","tag-storage","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I change a read-only file to a read-write file?&#8212; WR Hey, WR. We don\u2019t mean to shock you, but how can you change a read-only file to a read-write file? Why, by using a script, of course! We know: you never saw that coming, did you? As a matter of [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71193","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=71193"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71193\/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=71193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=71193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=71193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}