Computing.Net > Forums > Programming > Batch File search and delete file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch File search and delete file

Reply to Message Icon

Name: Zach (by Vertigoxat)
Date: April 10, 2008 at 13:58:37 Pacific
OS: ALL
CPU/Ram: ...
Product: ...
Comment:

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!



Sponsored Link
Ads by Google

Response Number 1
Name: devil_himself
Date: April 10, 2008 at 20:21:26 Pacific
Reply:

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 ---


0

Response Number 2
Name: devil_himself
Date: April 10, 2008 at 21:04:46 Pacific
Reply:

Usage --> Batchname Filename
Ex --> HD.Bat Searchdel.txt


0

Response Number 3
Name: klint
Date: April 11, 2008 at 01:52:14 Pacific
Reply:

DEL /S /P FILENAME


0

Response Number 4
Name: Zach (by Vertigoxat)
Date: April 11, 2008 at 13:12:43 Pacific
Reply:

Thank You guys works great!!


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch File search and delete file

Batch to search and copy to new Dir www.computing.net/answers/programming/batch-to-search-and-copy-to-new-dir/16809.html

Batch File to clean folders www.computing.net/answers/programming/batch-file-to-clean-folders/11862.html

Batch file , need help urgently www.computing.net/answers/programming/batch-file-need-help-urgently/16211.html