{"id":70633,"date":"2005-01-18T09:36:00","date_gmt":"2005-01-18T09:36:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/01\/18\/how-can-i-make-a-read-only-file-writeable\/"},"modified":"2005-01-18T09:36:00","modified_gmt":"2005-01-18T09:36:00","slug":"how-can-i-make-a-read-only-file-writeable","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-make-a-read-only-file-writeable\/","title":{"rendered":"How Can I Make a Read-Only File Writeable?"},"content":{"rendered":"<p><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\"> \n<P>Hey, Scripting Guy! Is it possible to use a script to change the read-only attribute of a file?<BR><BR>&#8212; ZO<\/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, ZO. Of course, you can do this; the FileSystemObject can do <I>anything<\/I>!<\/P>\n<P>Well, OK, it can\u2019t do either of those things. And, no, it can\u2019t do that, either. But it <I>can<\/I> change a read-only file to a writeable file, and with just a few lines of code.<\/P>\n<P>Here\u2019s a script that binds to the file C:\\Scripts\\Computers.txt, checks to see if the file is read-only and, if it is, makes it writeable:<\/P><PRE class=\"codeSample\">Const READ_ONLY = 1<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objFile = objFSO.GetFile(&#8220;C:\\Scripts\\Computers.txt&#8221;)<\/p>\n<p>If objFile.Attributes AND READ_ONLY Then\n    objFile.Attributes = objFile.Attributes XOR READ_ONLY\nEnd If\n<\/PRE>\n<P>Pretty easy, huh? The only tricky part comes with this line of code, when we check to see if the file is read-only:<\/P><PRE class=\"codeSample\">If objFile.Attributes AND READ_ONLY Then\n<\/PRE>\n<P>The way we determine whether or not a file is read-only is by checking the appropriate switch in the Attributes bitmask. If this switch (which has the value of 1; that\u2019s why we created a constant named READ_ONLY and assigned it the value 1) is enabled, then the file is read-only. If the switch is not enabled then the file is writeable. <\/P>\n<P>This line of code is a bit confusing (welcome to the world of Boolean logic!) but it can be read like this: if the read-only switch within the Attributes bitmask is enabled, then run the next line of code. The syntax is kind of clumsy-looking, but it\u2019s just something we have to live with. <\/P>\n<P>And before you ask, what if you wanted to turn a writeable file into a read-only file? Well, in that case, we\u2019d want to determine if the read-only switch is <I>not<\/I> set. Thus we\u2019d use this line of code instead:<\/P><PRE class=\"codeSample\">If Not objFile.Attributes AND READ_ONLY Then\n<\/PRE>\n<P>If it turns out that we have a read-only file, the script then uses the Boolean operator XOR to \u201cflip\u201d the read-only switch. This line of code executes only if the read-only switch is enabled; consequently, it\u2019s going to turn this switch off, thus making the file writeable. The XOR operator just flips things from on to off and vice-versa. That\u2019s why we check to see if the read-only switch is enabled before we call XOR. Suppose we didn\u2019t check and suppose the file was writeable. In that case, XOR would flip the switch, and make our writeable file read-only.<\/P>\n<P>Yes, it\u2019s confusing. For a very brief primer on bitmask properties and Boolean logic, you might take a look at this portion of the <A href=\"http:\/\/null\/technet\/scriptcenter\/guide\/sas_scr_tspz.mspx\" target=\"_blank\"><B>Microsoft Windows 2000 Scripting Guide<\/B><\/A>.<\/P>\n<P>Of course, instead of simply changing one read-only file to a writeable file, a more common task is to go through a folder and make <I>all<\/I> the read-only files writeable. Can a script do that? You bet it can. Here\u2019s a script that uses WMI to retrieve a list of all the read-only files found in the folder C:\\Scripts (by checking for files where the Writeable property is FALSE). The script then takes each of those files, passes the file name to the FileSystemObject, and proceeds to make the file writeable. We won\u2019t explain how all the WMI code works today; if you\u2019re interested in that, you might take a look at this <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/qanda\/jan05\/hey0114.mspx\"><B>Hey, Scripting Guy!<\/B><\/A> column.<\/P>\n<P>Oh, right; here\u2019s the script: <\/P><PRE class=\"codeSample\">Const READ_ONLY = 1<\/p>\n<p>strComputer = &#8220;.&#8221;\nSet objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colFiles = objWMIService. _\n    ExecQuery(&#8220;Select * from CIM_DataFile where Path = &#8216;\\\\Scripts\\\\&#8217; &#8221; &amp; _\n        &#8220;AND Drive = &#8216;C:&#8217; AND Writeable = FALSE&#8221;)<\/p>\n<p>For Each objFile in colFiles\n    Set objReadOnlyFile = objFSO.GetFile(objFile.Name)\n    objReadOnlyFile.Attributes = objReadOnlyFile.Attributes XOR READ_ONLY\nNext<\/PRE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! Is it possible to use a script to change the read-only attribute of a file?&#8212; ZO Hey, ZO. Of course, you can do this; the FileSystemObject can do anything! Well, OK, it can\u2019t do either of those things. And, no, it can\u2019t do that, either. But it can change a read-only file [&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-70633","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! Is it possible to use a script to change the read-only attribute of a file?&#8212; ZO Hey, ZO. Of course, you can do this; the FileSystemObject can do anything! Well, OK, it can\u2019t do either of those things. And, no, it can\u2019t do that, either. But it can change a read-only file [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70633","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=70633"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70633\/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=70633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70633"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}