Wednesday, July 25, 2007

How to fix Vista driver cache

I've installed Vista on 3 machines now, and on all 3 something I've done has caused the Vista driver cache to become corrupt. So, when plugging in a USB device which it should recognize automatically, it instead says "Driver not found" and gives me options to repair it. While you can tell it to browse for a location manually, and choose c:\windows to get around this, I have created a batch file which will allow you to force Vista to rebuild the driver cache. You have to be an administrator on your system to use it, and it works with UAC enabled.

Create a batch file with the following content and execute it as an administrator (Right click it, choose Run as Administrator). It will create a directory labeled \windows\inf\infbk-XXXX (where the XXXX is replaced with a random number), set the relevant files to be owned by "Administrators", give "Administrators" full control over the files, and move them to the backup folder. After running this you should be able to just plug in a new device, although you might want to reboot first.

Here is the content of the batch file:
@echo off
TITLE Repairing Inf Cache
rem change to drive where windows runs
%SystemDrive%
rem change to the \windows\inf folder
cd %SystemRoot%\inf

rem create random directory for backup
set FOLDERNAME=infbk-%RANDOM%
mkdir %FOLDERNAME%

rem list of files which need to be removed
set FILE1=INFPUB.DAT
set FILE2=INFSTOR.DAT
set FILE3=INFSTRNG.DAT
set FILE4=INFCACHE.*
set FILE5=SETUPAPI.*

rem take ownership of the first file
takeown /f %FILE1%
rem grant full control to 'administrators'
icacls %FILE1% /grant administrators:F /t
rem move it to the backup folder
move %FILE1% %FOLDERNAME%

rem same for the other files
takeown /f %FILE2%
icacls %FILE2% /grant administrators:F /t
move %FILE2% %FOLDERNAME%

takeown /f %FILE3%
icacls %FILE3% /grant administrators:F /t
move %FILE3% %FOLDERNAME%

takeown /f %FILE4%
icacls %FILE4% /grant administrators:F /t
move %FILE4% %FOLDERNAME%

takeown /f %FILE5%
icacls %FILE5% /grant administrators:F /t
move %FILE5% %FOLDERNAME%

echo Successfully moved all infcache related files to %FOLDERNAME%
pause

Labels: