3.11. Making a Disk or Volume Read-Only

Tip

This functionality is new to Windows XP and Windows Server 2003 and is not available on Windows 2000.

Problem

You want to make a disk or volume read-only so that users or programs can't write to it.

Solution

Using a command-line interface

The Joeware (http://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

Discussion

Volumes on basic disks and dynamic ...

Get Windows Server Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.