Search code examples
batch-filecmddel

Deleting multiple files with different extensions using TXT list


i need to delete multiple files with the same name but different extensions from a list that already has an extension type.

i have a batch program to find strings in a files and to make a results text

    @echo off
findstr /m "1090 FF" *.info > results.txt
if %errorlevel%==0 (
echo Found! logged files into results.txt
) else (
echo No matches found
)

this creates a list of files with the extension .info. i then need to take that list and delete all the files with the list of files but i need to delete all the sibling files with it.

example:
12345.info (in my list to del)
12345.cpr (need to del)
12345.emf (need to del)
12345.obj (need to del)
12345.timb (need to del)

i tried using this but it only deleted the .info file extension:

@echo off
REM Delete files/folders specified in a newline delimited txt file list. 

set "default_list_path=C:\Users\%USERNAME%\Desktop\SAD_FF bat\results.txt"

if not "%~2"=="" echo Error: unexpected arguments& exit /b
if not "%~1"=="" ( set "list=%1" ) else (
    set "list=%DEFAULT_LIST_PATH%"
)

if not exist "%LIST:"=%" echo Error: list could not be found& exit /b

set /a delete_counter=0
for /f "delims=" %%I in ('type "%LIST:"=%"') do (
    if exist "%%~fI" (
        set /a delete_counter+=1
        if exist "%%~fI"\* (
            rd /s /q "%%~fI"
        ) else (
            del /q "%%~fI.*"
        )
    ) else (
        echo No such path "%%~I".
    )
)

echo.& echo %DELETE_COUNTER% files or folders were deleted.

can i augment the later code to del all of the sibling files?? all suggestions welcomed!!


Solution

  • Try changing

            del /q "%%~fI.*"
    

    to

            echo del /q "%%~dpnI.*"
    

    The required DEL commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO DEL to DEL to actually delete the files.