I distributed a test version of Remote Control 4.0 across our network last week, and since it was a special debug version, it created a large number of files named debuglog.dat.
I wanted an easy way to find and delete these using a batch file. I researched and was able to take parts of other scripts to build what I needed.
The first step was to identify all of the local drives on the system. I know that this is available in the registry key HKEY_LOCAL_MACHINE\System\MountedDrives
Using the reg command, I can get this by calling:
reg query HKLM\SYSTEM\MountedDevices
And using the find command I can pull out only the devices with dos drive letters:
reg query HKLM\SYSTEM\MountedDevices^|find /i “\DosDevices\”
Still, this is not enough for our task. Here is what we get when calling the script:

Lots of binary data we do not need
We can take this output, and feed it through the “for” command, and call a function for each drive letter:
for /f “tokens=1″ %%x in (‘reg query HKLM\SYSTEM\MountedDevices^|find /i “\DosDevices\”‘) do echo %%x
Now, we get a list of the drives, minus the binary data. We can use that to call a function that will parse out the \DosDevices\
for /f %34tokens=1%34 %%x in (‘reg query HKLM\SYSTEM\MountedDevices^%7cfind /i %34\DosDevices\%34′) do call
eleteFileSub %%x
goto endScript
eleteFileSub
set LocalDrive=%1
set LocalDrive=%LocalDrive:~-2%
echo %Drive%
goto endScript
:endScript
With that script, we get a complete list of local drives. Hmm…starting to look good:

Now. What next? Well, we want to search each local drive for our file, and then delete it. For safety reasons, I am only going to echo the file names – I want you to think real hard before doing this..since the wrong move and you could wipe out important files.
Again, the ‘for’ command is a great way to do this:
for /f “tokens=1 delims=*” %%z in (‘dir “LocalDrive%\SomeFile.txt” /s /b’) do echo del “%%z”
Replace SomeFile.txt with the file you want to remove.
This will pull out the filename in a full directory search of the specified drive and echo it. If you want to actually delete the file you will need to remove the “echo”:
for /f “tokens=1 delims=*” %%z in (‘dir “LocalDrive%\SomeFile.txt” /s /b’) do echo del “%%z”
Now, putting the whole thing together:
@echo off
for /f “tokens=1″ %%x in (‘reg query HKLM\SYSTEM\MountedDevices^|find /i “\DosDevices\”‘) do call :deleteSub %%x
goto endSub
:deleteSUb
set LocalDrive=%1
set LocalDrive=%LocalDrive:~-2%
for /f “tokens=1 delims=*” %%z in (‘dir “%Drive%\Some File.txt” /s /b’) do echo del “%%z”
goto endSub
:endSub
Text version here if you have copy-paste issues
Make sure you do lots of testing before you pull the trigger and remove the “echo” statement. I wouldn’t want you to blow away the wrong files
Recent Comments