Share via

How to create a BAT file that finds and deletes all files with the same extension

Anonymous
2025-02-12T15:50:30+00:00

How to create a BAT file (when is possible) that finds and deletes all files with the same extension (in actual directory and all subdirectories)?

Example: find and delete all BAK files

Windows for home | Windows 10 | Files, folders, and storage

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
Answer accepted by question author
  1. Anonymous
    2025-02-13T12:07:05+00:00

    Dear MilanSalka

    To create a BAT file that recursively searches for and deletes all files with a specific extension (e.g., .bak) in a directory and its subdirectories, follow the steps below:

    Step 1: Create the delete_files.bat file

    1. Open Notepad or any text editor.
    2. Copy the following code into the editor:

    ==========================================================================

    @echo off 

    :: Ensure the file extension is provided as an argument 

    if "%1"=="" ( 

        echo Please provide the file extension to delete (e.g., .bak) 

        echo Usage: delete_files.bat .extension 

        pause 

        exit /b 

    :: Search and delete files 

    echo Searching and deleting all files with the extension %1 ... 

    for /r %%i in (*%1) do ( 

        echo Deleting: %%i 

        del /f /q "%%i" 

    echo Done! 

    pause 

    ===========================================================================

    1. %1: Refers to the file extension passed as an argument (e.g., .bak).
    2. for /r: Recursively searches through the current directory and all subdirectories.
    3. del /f /q: Deletes files forcefully without prompting ("/f" is for force, "/q" is quiet mode).
    4. Save the file:
      • Click on File > Save As.
      • Choose a location (e.g., Desktop), and name it delete_files.bat.
      • Ensure the file type is set to All Files (*.*) and save it with the .bat extension.

    ===========================================================================

    Step 2: Run the BAT File

    1. Navigate to the directory where you saved the delete_files.bat file.
    2. Open a command prompt in this directory:
      • Right-click inside the folder, then select Open in Terminal (or equivalent option like Open Command Window Here).
    3. Run the batch file with the extension you want to delete:

    delete_files.bat .bak 

    1. Replace .bak with the desired file extension (e.g., .tmp).
    2. The batch file will:
      • Search for all files with the specified extension in the current directory and subdirectories.
      • Delete them.

    Display a list of deleted files.

    Best Wish

    Shawn.Z-MSFT | Microsoft Community Support Specialist

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful