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 :DeleteFileSub %%x
goto endScript
:DeleteFileSub
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 🙂
One more thing…Subscribe to my newsletter and get 11 free network administrator tools, plus a 30 page user guide so you can get the most out of them. Click Here to get your free tools
{ 3 comments… read them below or add one }
Nice script.
I took the liberty of modifying it a little, it will display the drive currently being scanned, notify you when the drive is done and also to enter the file name to delete when running the script.
e.g. If you saved the script as EraseFiles.bat and you want to delete all the LOG files on your system just type: EraseFiles *.log
==== BEGIN OF SCRIPT ====
@echo off
cls
set FileToDelete=%1
for /f “tokens=1” %%x in (‘reg query HKLM\SYSTEM\MountedDevices^|find /i “\DosDevices\”‘) do call :delSub %%x
goto end
:delSub
set LocalDrive=%1
set LocalDrive=%LocalDrive:~-2%
echo [ Deleting files on drive %LocalDrive% ]
echo.
for /f “tokens=1 delims=*” %%z in (‘dir “%LocalDrive%\%FileToDelete%” /s /b’) do echo del “%%z”
goto end
:end
echo.
echo [ Finished scanning drive: %Localdrive% ]
echo.
:done
==== END OF SCRIPT ====
Nice script.
I took the liberty of modifying it a little, it will display the drive currently being scanned, notify you when the drive is done and also to enter the file name to delete when running the script.
e.g. If you saved the script as EraseFiles.bat and you want to delete all the LOG files on your system just type: EraseFiles *.log
You MUST enter a file name, the script’s has a very limited error control.
==== BEGIN OF SCRIPT ====
@echo off
cls
if “%1” == “” goto Error
set FileToDelete=%1
for /f “tokens=1” %%x in (‘reg query HKLM\SYSTEM\MountedDevices^|find /i “\DosDevices\”‘) do call :delSub %%x
goto end
:delSub
set LocalDrive=%1
set LocalDrive=%LocalDrive:~-2%
echo [ Deleting files on drive %LocalDrive% ]
echo.
for /f “tokens=1 delims=*” %%z in (‘dir “%LocalDrive%\%FileToDelete%” /s /b’) do echo del “%%z”
goto end
:end
echo.
echo [ Finished scanning drive: %Localdrive% ]
echo.
:done
:Error
echo **** Please enter a File name/No files found
echo.
==== END OF SCRIPT ====
Cool. Thanks for the updated script.