How to Delete a File in Microsoft Windows Using Batch Files

This wikiHow teaches you how to delete a file on your Windows computer by using a Batch file. Batch files are small files which can run commands via your computer's built-in Command Prompt program. Once you understand how to create a basic...

Part 1 of 5:

Finding the File's Information

  1. images 1 of How to Delete a File in Microsoft Windows Using Batch Files
    Images 1 of How to Delete a File in Microsoft Windows Using Batch Files
    Find the file you want to delete. Go to the location of the file that you want to remove via Batch file.
  2. images 2 of How to Delete a File in Microsoft Windows Using Batch Files
    Images 2 of How to Delete a File in Microsoft Windows Using Batch Files
    Right-click the file. Doing so prompts a drop-down menu.
  3. images 3 of How to Delete a File in Microsoft Windows Using Batch Files
    Images 3 of How to Delete a File in Microsoft Windows Using Batch Files
    Click Properties. It's at the bottom of the drop-down menu. This will open a pop-up window.
  4. images 4 of How to Delete a File in Microsoft Windows Using Batch Files
    Images 4 of How to Delete a File in Microsoft Windows Using Batch Files
    Determine the file's extension. In the "Type of file" section, look for the three- or four-letter extension in parentheses. This is the extension you'll need to add onto your file's name later.
    1. For example, if you have a text file, you'll see "Text Document (.txt)" to the right of the "Type of file" heading.
    2. If you see "File folder" listed as the file type, you'll have to close the Properties window, click once the file you want to check, and then right-click it again and click Properties.
  5. images 5 of How to Delete a File in Microsoft Windows Using Batch Files
    Images 5 of How to Delete a File in Microsoft Windows Using Batch Files
    Copy the file's location. Click and drag your mouse across the address listed in the "Location" section of the Properties window, then press Ctrl+C. This will add the address to your computer's clipboard. At this point, you're ready to begin writing the Batch file.
Part 2 of 5:

Writing the Batch File

  1. images 6 of How to Delete a File in Microsoft Windows Using Batch Files
    Images 6 of How to Delete a File in Microsoft Windows Using Batch Files
    Open Start
    images 7 of How to Delete a File in Microsoft Windows Using Batch Files
    Images 7 of How to Delete a File in Microsoft Windows Using Batch Files
    . Click the Windows logo in the bottom-left corner of the screen.
  2. images 8 of How to Delete a File in Microsoft Windows Using Batch Files
    Images 8 of How to Delete a File in Microsoft Windows Using Batch Files
    Open Notepad. Type in notepad, then click Notepad at the top of the Start window. The Notepad window will open.
  3. images 9 of How to Delete a File in Microsoft Windows Using Batch Files
    Images 9 of How to Delete a File in Microsoft Windows Using Batch Files
    Add the "Change Directory" command. Type in cd and press the spacebar, then paste in your copied address by pressing Ctrl+V and press Enter. This tells the command to look in the folder in which your file is stored.
    1. For example, if your file is on the desktop, your command might read cd C:UsersYourNameDesktop.
    2. If you don't do this step, the "Delete" command will look for your file in a system folder instead.
  4. images 10 of How to Delete a File in Microsoft Windows Using Batch Files
    Images 10 of How to Delete a File in Microsoft Windows Using Batch Files
    Enter the "Delete" command. On the line directly below the "Change Directory" command, type in del and press the spacebar.
  5. images 11 of How to Delete a File in Microsoft Windows Using Batch Files
    Images 11 of How to Delete a File in Microsoft Windows Using Batch Files
    Enter the file's name and extension in quotation marks. Type a quotation mark, type in the file's name exactly as it appears (including spaces, special characters, and capitalization), type in a period and the file's extension, and type the closing quotation mark.
    1. For a text file named "Bananas", for example, you would type in "Bananas.txt" as the file name and extension.
    2. At this point, the entire second line should read del "file name.extension" where "file name" is replaced by the file's name and ".extension" is replaced by the file's extension.
  6. images 12 of How to Delete a File in Microsoft Windows Using Batch Files
    Images 12 of How to Delete a File in Microsoft Windows Using Batch Files
    Add more file names. If you want to delete multiple files from a folder, press Enter to start a new line, then type in del and a space followed by the other file's name and extension; repeat this process as needed until all of the files you want to remove are named in the file on their own lines.
Part 3 of 5:

Saving and Executing the Batch File

  1. images 13 of How to Delete a File in Microsoft Windows Using Batch Files
    Images 13 of How to Delete a File in Microsoft Windows Using Batch Files
    Click File. It's in the top-left corner of the Notepad window. A drop-down menu will appear.
  2. images 14 of How to Delete a File in Microsoft Windows Using Batch Files
    Images 14 of How to Delete a File in Microsoft Windows Using Batch Files
    Click Save As…. This option is in the drop-down menu. Doing so opens the Save As window.
  3. images 15 of How to Delete a File in Microsoft Windows Using Batch Files
    Images 15 of How to Delete a File in Microsoft Windows Using Batch Files
    Enter a file name with the Batch extension. Type whatever you want to name your Batch file into the "File name" text box, then type in .bat at the end of the file's name.
    1. For example, to name your Batch file "fruit", you would type in fruit.bat here.
  4. images 16 of How to Delete a File in Microsoft Windows Using Batch Files
    Images 16 of How to Delete a File in Microsoft Windows Using Batch Files
    Click the "Save as type" drop-down box. You'll find this option near the bottom of the window. Clicking it prompts a drop-down menu.
  5. images 17 of How to Delete a File in Microsoft Windows Using Batch Files
    Images 17 of How to Delete a File in Microsoft Windows Using Batch Files
    Click All Files. It's in the drop-down menu.
  6. images 18 of How to Delete a File in Microsoft Windows Using Batch Files
    Images 18 of How to Delete a File in Microsoft Windows Using Batch Files
    Select a save location. Click a folder on the left side of the window (e.g., Desktop) to select that folder as the place in which to save your Batch file.
  7. images 19 of How to Delete a File in Microsoft Windows Using Batch Files
    Images 19 of How to Delete a File in Microsoft Windows Using Batch Files
    Click Save. It's in the bottom-right corner of the window. This will save your Batch file in the selected location.
  8. images 20 of How to Delete a File in Microsoft Windows Using Batch Files
    Images 20 of How to Delete a File in Microsoft Windows Using Batch Files
    Know that your Batch file will permanently delete your files. When you run your Batch file, the "del" command will tell your computer to remove permanently the named files (as opposed to moving them to the Recycle Bin). Because of this, you should be careful when executing your Batch file.
  9. images 21 of How to Delete a File in Microsoft Windows Using Batch Files
    Images 21 of How to Delete a File in Microsoft Windows Using Batch Files
    Run your Batch file. When you're ready to delete your named file(s), simply double-click the Batch file to run it. You should see the file(s) disappear after a moment.
Part 4 of 5:

Clearing a Folder of One File Type

  1. images 22 of How to Delete a File in Microsoft Windows Using Batch Files
    Images 22 of How to Delete a File in Microsoft Windows Using Batch Files
    Understand the purpose of this method. If you want to clear all files of a specific file type (e.g., text files) from a folder, you can use a Batch file. This is useful if you have a folder which automatically fills up with various files.
  2. images 23 of How to Delete a File in Microsoft Windows Using Batch Files
    Images 23 of How to Delete a File in Microsoft Windows Using Batch Files
    Open the folder you want to clear. Double-click the folder that you want to clear to open it.
  3. images 24 of How to Delete a File in Microsoft Windows Using Batch Files
    Images 24 of How to Delete a File in Microsoft Windows Using Batch Files
    Determine the extension for the files you want to delete. Do the following:
    1. Right-click the type of file you want to delete.
    2. Click Properties.
    3. Look at the extension in the "Type of file" section.
  4. images 25 of How to Delete a File in Microsoft Windows Using Batch Files
    Images 25 of How to Delete a File in Microsoft Windows Using Batch Files
    Copy the folder's address. Click the address bar at the top of the folder to highlight the address, then press Ctrl+C to copy it.
    1. If clicking the address bar doesn't highlight the address, click and drag across the address to highlight it.
  5. images 26 of How to Delete a File in Microsoft Windows Using Batch Files
    Images 26 of How to Delete a File in Microsoft Windows Using Batch Files
    Open Notepad. Click Start
    images 27 of How to Delete a File in Microsoft Windows Using Batch Files
    Images 27 of How to Delete a File in Microsoft Windows Using Batch Files
    , type in notepad, and click Notepad when it appears at the top of the Start menu.
  6. images 28 of How to Delete a File in Microsoft Windows Using Batch Files
    Images 28 of How to Delete a File in Microsoft Windows Using Batch Files
    Tell the file to select your folder's address. Type in cd and press the spacebar, then press Ctrl+V to paste in the folder's address.
  7. images 29 of How to Delete a File in Microsoft Windows Using Batch Files
    Images 29 of How to Delete a File in Microsoft Windows Using Batch Files
    Enter the "Delete" command. Press Enter to start a new line below the first one, then type in del and press the spacebar.
  8. images 30 of How to Delete a File in Microsoft Windows Using Batch Files
    Images 30 of How to Delete a File in Microsoft Windows Using Batch Files
    Tell the file to look for your preferred file type. Instead of typing in a specific file's name, you'll type in a quotation mark, an asterisk, the extension, and another quotation mark. This tells your computer to delete any files ending in your specified extension.
    1. For example, if you want to empty the folder of text files, your "Delete" line would say del "*.txt".
  9. images 31 of How to Delete a File in Microsoft Windows Using Batch Files
    Images 31 of How to Delete a File in Microsoft Windows Using Batch Files
    Add more file types if necessary. If you want to empty the folder of more than one file type, start a new line by pressing Enter, then type in the "Delete" command followed by the extension for the file type.
  10. images 32 of How to Delete a File in Microsoft Windows Using Batch Files
    Images 32 of How to Delete a File in Microsoft Windows Using Batch Files
    Save your file as a Batch file. Do the following:
    1. Click File.
    2. Click Save As....
    3. Type whatever you want to name your Batch file followed by .bat into the "File name" text box.
    4. Click the "Save as type" box, then click All Files.
    5. Select a save location on the left side of the window, then click Save.
  11. images 33 of How to Delete a File in Microsoft Windows Using Batch Files
    Images 33 of How to Delete a File in Microsoft Windows Using Batch Files
    Run your file whenever you need to clear the folder. Simply double-click the Batch file to do so. This will remove any file(s) matching your specified extension(s) from the folder.
    1. If you ever move the folder, you'll need to edit the Batch file to include the updated folder's address.
