Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I am attempting to create a batch file to search for a specific file on my computer and delete it.
I have been trying to figure it out and cant get it.
Thank You for any help!

This batch Will Search for the specified file on the hard disk and delete when found
Remove "echo" from |echo del "%%f" /f| to make it delete the file
:: --- BATCH SCRIPT START ---
:bof
@echo off & setlocal enableextensions enabledelayedexpansion:init
:: Configure the file name supplied
if "%1"=="" (
echo Please specify the name of the file to search and delete
goto :eof
) else (
set file=%*
set ffile=!file:\=!
set file=!ffile::=!
if not "!file!"=="%*" (
echo supply file's name only, not the path
goto :eof
)
):: Determine available fixed drives
set drives=
if exist "C:\Drives.txt" del "C:\Drives.txt"
for /f "usebackq tokens=1*" %%a in (`fsutil fsinfo drives ^| find ":"`) do (
if /i "%%a" NEQ "Drives:" (
set "drives=!drives! %%a"
echo:%%a >> C:\Drives.txt
) ELSE (
set "drives=!drives! %%b"
echo:%%b >> C:\Drives.txt
)
)
for /f "tokens=*" %%d in ('type "C:\Drives.txt"') do (
fsutil fsinfo drivetype %%d | find /i "Fixed" >nul
if not errorlevel 1 (
echo searching for "%file%" on drive "%%d" ...
call :SEARCHIT %%d
)
):: Search and delete the file if found
:SEARCHIT
pushd %*\
for /f "tokens=*" %%f in ('dir "%file%" /b /s /a-d 2^>nul') do (
echo del "%%f" /f
if not errorlevel 1 (
echo "%%f" deleted
) else (
echo unable to delete "%%f"
)
)
goto :eof:eof
:: --- BATCH SCRIPT END ---

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |