Marcus Jones
Marcus Jones

Reputation: 41

Batch script to search and delete a specific filename

I'll explain exactly my situation. I need a batch file that searches C:/ for 'Minecraft.exe' and deletes it if it exists.

I don't know enough batch to be able to do this on my own and I've looked everywhere and all I can find is partial solutions.

Upvotes: 4

Views: 21293

Answers (3)

techrat
techrat

Reputation: 1

This is probably way too late but:

For /F "tokens=*" %%I in ('Dir c:\Minecraft.exe /s /b') do set FOUND="%%~fI"
del %FOUND%

The first line searches the whole C: drive. The second line deletes the file.

Upvotes: 0

Magoo
Magoo

Reputation: 80213

Well - good idea to do this first:

DIR /s c:\minecraft.exe

But that's just looking for it.

Be VERY, VERY careful with this command - make an error and you could have real trouble.

del /p /s c:\minecraft.exe

will delete all minecraft.exe (but prompt first)

Omit the /p and it'll delete without prompting

Add /f and it'll ALSO delete any READ-ONLY versions it finds.

Just be careful, 'k?

Upvotes: 4

Endoro
Endoro

Reputation: 37589

Try this (it doesn't delete anything before you remove the echo):

@echo off &setlocal 
for /f "delims=" %%i in ('dir /a-d /s /b c:\minecraft.exe') do echo del "%%~i"
endlocal

Upvotes: 0

Related Questions