How do delete files with a certain name in a batch file

Iv'e got a directory %userprofile%\appdata\roaming\.minecraft\versions and in that directory is a whole heap of folders, I want a batch file to delete all the folder with forge anywhere in their name.

There are no sub files in these folders, but there are 2 files in each... I want anything in that directory with forge in its name deleted automatically with a batch file.

So delete all folders including forge in their name. I can't seem to do it.

I have tried googling stuff, but I can't remember everything I have tried... but here is some.

rmdir /s %userprofile%\appdata\roaming\.minecraft\versions
for /d /r . %d in (forge) do @if exist "%d" rd /s/q "%d"

Also, tried this (below) but it only listed the folders and files I want deleted.

dir "%userprofile%\appdata\roaming.minecraft\versions" /s /b /d | find "forge"

4

4 Answers

@echo off & set "_target=%userprofile%\appdata\roaming.minecraft\versions"
for /f tokens^=* %%i in ('dir /s/b/a:d "%_target%\*forge*"')do echo/RMDir /q/s "%%~i"
  • Or...
@set "_target=%userprofile%\appdata\roaming.minecraft\versions"
@for /f tokens^=* %%i in ('dir /s/b/a:d "%_target%\*forge*"')do @echo/RMDir /q/s "%%~i"

1) Set target path to a variable

set "_target=%userprofile%\appdata\roaming.minecraft\versions"

2) Use For /f with dir /s /b /a:d to list the desire folder:

for /f tokens^=* %%i in ('dir /s /b /a:d "!_target!\*forge*"')...

3) See the for loop command and echo output...

')do echo/RMDir /q/s "%%~i"

4) If previous output is ok, remove echo command to delete the folder and possible sub folder/files recursively...

echo/RMDir /q /s "%%~i"

For command line help, you can use /?:

Set /?, For /?, RMDir /?

On the internet, you can get more help on:

2

PowerShell:

With the -WhatIF parameter, you can safely do a "test run" to see what folders the command will delete. If the items listed are the ones you want deleted, remove the -WhatIf parameter.

$target = Join-Path $env:appdata '.minecraft\versions'
Get-ChildItem $Target -filter '*forge*'-Directory | Remove-Item -Recurse -Force -WhatIf

Join-PathGet-ChildItemRemove-ItemRecurse

5

When building this type of batch program, do yourself a favor and test that the files your parseing over correspond correctly with what your trying to achieve by replacing the end command with echo or a confirmation before running the final version

@Echo Off
PUSHD %userprofile%\appdata\roaming.minecraft\versions
For /R %%A in (*forge*) do ( IF "%%~xA" == "" ( Choice /N /C ny /M "Remove Folder? %%A" IF Errorlevel 2 (RMDIR "%%A") ) Else ( Del /P "%%A" )
)
POPD
Pause

If you want to find and delete the files what have "forge" in the name, just enter forge in the search bar, then CTRL + A to select all the files (with file names including forge) and hit delete

5

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like