Part 5 of 5:

Emptying a Folder of All Files

  1. images 34 of How to Delete a File in Microsoft Windows Using Batch Files
    Images 34 of How to Delete a File in Microsoft Windows Using Batch Files
    Understand how this works. If you want to remove an entire folder's file contents without deleting the subfolders inside of the main folder, you can use a Batch file to do so.
    1. This method won't delete folders inside of the directory, but it will remove all files from those folders.
  2. images 35 of How to Delete a File in Microsoft Windows Using Batch Files
    Images 35 of How to Delete a File in Microsoft Windows Using Batch Files
    Right-click the folder you want to clear. This will open a drop-down menu.
  3. images 36 of How to Delete a File in Microsoft Windows Using Batch Files
    Images 36 of How to Delete a File in Microsoft Windows Using Batch Files
    Click Properties. It's at the bottom of the drop-down menu. Doing so opens the Properties window.
  4. images 37 of How to Delete a File in Microsoft Windows Using Batch Files
    Images 37 of How to Delete a File in Microsoft Windows Using Batch Files
    Copy the folder's location. Click and drag your mouse across the address in the "Location" section, then press Ctrl+C.
  5. images 38 of How to Delete a File in Microsoft Windows Using Batch Files
    Images 38 of How to Delete a File in Microsoft Windows Using Batch Files
    Open Notepad. Click Start
    images 39 of How to Delete a File in Microsoft Windows Using Batch Files
    Images 39 of How to Delete a File in Microsoft Windows Using Batch Files
    , type in notepad, and click Notepad when it appears at the top of the Start menu.
  6. images 40 of How to Delete a File in Microsoft Windows Using Batch Files
    Images 40 of How to Delete a File in Microsoft Windows Using Batch Files
    Enter the "Change Directory" command along with the folder's address. Type in cd and press the spacebar, then press Ctrl+V to paste in the folder's address.
  7. images 41 of How to Delete a File in Microsoft Windows Using Batch Files
    Images 41 of How to Delete a File in Microsoft Windows Using Batch Files
    Enter the command to delete all files without confirmation. Press Enter to start a line below the "Change Directory" command, then do the following:
    1. Type in del and then press the spacebar.
    2. Type in /S /Q and then press the spacebar. The "/S" ensures that any folders inside of the main folder also have their files deleted, and the "/Q" removes the confirmation prompt.
    3. Paste in the folder's address from earlier.
    4. Type in a backslash ().
    5. Type in the folder's name exactly as it appears in the address bar when you open the folder.
  8. images 42 of How to Delete a File in Microsoft Windows Using Batch Files
    Images 42 of How to Delete a File in Microsoft Windows Using Batch Files
    Save your file as a Batch file. Do the following:
    1. Click File.
    2. Click Save As....
    3. Type whatever you want to name your Batch file followed by .bat into the "File name" text box.
    4. Click the "Save as type" box, then click All Files.
    5. Select a save location on the left side of the window, then click Save.
  9. images 43 of How to Delete a File in Microsoft Windows Using Batch Files
    Images 43 of How to Delete a File in Microsoft Windows Using Batch Files
    Run your Batch file when needed. Whenever you want to clear out the folder and its subfolders, double-click the Batch file.
3.7 | 3 Vote
« PREV : How to Block an...
How to Delete a File... : NEXT »