Recipe 3.11. Making a Disk or Volume Read-Only
ProblemYou want to make a disk or volume read-only so that users or programs can't write to it. SolutionUsing a command-line interfaceThe Joeware (www.joeware.net) tool, writeprot, allows you to make a disk or volume read-only as long as no files are locked on that volume. The following command lists the read/write state of all disks and volumes on a system: > writeprot /mview The following command attempts to make the D: drive read-only: > writeprot /vol d: /ro The following command makes the D: drive read-write: > writeprot /vol d: /rw The following command attempts to make all volumes on the basic disk represented by the D: drive read-only: > writeprot /vol d: /ro Using VBScript' This code makes a volume or disk read-only (if possible) ' ------ SCRIPT CONFIGURATION ------ strDrive = "<Drive>" ' e.g., e: ' This assumes writeprot is in your PATH, if not, fully qualify ' the path to the command (e.g., c:\bin\writeprot.exe) strCommand = "writeprot /vol " & strDrive & " /ro" ' ------ END CONFIGURATION --------- set objWshShell = WScript.CreateObject("WScript.Shell") intRC = objWshShell.Run(strCommand, 0, TRUE) if intRC <> 0 then WScript.Echo "Error returned from running the command: " & intRC WScript.Echo "Command attempted: " & strCommand else WScript.Echo "Command executed successfully." end if DiscussionVolumes on basic disks and dynamic disks are treated different when it comes to enabling write protection. You can make individual volumes on a dynamic disk read-only, but for basic disks, you have to write protect the entire disk including all volumes. If you attempt to write protect a single volume on a basic disk with multiple volumes using writeprot, an error will be returned. You'd need to run the same command with the -unsafe option to write protect all volumes on the basic disk. You cannot write protect any volume that has files locked for read or write access. Since files are always opened for reading on the system volume, you will not be able to write protect that volume. See Recipe 4.19 for more information on finding open files.
See AlsoRecipe 4.12 and Recipe 4.18 |