You Are Working at the Command Line and Want to Add the Read-only
When you need to ensure a file isn't changed nether any circumstances in Windows, it'south time to make information technology read-only. Setting a file as read-only or write-protecting a file prevents any changes whatsoever. If yous don't know how to set files as read-only and demand to, you're in the right place!
In this tutorial, y'all're going to larn how to set and remove the read-simply attribute in File Explorer, the command-line (cmd), and via PowerShell!
Permit'due south become going.
Prerequisites
This post volition be a hands-on tutorial. If you'd like to follow along, be sure you accept the following:
- A Windows PC. This tutorial will use Windows 10 and Windows PowerShell v5.1.
- A file to toggle the read-only attribute on. This tutorial will use a file located at C:\Shared\readme.txt.
How to Make a File Read-Only (and Writable) in File Explorer
In that location are a few different ways to fix a file as read-only but permit's showtime out this tutorial by showtime covering how to make it happen via Windows File Explorer.
The read-merely attribute is stored every bit a metadata property rather than in the content of the file. The belongings tin can be seen through Windows File Explorer.
Assuming that you lot're on the desktop of your Windows PC:
i. Open Windows File Explorer and navigate to the folder that stores the file you'll be working with (C:\Shared) in this instance.
two. Correct click the file and click Properties to open up the file'due south Properties box. This box contains various metadata most that particular file such as size, when information technology was created, modified, etc. It also has file attributes.
Click the read-just checkbox in the Attributes group to write-protect the file or prepare it as read-but, click Apply to assign the read-only attribute, and OK to close the dialog box.
Similarly, to remove the read-only aspect from the file, uncheck the Read-only checkbox.
How to Brand a File Read-Simply (and Writable) via Control Line
File attributes aren't just exposed via File Explorer. Y'all can also dispense file attributes including the read-only aspect via the command line. To do so, y'all must utilise a command called attrib.
1. Open up upwards a command prompt (cmd.exe).
2. Navigate to the folder the file is in.
3. Run attrib with no parameters. The attrib control volition immediately look for all files in the working directory and return all files and the enabled file attributes. You can see below the readme.txt file does not have the read-just aspect enabled.
iv. Now, run attrib to assign the read-just attribute. The +r parameter is key here. This parameter tells Windows to set the read-but aspect on the file.
5. Run attrib again with no parameters to verify the read-only attribute was practical. You can at present see in the screenshot below the R label shows upwardly indicating the read-only attribute has been practical write-protecting the file.
Similarly, to remove the read-only aspect utilise the
-rparameter.
How to Make a File Read-Simply (and Writable) with PowerShell
If you're not a fan of the GUI or the command prompt or want to use have a little more ease of control, wait no farther than PowerShell! Permit's now acquire how to brand a file read-only with PowerShell!
- Open up Windows PowerShell.
2. Run the Go-Item cmdlet providing the path to the file you're toggling the read-simply attribute on. The Attributes belongings returned contains each file aspect that'south currently enabled.
get-item -Path "c:\shared\readme.txt" | format-tabular array name, attributes
Using the
Format-Tablecmdlet is non necessary just by doing so, tells PowerShell to display only the 2 backdrop you lot're interested in in an easy-to-view format (Proper nameandAttributes)
3. Assign the read-merely aspect by adding the ReadOnly aspect to the Attributes property as shown below.
## Capture the file object in the $file variable $file = Go-Item -Path "c:\shared\readme.txt" ## Not to overwrite the existing attributes, create a new array containing ## the current file attributes while calculation the ReadOnly aspect and assign ## that array equally the new value of the Attributes property $file.Attributes = @($file.Attributes,"ReadOnly")
four. Now, view the Attributes property of the $file object and you'll see that yous've now applied the ReadOnly aspect to the file while maintaining any existing attributes that existed on the file beforehand. You can besides run Get-Item again to view the same results.
To remove the ReadOnly attribute from the file, follow the exact same approach just this time ready the value of the Attributes belongings to Normal e.g. $file.Attributes = "Normal".
Conclusion
You should at present know how to ready a file as read-just and vice versa. Setting a file as read-only is meant to forestall accidental changes. Only know that, setting a file equally read-only is not necessarily a security measure and does non forbid access to the file or someone from simply changing the attribute themselves.
Armed with this new noesis of protecting files from being accidentally overwritten, do you have any files you'd similar to protect?
hamiltonainal1983.blogspot.com
Source: https://adamtheautomator.com/how-to-make-a-file-read-only/
0 Response to "You Are Working at the Command Line and Want to Add the Read-only"
Postar um comentário