It's amazing how many Windows problems are caused by a faulty registry, from Windows protection errors on startup to Windows hanging on shutdown. Severe problems resulting from severe registry damage may require a fresh install of Windows. But for most annoyances and anomalies caused by registry corruption, a quick rebuild will get you back to a smooth working system.
But what is the registry?
The registry is a database—an amalgam of two special files, SYSTEM.DAT and USER.DAT. These files are written to and edited much like any other database files, and just about any installation program will write to or edit them, although sometimes not as cleanly as we’d like. The "garbage in, garbage out" principle applies here, as well—except in the case of the registry, the "garbage out" seems to manifest itself as one or more Windows problems.
Third-party registry utilities
Before I begin describing the rebuilding process in detail, let me state that I know all about REGCLEAN and other Windows utilities that are supposed to cure registry ills. However, these programs work only if you can boot into Windows. Even then, Windows is using the very registry we're trying to clean. To me, this is like working on your car's engine while you're driving. Instead, we're going to clear things up another way. We're going to do it from DOS.
Warning: The following article involves editing your system registry. Using the Windows Registry Editor incorrectly can cause serious problems requiring the reinstallation of your operating system. TechRepublic does not and will not support problems that arise from editing your registry. Use the Registry Editor and the following directions at your own risk.
Using REGEDIT in DOS
The utility we're going to use is REGEDIT.EXE—the same REGEDIT that we use in Windows also runs as a DOS program. REGEDIT.EXE supports command line arguments that allow us to do a complete registry rebuild, while leaving the "dirt" and empty spaces behind. We'll eliminate the need to repetitively type commands by creating four batch files that you can carry with you and run from a floppy.
For the sake of simplicity, we'll assume that SYSTEM.DAT, USER.DAT, and REGEDIT.EXE reside in the C:\WINDOWS directory.
Step one: Remove the ReadOnly and Hidden attributes from SYSTEM.DAT and USER.DAT
The first step in rebuilding the Windows registry from DOS is to remove the ReadOnly, Hidden, and System attributes from the SYSTEM.DAT and USER.DAT files. A batch file that allows you to toggle the attributes off and on at will (REGATT.BAT) looks like this:
if not "%1"=="-" if not "%1"=="+" goto INSTRUCT
attrib %1r %1h %1s c:\windows\system.dat
attrib %1r %1h %1s c:\windows\user.dat
echo You must specify a - or + parameter, as in "%0 +" or "%0 -"
To use it, enter the command REGATT - or REGATT + to remove or add the file attributes, respectively.
Here's how REGATT.BAT works:
- @echo off: Prevents the lines that follow from being displayed on the screen while the commands are being executed. The "@" prevents "echo off" from displaying.
- if not "%1"=="-" if not "%1"=="+" goto INSTRUCT: This makes the batch file look for one parameter and limits the parameter choices to either "-" or "+." If neither is found, the script jumps to the INSTRUCT portion of the batch file. Note the use of the double "equals" signs (==).
- attrib %1r %1h %1s c:\windows\system.dat: Runs the "attrib" command on SYSTEM.DAT with either "-r -h -s" or "+r +h +s," depending on the parameter.
- attrib %1r %1h %1s c:\windows\user.dat: Runs the "attrib" command on USER.DAT the same as it does for SYSTEM.DAT.
- goto ENDIT: Jumps over the INSTRUCT statement since all went well.
- :INSTRUCT: Label that identifies this portion of the batch file.
- echo.–: Prints a blank line on the screen. Note that there is no space between "echo" and "."
- echo You must specify a - or + parameter, as in "%0 +" or "%0 -": Instructions for using the batch file. The %0 is a variable that is automatically replaced by DOS with the name of the batch file you typed on the command line. If you typed regatt in lower case, the line will read "You must specify a - or + parameter, as in regatt + or regatt -." If you change the name of the batch file to "wom.bat" and type WOM in upper case, it will read "You must specify a - or + parameter, as in WOM + or WOM -" without further editing. Neat, huh?
- :ENDIT: Label that identifies this portion of the batch file.
- echo.: Prints a blank line on the screen before returning to the prompt.
Step two: Create a backup of SYSTEM.DAT and USER.DATThe rebuilding process effectively destroys the current registry. If the rebuild fails (I've seen it happen when the DAT files are badly corrupted), there will be no registry. Having a corrupted registry to restore is better than having no registry at all.
To make a backup, we simply copy the "unattribbed" SYSTEM.DAT and USER.DAT files with REGBACK.BAT:
if "%1"=="" goto INSTRUCT
copy c:\windows\system.dat c:\windows\system.%1
copy c:\windows\user.dat c:\windows\user.%1
echo You must enter a 1 to 3 character file extension, as in "%0 sav"
Most of the lines in REGBACK.BAT are similar to those in REGATT.BAT. The three unique lines are:
- if "%1"=="" goto INSTRUCT: This jumps to the INSTRUCT section if no parameter is given after the "regback" command. Without a parameter, the value of %1 is null, so the statement translates to if ""=="" goto INSTRUCT and, since double-quotes indeed equal double-quotes, the script jumps to give the user instructions.
- copy c:\windows\system.dat c:\windows\system.%1: Copies SYSTEM.DAT to SYSTEM.parameter. Be sure to limit your parameter to three allowable DOS characters.
- copy c:\windows\user.dat c:\windows\user.%1: Copies USER.DAT to USER.parameter as above.
Step three: Rebuilding the registryDO NOT ATTEMPT TO RUN THESE COMMANDS UNLESS YOU HAVE MADE BACKUP COPIES OF SYSTEM.DAT AND USER.DAT! Remember, you will destroy the existing copy of the registry in the rebuilding stage. If the rebuild fails, so will you. Be sure you have your own backup.
REGREBLD.BAT looks like this:
echo Exporting registry contents. Please wait...
regedit /l:c:\windows\system.dat /r:c:\windows\
user.dat /e c:\windows\newreg.reg
echo Rebuilding the Windows registry. Do not interrupt!
regedit /l:c:\windows\system.dat /r:c:\windows\
user.dat /c c:\windows\newreg.reg
REGREBLD.BAT takes no parameters. Here's what the crucial lines do:
- echo Exporting registry contents. Please wait...: The REGEDIT "export" command displays no information while it's executing. This is a courtesy line to let you know that something is happening.
- regedit /l:c:\windows\system.dat /r:c:\windows\user.dat /e c:\windows\newreg.reg: Exports the contents of the current registry to a file we'll call "newreg.reg." The "/l:" and "/r:" switches point to the exact paths of SYSTEM.DAT and USER.DAT, respectively. The "/e" switch is for "export" and "c:\windows\newreg.reg" is the name of the target file that is created during the process.
- echo Rebuilding the Windows registry. Do not interrupt!: Another courtesy statement. Unlike the "export" command, the REGEDIT "create" command displays a progress counter. However, it doesn't state what it's creating, only that it’s importing.
- regedit /l:c:\windows\system.dat /r:c:\windows\user.dat /c c:\windows\newreg.reg: Creates a new registry from the contents of "c:\windows\newreg.reg." The key here is the "/c" switch, for "create." As soon as it is encountered, the current SYSTEM.DAT and USER.DAT are destroyed as new files are created from the data in newreg.reg. If this process is interrupted, the new registry will be incomplete and, therefore, useless.
- echo.: The progress counter that is displayed by REGEDIT does not have a carriage return. This statement forces one at the completion of the "create" process.
- del c:\windows\newreg.reg: Deletes the now unnecessary newreg.reg data file. You can remove this line if you want to look at the contents of newreg.reg before you delete it manually.
The full export/create routine can be quite time-consuming, depending on the size and state of the current registry. I've seen it take anywhere from five minutes to over an hour to rebuild the registry on desktop PCs. I don't recommend using it on laptops. If the rebuilding is successful (and most of the time it is), you won't need the next step.
Step four: Restoring a failed rebuild
Step four involves returning the registry to its previous state in the event a failed rebuild leaves you without working SYSTEM.DAT and USER.DAT files. We'll call this batch file REGRET.BAT. Remember the extension you used when creating your backups? You'll need it here:
if "%1"=="" goto INSTRUCT
if not exist c:\windows\system.%1 goto NOFILE
if not exist c:\windows\user.%1 goto NOFILE
attrib -r -h -s c:\windows\system.dat
attrib -r -h -s c:\windows\user.dat
copy c:\windows\system.%1 c:\windows\system.dat
copy c:\windows\user.%1 c:\windows\user.dat
echo Cannot locate one or more of your "%1" backup files!
echo Please verify your file extension and try again.
echo You must give a valid backup file extension, as in "%0 ext"
REGRET.BAT runs by entering “regret ext” at the prompt, where “ext” is the extension you used when creating your backups. If the ext files aren’t found, REGRET tells you. All of the REGRET commands are similar to ones we've used in the previous batch files. Note that after we delete the failed SYSTEM.DAT and USER.DAT files, we copy the backups to the DATs as opposed to renaming the backups. I never feel comfortable deleting critical backup files until I'm absolutely sure they won't be needed again. Delete them manually when you are comfortable.
Use what you’ve learned
Now that you have your batch files, go ahead and try them on a sick system. Boot the PC to a true DOS "Safe mode command prompt only" and run the files from a floppy. If you make the floppy bootable, be sure that you have an AUTOEXEC.BAT that contains a path statement pointing to C:\WINDOWS;C:\WINDOWS\COMMAND. A successful rebuilding of the registry will solve many of your "mysterious" Windows problems, including many Windows protection errors.
What do you think of the method for rebuilding a registry described here? Do you know of a better way to solve this problem? We want to know. Post a comment below or send us an e-mail.